预期状态:204实际:404

时间:2018-02-09 14:09:08

标签: java spring mockmvc spring-boot-test

您好,请参加综合测试。我的断言状态面临着极大的困难。

这是错误

  

java.lang.AssertionError:状态   预计:204   实际: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());

1 个答案:

答案 0 :(得分:1)

您的控制器方法始终返回404(未找到)。所以一切都按预期工作。

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