Код: Выделить всё
public ResponseEntity addCustomer(Customer customer) {
[validation etc...]
return new ResponseEntity(repository.save(customer), HttpStatus.OK);
}
Код: Выделить всё
@Test
public void addCustomer() throws Exception {
String json = "{" +
"\"name\": \"Test Name\"," +
"\"email\": \"test@email.com\"" +
"}";
Customer customer = new Customer("Test Name", "test@email.com");
when(service.addCustomer(customer))
.thenReturn(new ResponseEntity(customer, HttpStatus.OK));
this.mockMvc.perform(post(CustomerController.URI)
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").exists())
.andExpect(jsonPath("$.name", is("Test Name")))
.andExpect(jsonPath("$.email", is("test@email.com")))
.andExpect(jsonPath("$.*", hasSize(3)))
.andDo(print());
}
Код: Выделить всё
java.lang.AssertionError: No value at JSON path "$.id"
Подробнее здесь: https://stackoverflow.com/questions/494 ... -in-spring
Мобильная версия