Search code examples
restunit-testingspring-bootjunit4junit5

Unit Testing Rest Services with Spring Boot and JUnit


I have a basic SpringBoot app. using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I've defined this Rest method to get a User

  @GetMapping(path = "/api/users/{id}", 
                consumes = "application/json", 
                produces = "application/json")
    public ResponseEntity<User> getUser
                                    (HttpServletRequest request, 
                                    @PathVariable long id) {

        User user = checkAccess(request, id);
        return ResponseEntity.ok(user);

    }

I've created this Junit to test it

@ContextConfiguration(classes={TestSystemConfig.class})
@RunWith(SpringRunner.class)
@WebMvcTest(UserResourceController.class)
public class UserResourceControllerTests {

    @Autowired
    private MockMvc mvc;


    @MockBean
    private UserResourceController UserResourceController;

    @Test
    public void getUser() throws Exception {

        mvc.perform(get("/api/users/1")
                   .with(user("pere.peris@gmail.com").password("password"))
                   .contentType(APPLICATION_JSON))
                   .andExpect(status().isOk());

    }
}

But I got this error when I run the test:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Name for argument type [long] not available, and parameter name information not found in class file either.
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)

Solution

  • The reason is because you are mocking your controller. This is not necessary when you have @WebMvcTest(UserResourceController.class)

    This should work.

    @ContextConfiguration(classes={TestSystemConfig.class})
    @RunWith(SpringRunner.class)
    @WebMvcTest(UserResourceController.class)
    public class UserResourceControllerTests {
    
        @Autowired
        private MockMvc mvc;
    
        @Test
        public void getUser() throws Exception {
    
            mvc.perform(get("/api/users/1")
                       .with(user("pere.peris@gmail.com").password("password"))
                       .contentType(APPLICATION_JSON))
                       .andExpect(status().isOk());
    
        }
    }