我想在 Spring Boot 中对创建和更新 API 进行测试:
@Test
public void shouldCreateNewUser() throws Exception, UserAlreadyExistException {
User user = User.builder()
.id(10L)
.firstName("Alin")
.lastName("Balan")
.mail("alin@yahoo.com")
.password("pass")
.isAdmin(true)
.role(Roles.PTE)
.build();
mvc.perform(MockMvcRequestBuilders.post("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(user)))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$[10].id",is(10)));
Mockito.verify(userService, Mockito.times(1)).save(Mockito.any());
}
但我有这个错误
java.lang.AssertionError:JSON 路径“$[10].id”处没有值,异常:json 不能为 null 或为空