gpt4 book ai didi

java - 状态预期为 :204 Actual :404

转载 作者:行者123 更新时间:2023-12-02 11:39:09 29 4
gpt4 key购买 nike

您好,我是集成测试新手。我的断言状态面临一些困难。

这是错误

java.lang.AssertionError: Status Expected :204 Actual :404

这是测试代码

    @Test
@Transactional
public void deleteCustomerTest() throws Exception{
//initialize database
customerRepository.saveAndFlush(customer);
int dataBaseSizeBeforeDelete = customerRepository.findAll().size();

//Get the customer to be deleted
restCustomerMockMvc.perform(delete( "/v1/customers/{uidpk}", customer.getUidpk())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isNoContent());

//Validate the database is empty
List<Customer> customers = customerRepository.findAll();
assertThat(customers).hasSize(dataBaseSizeBeforeDelete - 1);
}

这是请求

    @DeleteMapping(CUSTOMER_ID_ENDPOINT)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
@ApiResponses(value = {
@ApiResponse(code = 204, message = "The Customer was deleted", response = CustomerDto.class),
@ApiResponse(code = 404, message = "The Customer with the given uidpk id was not found", response = ResponseError.class),
@ApiResponse(code = 500, message = "Unexpected error")
})
@Timed
public ResponseEntity deleteCustomer(@PathVariable Long uidpk){
log.debug("[CustomerResource] DELETE {} : Deleting customer ({})", CUSTOMER_ID_ENDPOINT, uidpk );

try {
customerService.delete(uidpk);
}catch (EmptyResultDataAccessException e){
//No Customer found with this uidpk ID
ResponseError error = new ResponseError(HttpStatus.NOT_FOUND.getReasonPhrase(), "The customer with id " + uidpk+ " was not found");
log.error("[CustomerResource] Customer ({}) does not exist", uidpk);
return new ResponseEntity<>(error,null,HttpStatus.NOT_FOUND);
}

log.debug("[CustomerResource] Customer ([]) deleted", uidpk);
return new ResponseEntity<>(null,null,HttpStatus.NOT_FOUND);
}

当我点击函数名称时,它指向这一行:.andExpect(status().isNoContent());

最佳答案

您的 Controller 方法始终返回 404(未找到)。所以一切都按预期进行。

如果你想返回 402(无内容),你应该在删除语句后添加:return new ResponseEntity<>(null,null,HttpStatus.NO_CONTENT);并删除方法末尾的 return 语句。

关于java - 状态预期为 :204 Actual :404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48707737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com