No MockMvc created in Rest Controller Test

Hi i have seen here WebMvcTest not generated for controller class - #4 by cbontoiu that Diffblue uses MockMvc when creating tests for RestControllers.

For my Rest Controllers it does not work. I think the problem might be that Annotations like @RequestMapping and @PostMapping are not in the class itself but in an interface which is implemented by the Rest controller class. This is a common usecase to use the interfave for jersey proxy client mechanismn

Hi arnzel and thank you for contacting Diffblue support. We would like to investigate this case and understand what part of test writing does not work for you. We would be happy if you could kindly upload:

  • a piece of Java source code with the desired output for a unit test;
  • a piece of unit test code written by Diffblue, which refers to the source code above;
  • a log file which can be found the .diffblue folder either on your operating system user name folder if you run the IntelliJ plugin or in the project folder if you run from the console.

We are looking forward to helping you make the most of our products.
Kind Regards,

Cristian

Hi Christian

when i have this Interface

public interface HelloWorldEndpoint {

    @GetMapping(path = "/hello")
    public String helloWorld();
}

and this class

@RestController
public class HelloWorldEndpointImpl implements HelloWorldEndpoint {

    @Override
    public String helloWorld() {
        return "Hello";
    }
}

then the test is

@ContextConfiguration(classes = {HelloWorldEndpointImpl.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class HelloWorldEndpointImplTest {
    @Autowired
    private HelloWorldEndpointImpl helloWorldEndpointImpl;

    /**
     * Method under test: {@link HelloWorldEndpointImpl#helloWorld()}
     */
    @Test
    public void testHelloWorld() {
        assertEquals("Hello", helloWorldEndpointImpl.helloWorld());
    }
}

But when i move the Get Mapping from the interface to the class

@RestController
public class HelloWorldEndpointImpl implements HelloWorldEndpoint {

    @Override
    @GetMapping(path = "/hello")
    public String helloWorld() {
        return "Hello";
    }
}

then i get test using mockmvc

@ContextConfiguration(classes = {HelloWorldEndpointImpl.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class HelloWorldEndpointImplTest {
    @Autowired
    private HelloWorldEndpointImpl helloWorldEndpointImpl;

    /**
     * Method under test: {@link HelloWorldEndpointImpl#helloWorld()}
     */
    @Test
    public void testHelloWorld() throws Exception {
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello");
        MockMvcBuilders.standaloneSetup(helloWorldEndpointImpl)
                .build()
                .perform(requestBuilder)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=ISO-8859-1"))
                .andExpect(MockMvcResultMatchers.content().string("Hello"));
    }

    /**
     * Method under test: {@link HelloWorldEndpointImpl#helloWorld()}
     */
    @Test
    public void testHelloWorld2() throws Exception {
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello", "Uri Vars");
        MockMvcBuilders.standaloneSetup(helloWorldEndpointImpl)
                .build()
                .perform(requestBuilder)
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("text/plain;charset=ISO-8859-1"))
                .andExpect(MockMvcResultMatchers.content().string("Hello"));
    }
}

Do you see the point ? I would also expect that test is using mockmvc when the Annotation is in the interface

Hi arnzel,

Thank you for coming back with the code. It is clear now. This issue will be investigated by our developers as it is a great request from you, and one other user. We will come with a message as soon as possible.

Kind Regards
Cristian