0

I have the following method to test in my Java service class:

public void findPersonToDelete(String id){ //repository method then called personRepository.findPersonAndDelete(id); } 

The issue is that the personRepository calls other code which throws a Null Pointer.

I tried to use the following line to stop the personRepository from calling other methods:

 Mockito.doNothing().when(personRepository).findPersonAndDelete(id); 

However the error is still persisting? How can I fix this?

4
  • 1
    If you aren't calling it with whatever value id is, it will execute the method normally. Commented Aug 19, 2016 at 9:58
  • Is personRepository a mock? Commented Aug 19, 2016 at 9:59
  • 2
    try anyInt() rather than a specific value. Further PersonRepository needs to be a mock or a spy. Commented Aug 19, 2016 at 9:59
  • @Heisenberg yes its a mock Commented Aug 19, 2016 at 10:11

1 Answer 1

1

If you have a setter for your PersonRepository you can mock the class and set it in your service, this way the mocked class will be called and you can only check if it is called. so something like:

PersonRepository repo = Mockito.mock(PersonRepository.class); service.setPersonRepository(repo); service.findPersonToDelte(1); 

and then check what you want to test.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.