Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Testing Controllers with MockMvc | Unit Testing and Mocking in Spring Boot
Spring Testing Concepts

bookTesting Controllers with MockMvc

Testing Spring Boot Controllers with MockMvc

Testing controllers is essential to ensure your application's endpoints work as expected. In Spring Boot, you can use the MockMvc tool to test controllers without starting the full server. This approach is fast, reliable, and lets you focus on controller logic.

What is MockMvc?

MockMvc is a Spring testing utility that allows you to simulate HTTP requests and assert responses. It works directly with your controller layer, so you can test endpoints just like a real client would, but without running the application server.

Setting Up a Simple Controller

Suppose you have a controller like this:

@RestController
public class GreetingController {
    @GetMapping("/greet")
    public String greet() {
        return "Hello, World!";
    }
}

Writing a MockMvc Test

You can write a test for this controller using MockMvc as follows:

@WebMvcTest(GreetingController.class)
public class GreetingControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testGreetEndpoint() throws Exception {
        mockMvc.perform(get("/greet"))
            .andExpect(status().isOk())
            .andExpect(content().string("Hello, World!"));
    }
}

Step-by-Step Explanation

  1. Use the @WebMvcTest annotation to load only the controller layer for testing;
  2. Inject the MockMvc object with @Autowired;
  3. Call the perform() method with an HTTP GET request to /greet;
  4. Chain assertions:
    • andExpect(status().isOk()) checks that the response status is 200 OK;
    • andExpect(content().string("Hello, World!")) checks that the response body matches the expected string.

Why Use MockMvc?

  • Simulates real HTTP requests without running a server;
  • Keeps tests fast and focused on controller behavior;
  • Lets you check both status codes and response content.

Use MockMvc whenever you want to test your Spring Boot controllers quickly and reliably.

question mark

What is the main purpose of using MockMvc in Spring Boot testing?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show how to test controllers with request parameters or path variables?

How do I test POST requests with MockMvc?

What should I do if my controller uses services or dependencies?

bookTesting Controllers with MockMvc

Swipe to show menu

Testing Spring Boot Controllers with MockMvc

Testing controllers is essential to ensure your application's endpoints work as expected. In Spring Boot, you can use the MockMvc tool to test controllers without starting the full server. This approach is fast, reliable, and lets you focus on controller logic.

What is MockMvc?

MockMvc is a Spring testing utility that allows you to simulate HTTP requests and assert responses. It works directly with your controller layer, so you can test endpoints just like a real client would, but without running the application server.

Setting Up a Simple Controller

Suppose you have a controller like this:

@RestController
public class GreetingController {
    @GetMapping("/greet")
    public String greet() {
        return "Hello, World!";
    }
}

Writing a MockMvc Test

You can write a test for this controller using MockMvc as follows:

@WebMvcTest(GreetingController.class)
public class GreetingControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testGreetEndpoint() throws Exception {
        mockMvc.perform(get("/greet"))
            .andExpect(status().isOk())
            .andExpect(content().string("Hello, World!"));
    }
}

Step-by-Step Explanation

  1. Use the @WebMvcTest annotation to load only the controller layer for testing;
  2. Inject the MockMvc object with @Autowired;
  3. Call the perform() method with an HTTP GET request to /greet;
  4. Chain assertions:
    • andExpect(status().isOk()) checks that the response status is 200 OK;
    • andExpect(content().string("Hello, World!")) checks that the response body matches the expected string.

Why Use MockMvc?

  • Simulates real HTTP requests without running a server;
  • Keeps tests fast and focused on controller behavior;
  • Lets you check both status codes and response content.

Use MockMvc whenever you want to test your Spring Boot controllers quickly and reliably.

question mark

What is the main purpose of using MockMvc in Spring Boot testing?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt