Unit Testing Rest Services with Spring Boot and JUnit

て烟熏妆下的殇ゞ 提交于 2020-01-06 07:24:12

问题


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)

回答1:


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());

    }
}


来源:https://stackoverflow.com/questions/50605095/unit-testing-rest-services-with-spring-boot-and-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!