在spring启动应用程序上实现java单元测试用例。
当我使用java单元测试框架junit 5匹配预期数据和实际数据时,出现了这个错误。
这是我实现的代码,
@Test
public void testListAddress() throws Exception {
listAddress.add(new Address(1L, "road 89", "Dhaka", "Manikgonj1", "1800"));
listAddress.add(new Address(2L, "road 88", "Dhaka1", "Manikgonj2", "1801"));
Mockito.when(addressServiceImpl.findAllAddresses()).thenReturn(listAddress);
String url = "/api/v1/address";
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(url);
ResultMatcher ok = MockMvcResultMatchers.status().isOk();
final ObjectMapper objectMapper = new ObjectMapper();
String expectedJsonResponse = objectMapper.writeValueAsString(listAddress);
ResultActions mvcResult = (ResultActions) this.mockMvc.perform(builder).andExpect(ok)
.andExpect(MockMvcResultMatchers.jsonPath("$.data").value(expectedJsonResponse));
}
我的端点结果如下:
{
"status":true,
"message":"Address record lists!!",
"data":[
{"id":1,"street":"road 89","city":"Dhaka","state":"Manikgonj1","zip":"1800"},
{"id":2,"street":"road 88","city":"Dhaka1","state":"Manikgonj2","zip":"1801"}
]
}
堆栈跟踪:
java.lang.AssertionError: Got a list of values
[{"id":1,"street":"road 89","city":"Dhaka","state":"Manikgonj1","zip":"1800"},{"id":2,"street":"road 88","city":"Dhaka1","state":"Manikgonj2","zip":"1801"}]
instead of the expected single value
[{"id":1,"street":"road 89","city":"Dhaka","state":"Manikgonj1","zip":"1800"},{"id":2,"street":"road 88","city":"Dhaka1","state":"Manikgonj2","zip":"1801"}]
暂无答案!
目前还没有任何答案,快来回答吧!