0
0
JUnittesting~8 mins

@WebMvcTest for controller testing in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - @WebMvcTest for controller testing
Folder Structure
spring-boot-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/app/
│   │   │       ├── controller/
│   │   │       │   └── UserController.java
│   │   │       ├── service/
│   │   │       │   └── UserService.java
│   │   │       └── model/
│   │   │           └── User.java
│   │   └── resources/
│   │       └── application.properties
│   └── test/
│       ├── java/
│       │   └── com/example/app/
│       │       └── controller/
│       │           └── UserControllerTest.java
│       └── resources/
└── build.gradle
Test Framework Layers
  • Test Class Layer: Contains JUnit test classes annotated with @WebMvcTest to test Spring MVC controllers in isolation.
  • Controller Layer: Spring MVC controllers handling HTTP requests.
  • Mocked Service Layer: Services are mocked using @MockBean to isolate controller logic.
  • Test Utilities: Helper methods for building mock requests and responses, JSON converters.
  • Configuration Layer: Spring Boot test configuration, including mock MVC setup.
Configuration Patterns
  • Use @WebMvcTest(ControllerClass.class): Limits Spring context loading to web layer only, speeding up tests.
  • Mock Dependencies with @MockBean: Replace service beans to isolate controller behavior.
  • Profiles and Properties: Use application-test.properties or @TestPropertySource to configure environment-specific settings.
  • MockMvc Injection: Inject MockMvc to perform HTTP requests and verify responses.
  • JSON Serialization: Use JacksonTester or ObjectMapper for request/response body conversion.
Test Reporting and CI/CD Integration
  • JUnit Reports: Standard JUnit XML reports generated by build tools (Maven/Gradle) for test results.
  • IDE Integration: Run and debug @WebMvcTest tests inside IDEs like IntelliJ or Eclipse with clear pass/fail feedback.
  • CI/CD Pipelines: Integrate tests in pipelines (GitHub Actions, Jenkins) to run on each commit or pull request.
  • Code Coverage: Use tools like JaCoCo to measure how much controller code is covered by tests.
  • Test Logs: Capture detailed logs for failed tests to help debugging.
Best Practices
  1. Isolate Controller Tests: Use @WebMvcTest to load only the web layer, avoiding full application context startup.
  2. Mock External Dependencies: Use @MockBean to mock services and repositories, focusing tests on controller logic.
  3. Use MockMvc for HTTP Simulation: Perform GET, POST, PUT, DELETE requests and verify status, headers, and body.
  4. Test JSON Serialization: Verify request and response JSON formats match expected structure.
  5. Keep Tests Small and Focused: Each test should verify one controller endpoint or behavior clearly.
Self Check

Where in this folder structure would you add a new test class to verify the behavior of a newly created OrderController?

Key Result
Use @WebMvcTest to isolate and test Spring MVC controllers with mocked dependencies for fast, focused tests.