提供空指针异常

时间:2019-08-21 18:31:05

标签: java testing mockito

嘲笑一个服务api,该服务api再次调用存储库api,它给出了nullpointer异常

创建带有一些验证的SavingAccount。尝试使用mocktio为剩余端点编写junit测试用例

它在测试文件中给出了nullpointerexception when(savingsProductService.findByName(anyString())).thenReturn(Optional.of(savingsProduct));行。

控制器文件。...

ResponseEntity<ResponseModel> createSavingsProduct(@RequestBody  final SavingsProduct savingsProduct, final HttpServletRequest request,
                                                           final HttpServletResponse response) throws Exception {

    Set<ConstraintViolation<Object>> errors= savingsProductService.checkValidation(savingsProduct,new BigInteger("1"));
    if(errors.size()>0)
    {
        List<String> messages = new ArrayList<>();
        ResponseModel responseModel = new ResponseModel("");
        for(ConstraintViolation<Object> constraintViolation: errors)
        {
            messages.add(constraintViolation.getMessage());
        }
        responseModel.setData(messages);

        return ResponseEntity.badRequest().body(responseModel);

    }
    savingsProductService.findByName(savingsProduct.getName())
            .ifPresent(product -> {throw ServiceException.conflict("Duplicate name: " + product.getName());});

//  savingsProductService.findByIdentifier(savingsProduct.getIdentifier())
//  .ifPresent(product -> {throw ServiceException.conflict("Duplicate product short name: " + product.getIdentifier());});

    savingsProductService.findByShortName(savingsProduct.getShortName())
            .ifPresent(product -> {throw ServiceException.conflict("Duplicate short name: " + product.getShortName());});


    log.info("*****Create Savings Product*************** ");
    this.commandGateway.process(new CreateSavingsProductCommand(request.getHeader("UUID"), savingsProduct));
    ResponseModel responseModel = new ResponseModel("Accepted the Savings Product created");
    return ResponseEntity.accepted().body(responseModel);
}

服务文件...

public Optional<SavingsProduct> findByName(final String name) {
    return savingsProductRepository.findByName(name).map(SavingsProductMapper::map);
}

RepositoryFile ...

Optional<SavingsProductEntity> findByName(String var1);

我正在为控制器API编写测试。

@Test
public void createSavingsProduct() throws Exception {
    String testUrl = baseUrl + "/products";
    SavingsProduct savingsProduct = new SavingsProduct();
    savingsProduct.setName("Chocolate");
    savingsProduct.setShortName("Cho");

    CreateSavingsProductCommand createSavingsProductCommand = new CreateSavingsProductCommand(httpServletRequest.getHeader("UUID"), savingsProduct);
    Set<ConstraintViolation<Object>> errors = new HashSet<>();
    when(savingsProductService.checkValidation(savingsProduct,new BigInteger("1"))).thenReturn(errors);
    SavingsProductEntity savingsProduct1 = new SavingsProductEntity();
    savingsProduct1.setIdentifier(BigInteger.ONE);
    savingsProduct1.setName("xvm");

    //when(savingsProductRepository.findByName(anyString())).thenReturn(java.util.Optional.of(savingsProduct1));
    //when(savingsProductRepository.findByShortName(anyString())).thenReturn(java.util.Optional.of(savingsProduct1));

    when(savingsProductService.findByName(anyString())).thenReturn(Optional.of(savingsProduct));

    doNothing().when(commandGateway).process(createSavingsProductCommand);

    MvcResult result = mockMvc.perform(post(testUrl,httpServletRequest,httpServletResponse).content(new ObjectMapper().writeValueAsString(savingsProduct)).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn();

    assertThat(result.getResponse().getStatus()).isEqualTo(HttpStatus.ACCEPTED.value());
    verify(commandGateway).process(commandGateway);

}

0 个答案:

没有答案