简体   繁体   English

预期状态:204实际:404

[英]Status Expected :204 Actual :404

hello please am new in intergration Tests. 您好,请加入集成测试。 Am facing sopme difficulties with my assertion status. 我的断言状态正面临一些麻烦。

here is the error 这是错误

java.lang.AssertionError: Status Expected :204 Actual :404 java.lang.AssertionError:预期状态:204实际:404

here is the test code 这是测试代码

    @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);
}

here is the Request 这是请求

    @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);
}

when i click onto the function name its point at this line : .andExpect(status().isNoContent()); 当我单击函数名称时,它在此行的位置: .andExpect(status()。isNoContent());

You controller method always returns 404 (not found). 您的控制器方法始终返回404(未找到)。 So everything is working as expected. 所以一切都按预期进行。

If you want to return 402 (no content) you should add this after the delete statement: return new ResponseEntity<>(null,null,HttpStatus.NO_CONTENT); 如果要返回402(无内容),则应在delete语句之后添加此内容: return new ResponseEntity<>(null,null,HttpStatus.NO_CONTENT); and remove the return statement at the end of the method. 并在方法末尾删除return语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM