Spring MVC 控制器测试 PUT
Spring MVC Controller testing PUT
尝试使用 junit5 和 mockito 测试我的 web 层(spring boot,spring mvc)。对 http 方法(get、put、...)的所有其他测试工作正常,但更新。
按照代码。
控制器:
@PutMapping(value = "{id}")
public ResponseEntity<?> putOne(@PathVariable Integer id, @Valid @RequestBody Customer customerToUpdate) {
Customer updated = customerService.update(id, customerToUpdate);
return ResponseEntity.ok(updated);
}
服务:
public Customer update(Integer customerId, Customer customerToUpdate) {
Customer customerFound = customerRepository.findById(customerId).orElseThrow(() -> {
throw new CustomerControllerAdvice.MyNotFoundException(customerId.toString());
});
customerToUpdate.setId(customerFound.getId());
return customerRepository.save(customerToUpdate);
}
终于测试了:
static final Customer oneCustomer = Customer.of(3,"john", LocalDate.of(1982, 11, 8));
@Test
void putOneTest() throws Exception {
when(customerService.update(oneCustomer.getId(), oneCustomer)).thenReturn(oneCustomer);
mockMvc.perform(put(CUSTOMER_URL + oneCustomer.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(oneCustomer)))
.andDo(print())
.andExpect(jsonPath("$.name").value(oneCustomer.getName()))
.andExpect(jsonPath("$.birthDate").value(oneCustomer.getBirthDate().toString()))
.andExpect(status().isOk());
}
结果:
java.lang.AssertionError: No value at JSON path "$.name"
CustomerService return 中的 update(...) 方法只是 null。无法理解的方式。请指教
问题出在这一行:
when(customerService.update(oneCustomer.getId(), oneCustomer)).thenReturn(oneCustomer);
你应该把它改成
when(customerService.update(eq(oneCustomer.getId()), any())).thenReturn(oneCustomer);
因为您的 put 请求正文是 JSON
,而不是真正的 Customer
,所以 when...thenReturn
语句没有像您预期的那样有效。默认情况下,模拟的 customerService
returns 为 null。这就是为什么您得到空响应的原因。所以你必须更正参数匹配器才能做到。
尝试使用 junit5 和 mockito 测试我的 web 层(spring boot,spring mvc)。对 http 方法(get、put、...)的所有其他测试工作正常,但更新。 按照代码。
控制器:
@PutMapping(value = "{id}")
public ResponseEntity<?> putOne(@PathVariable Integer id, @Valid @RequestBody Customer customerToUpdate) {
Customer updated = customerService.update(id, customerToUpdate);
return ResponseEntity.ok(updated);
}
服务:
public Customer update(Integer customerId, Customer customerToUpdate) {
Customer customerFound = customerRepository.findById(customerId).orElseThrow(() -> {
throw new CustomerControllerAdvice.MyNotFoundException(customerId.toString());
});
customerToUpdate.setId(customerFound.getId());
return customerRepository.save(customerToUpdate);
}
终于测试了:
static final Customer oneCustomer = Customer.of(3,"john", LocalDate.of(1982, 11, 8));
@Test
void putOneTest() throws Exception {
when(customerService.update(oneCustomer.getId(), oneCustomer)).thenReturn(oneCustomer);
mockMvc.perform(put(CUSTOMER_URL + oneCustomer.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(oneCustomer)))
.andDo(print())
.andExpect(jsonPath("$.name").value(oneCustomer.getName()))
.andExpect(jsonPath("$.birthDate").value(oneCustomer.getBirthDate().toString()))
.andExpect(status().isOk());
}
结果:
java.lang.AssertionError: No value at JSON path "$.name"
CustomerService return 中的 update(...) 方法只是 null。无法理解的方式。请指教
问题出在这一行:
when(customerService.update(oneCustomer.getId(), oneCustomer)).thenReturn(oneCustomer);
你应该把它改成
when(customerService.update(eq(oneCustomer.getId()), any())).thenReturn(oneCustomer);
因为您的 put 请求正文是 JSON
,而不是真正的 Customer
,所以 when...thenReturn
语句没有像您预期的那样有效。默认情况下,模拟的 customerService
returns 为 null。这就是为什么您得到空响应的原因。所以你必须更正参数匹配器才能做到。