mocking one service api which is again calling repository api it is giving nullpointer exception
creating savingAccount with some validation. trying to write junit testcase for rest end point using mocktio
it is giving nullpointerexception when(savingsProductService.findByName(anyString())).thenReturn(Optional.of(savingsProduct));
line in test file.
Controller file....
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);
}
Servicefile...
public Optional<SavingsProduct> findByName(final String name) {
return savingsProductRepository.findByName(name).map(SavingsProductMapper::map);
}
RepositoryFile...
Optional<SavingsProductEntity> findByName(String var1);
i am writing test for controller API like this.
@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);
}