我写我的REST API控制器测试,我需要从返回JSON
对象检查UUID
值,请参阅该测试方法:MockMvc JsonPath结果匹配不匹配的单值
@Test
public void findById() throws Exception {
final String uuidString = "6c2b1c8a-3c29-4160-98b0-b8eaea7ea4d1";
final UUID id = UUID.fromString(uuidString);
final Envelope envelope = createEnvelope(id);
when(envelopeService.findOne(id, currentUser)).thenReturn(Optional.of(envelope));
when(utilService.getLoggedInUser()).thenReturn(currentUser);
mockMvc.perform(get("/api/envelopes/{id}", uuidString)).
andExpect(status().isOk()).andExpect(content().contentType(Util.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$..id", is(uuidString)));
verify(envelopeService, times(1)).findOne(id, currentUser);
//verifyNoMoreInteractions(envelopeService);
}
但测试产生这个错误:
Expected: is "6c2b1c8a-3c29-4160-98b0-b8eaea7ea4d1"
but: was <["6c2b1c8a-3c29-4160-98b0-b8eaea7ea4d1"]>
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)
似乎ID 6c2b1c8a-3c29-4160-98b0-b8eaea7ea4d1
返回正确,但在序列化到不同的结构。
我的代码有什么问题?
您可以发布impl。的api .. – Jaiwo99
这与具体实施无关。我需要知道如何检查返回的UUID值等于我的值。 – Artegon