0
0
JUnittesting~15 mins

@WebMvcTest for controller testing in JUnit - Build an Automation Script

Choose your learning style9 modes available
Test GET /api/greeting endpoint in GreetingController
Preconditions (2)
Step 1: Send a GET request to /api/greeting
Step 2: Verify the response status is 200 OK
Step 3: Verify the response content type is application/json
Step 4: Verify the response body contains JSON with message field equal to 'Hello, World!'
✅ Expected Result: The GET /api/greeting request returns HTTP 200 with JSON {"message": "Hello, World!"}
Automation Requirements - JUnit 5 with Spring Boot @WebMvcTest and MockMvc
Assertions Needed:
Response status is 200
Response content type is application/json
Response JSON contains message field with value 'Hello, World!'
Best Practices:
Use @WebMvcTest to load only the controller layer
Use MockMvc to perform HTTP requests
Use @AutoConfigureMockMvc for MockMvc injection
Use explicit assertions on status, content type, and JSON content
Avoid loading full Spring context for faster tests
Automated Solution
JUnit
package com.example.demo;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(GreetingController.class)
public class GreetingControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void testGreetingEndpoint() throws Exception {
        mockMvc.perform(get("/api/greeting"))
            .andExpect(status().isOk())
            .andExpect(content().contentType("application/json"))
            .andExpect(jsonPath("$.message").value("Hello, World!"));
    }
}

// Assume GreetingController.java exists with @RestController and GET /api/greeting returning {"message":"Hello, World!"}

This test class uses @WebMvcTest to load only the GreetingController and related MVC components, making the test fast and focused.

The MockMvc object is injected to simulate HTTP requests without starting a server.

The test method testGreetingEndpoint sends a GET request to /api/greeting and asserts:

  • Status is 200 OK
  • Content type is application/json
  • JSON response has a message field with value Hello, World!

This ensures the controller behaves as expected.

Common Mistakes - 4 Pitfalls
Using @SpringBootTest instead of @WebMvcTest for controller tests
Not specifying the controller class in @WebMvcTest annotation
Not using MockMvc and trying to call controller methods directly
Hardcoding JSON strings in assertions without using jsonPath
Bonus Challenge

Now add data-driven testing to verify the greeting message changes based on a 'name' query parameter with 3 different names.

Show Hint