Spring Boot: REST controller Test example

How it works

The annotation @WebMvcTest configure only the components that usually interest the web development.

As shown in the image @Service and @Repositoryare not configured.

When we call the @Service from the @Controller we return the mocked object.

Controller example

This is a very simple controller that calls a service and returns a custom object containing a text value:

public class SimpleController { 
    private SimpleService simpleService; 
    public SimpleController(SimpleService simpleService) { 
        this.simpleService = simpleService; 
    @GetMapping(value = "/simple",produces = MediaType.APPLICATION_JSON_VALUE) 
    public ResponseEntity<StringJsonObject> simpleResult() { 
        return ResponseEntity.ok(simpleService.getText()); 

Here the service code:

public class SimpleServiceImpl implements SimpleService{ 
    public StringJsonObject getText(){ 
        return new StringJsonObject("Cool!"); 

The returned object:

public class StringJsonObject { 
    private String content; 
    public StringJsonObject(String content) { 
        this.content = content; 
    public String getContent() { 
        return content; 

The test with comments

Here the code used to test the controller:

// SpringRunner is an alias of SpringJUnit4ClassRunner 
// it's a Spring extension of JUnit that handles the TestContext 
// we test only the SimpleController 
public class SimpleControllerTest { 
    // we inject the server side Spring MVC test support 
    private MockMvc mockMvc; 
    // we mock the service, here we test only the controller 
    // @MockBean is a Spring annotation that depends on mockito framework 
    private SimpleService simpleServiceMocked; 
    public void simpleResult() throws Exception { 
        // this is the expected JSON answer 
        String responseBody = "{\"content\":\"Hello World from Spring!\"}"; 
        // we set the result of the mocked service 
                .willReturn(new StringJsonObject("Hello World from Spring!")); 
        // the test is executed: 
        // perform: it executes the request and returns a ResultActions object 
        // accept: type of media accepted as response 
        // andExpect: ResultMatcher object that defines some expectations