Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Testing with @WebMvcTest | Integration Testing and Test Slices
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Spring Testing Concepts

bookTesting with @WebMvcTest

What is @WebMvcTest?

@WebMvcTest is a specialized annotation for testing only the web layer of your Spring application. It loads only the beans needed for controllers, filters, and related MVC components, making your tests faster and more focused.

Why use @WebMvcTest?

  • Focuses on testing Spring MVC controllers;
  • Does not load service, repository, or full application context beans;
  • Allows you to verify request mappings, validation, and controller logic.

Example: Testing a Simple Controller

Suppose you have a simple controller:

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

You can write a test for this controller using @WebMvcTest:

@WebMvcTest(GreetingController.class)
class GreetingControllerTest {
    @Autowired
    private MockMvc mockMvc;

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

Key Steps Explained

  • Add @WebMvcTest(GreetingController.class) to load only the GreetingController and necessary web components;
  • Inject MockMvc to simulate HTTP requests and inspect responses without starting a real server;
  • Use mockMvc.perform(get("/greet")) to send a GET request to the /greet endpoint;
  • Chain assertions to check the response status and body content.

Best Practices

  • Use @WebMvcTest for controller logic, request mappings, and web validation;
  • Mock any dependencies your controller needs using @MockBean if required;
  • Avoid using @WebMvcTest for service or repository testing — use other test slices or full integration tests for those layers.
question mark

Which annotation is used to configure a Spring Boot test that focuses only on controller layer components?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain the difference between `@WebMvcTest` and `@SpringBootTest`?

How do I mock dependencies in a controller test using `@WebMvcTest`?

What are some common pitfalls when using `@WebMvcTest`?

bookTesting with @WebMvcTest

Swipe um das Menü anzuzeigen

What is @WebMvcTest?

@WebMvcTest is a specialized annotation for testing only the web layer of your Spring application. It loads only the beans needed for controllers, filters, and related MVC components, making your tests faster and more focused.

Why use @WebMvcTest?

  • Focuses on testing Spring MVC controllers;
  • Does not load service, repository, or full application context beans;
  • Allows you to verify request mappings, validation, and controller logic.

Example: Testing a Simple Controller

Suppose you have a simple controller:

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

You can write a test for this controller using @WebMvcTest:

@WebMvcTest(GreetingController.class)
class GreetingControllerTest {
    @Autowired
    private MockMvc mockMvc;

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

Key Steps Explained

  • Add @WebMvcTest(GreetingController.class) to load only the GreetingController and necessary web components;
  • Inject MockMvc to simulate HTTP requests and inspect responses without starting a real server;
  • Use mockMvc.perform(get("/greet")) to send a GET request to the /greet endpoint;
  • Chain assertions to check the response status and body content.

Best Practices

  • Use @WebMvcTest for controller logic, request mappings, and web validation;
  • Mock any dependencies your controller needs using @MockBean if required;
  • Avoid using @WebMvcTest for service or repository testing — use other test slices or full integration tests for those layers.
question mark

Which annotation is used to configure a Spring Boot test that focuses only on controller layer components?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
some-alt