断言数组中的值,数组没有名字。

问题描述 投票:0回答:1

我试图嘲笑 HttpMessageNotReadable异常下面是Junit 5的代码。

@BeforeEach
    public void setUp()
    {
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); 
    }

    @Test
    void testHandleHttpMessageNotReadableException() throws Exception {
        String jsonStr = "{\"name\":\"ABC\"}";

        mockMvc.perform(post("/abc/pqr-stu")
                .content(jsonStr)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isBadRequest())
                //.andExpect(jsonPath("$").isArray())
                //.andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$.field", hasItem("somethingId")))
                .andExpect(jsonPath("$.message", hasItem("Something is Mandatory")));

        verify(mockRepository, times(0)).save(any(ModelConfig.class));

    }
  • 嘲讽后的URI:abcpqr-stu。

以下是日志

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /abc/pqr-stu
       Parameters = {}
          Headers = [Content-Type:"application/json", Content-Length:"491"]
             Body = <no character encoding set>
    Session Attrs = {}

MockHttpServletResponse:
           Status = 400
    Error message = null
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Content-Type:"application/json"]
     Content type = application/json
             Body = [{"field":"somethingId","value":null,"message":"Something is Mandatory"}]
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 6.735 s <<< FAILURE! - in com.XXXControllerTest
[ERROR] testHandleHttpMessageNotReadableException  Time elapsed: 0.269 s  <<< FAILURE!
java.lang.AssertionError: No value at JSON path "$.field"
    at com.controller.XXXControllerTest.testHandleHttpMessageNotReadableException(XXXControllerTest.java:60)
Caused by: com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['field'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
    at com.controller.XXXControllerTest.testHandleHttpMessageNotReadableException(XXXControllerTest.java:60)

Junit result
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   XXXControllerTest.testHandleHttpMessageNotReadableException:60 No value at JSON path "$.field"

所以从错误中,我发现测试用例在读取响应时失败了。

请帮我解决如何读取assert 字段,值,信息

[{"field":"somethingId","value":null,"message":"Something is Mandatory"}]
junit5 jsonpath assertion springmockito
1个回答
0
投票

下面的代码工作正常

mockMvc.perform(post("/abc/pqr-stu")
                .content(emptyJson).header(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isBadRequest())
                .andExpect(jsonPath("$").isArray())
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$.[0].field", is("somethingId")))
                .andExpect(jsonPath("$.[0].message", is("Something is Mandatory")));
© www.soinside.com 2019 - 2024. All rights reserved.