GET java.lang.AssertionError:预期状态:200实际:500使用mockMvc编写测试时

时间:2020-05-01 03:06:24

标签: spring-boot-test mockmvc

我正在使用Spring-boot 2.1.7-RELEASE,并在测试类中使用org.springframework.test.web.servlet.MockMvc和org.springframework.web.context.WebApplicationContext

这是我的终点站

@RequestMapping(value = "/user/{userId}",produces = { "application/json" }, method = RequestMethod.GET)
public User getUser(@PathVariable("userId") UUID userId, @Valid @RequestParam(value = "filters", required = false) List<String> filters){

//some processing and return User;

}

这是我的测试方法

@Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        this.mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();

    }

@Test
public void getAccount()throws Exception {
     mockMvc.perform(get("/user/1"))
            .andDo(print())
            .andExpect(status().isOk());
}

我收到以下错误

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /user/1
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8"]
             Body = null
    Session Attrs = {}

Handler:
             Type = org.springframework.cloud.netflix.zuul.web.ZuulController

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 500
    Error message = null
          Headers = [X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
     Content type = null
             Body = 
    Forwarded URL = /error
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status 
Expected :200
Actual   :500

有人可以帮助我了解为什么我会收到此错误吗?

0 个答案:

没有答案