有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring mvc java。执行单元测试时lang.AssertionError

我有一个java。当我试图验证href时,lang.AssertionError。身体看起来很好

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8]}
     Content type = application/json;charset=UTF-8
             Body = {"itemName":"ThinkPad","links":[{"rel":"self","href":"http://localhost/items/001"}]}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

但是当被称为这句话时:andExpect(jsonPath("$.links[0].href", hasItem(endsWith("/items/001"))))

出现错误:

java.lang.AssertionError: JSON path "$.links[0].href"
Expected: a collection containing ""
     but: was "http://localhost/items/001"

    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74)
    at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:86)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
    at scnz.api.controller.ItemDispatchControllerTest.getCurrentStock(ItemDispatchControllerTest.java:62)

以下是测试代码:

@Test
    public void getCurrentStock() throws Exception {
        Item item = new Item("001", "ThinkPad");
        when(service.retrieve("001")).thenReturn(item);

        mockMvc.perform(get("/items/001"))
                .andDo(print())
                .andExpect(jsonPath("$.itemName", is(item.getItemName())))
                .andExpect(jsonPath("$.links[0].href", hasItem(endsWith("/items/001"))))
                .andExpect(status().isOk());

谁能找出哪里出了问题


共 (0) 个答案