Using MockMvc

So far, we have tested our beans, but we need to test how our controller answers our HTTP requests. For example, what happens when we do an HTTP GET request from the /customers URL? To test this, we need a way to test each of our requests. Spring provides a class named MockMvc that simulates our server in a way that we can test our requests.

First, go to the CustomerController class. Select it in the Project window, and then select the class name in the editor. Press Shift + Ctrl + T and choose to Create New Test. This will be automatically named as CustomerControllerTest. We do not need to choose any options on Create Test window, just click on OK.

Now, we need to add our MockMvc object to the class:

package com.microservices.chapter09 ...

Get Hands-On Microservices with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.