The solution is simple. You mocked a class customerDataRepository but did not instruct it the mock what to do if the corresponding method is called. Mockito mocks then default back on doing nothing by method call and if there is a return value return null.
Since your returned customerData is null you get your NPE when calling on this object. In your case this is in the error case that you get by calling getCustomerId().
To solve this issue simply instruct your mock
@Test
void removeCustomerDataWhenConsentIsNotGiven() {
CustomerData customerDataTest = customerData;
//when
Mockito.when(customerDataRepository.findCustomerByDialogId(Mockito.any())).thenReturn(new CustomerData()); // <-- Add this line
customerDataService.giveConsent(false,22L);
//then
verify(customerDataRepository,times(1)).save(customerDataTest);
}
you can obviously replace Mockito.any() with Mockito.anyInt() or 42 and new CustomerData() with a object you previously created. I think you get the idea ;)
The solution is simple. You mocked a class customerDataRepository but did not instruct it the mock what to do if the corresponding method is called. Mockito mocks then default back on doing nothing by method call and if there is a return value return null.
Since your returned customerData is null you get your NPE when calling on this object. In your case this is in the error case that you get by calling getCustomerId().
To solve this issue simply instruct your mock
@Test
void removeCustomerDataWhenConsentIsNotGiven() {
CustomerData customerDataTest = customerData;
//when
Mockito.when(customerDataRepository.findCustomerByDialogId(Mockito.any())).thenReturn(new CustomerData()); // <-- Add this line
customerDataService.giveConsent(false,22L);
//then
verify(customerDataRepository,times(1)).save(customerDataTest);
}
you can obviously replace Mockito.any() with Mockito.anyInt() or 42 and new CustomerData() with a object you previously created. I think you get the idea ;)
Assuming that you have just corrected method names before posting it to Stackoverflow, and method you are calling in the test: giveConsent is, actually, the same method as methodTotest of the CustomerDataService.
Before calling customerDataService.giveConsent(false,22L);, you need to configure you repository to return some test (not null! or mocked) customerData entity:
when(customerDataRepository.findCustomerByDialogId(22L)).thenReturn(customerDataTest);
customerDataService.giveConsent(false,22L);
Note: since you are passing false as 1st variable, you will get to this branch of code
if (!consent) {
customerDataRepository.deleteById(customer.getCustomerId());
}
And in the test you are expecting save() method to be called, so the test will fail.
I had this issue and my problem was that I was calling my method with any() instead of anyInt(). So I had:
doAnswer(...).with(myMockObject).thisFuncTakesAnInt(any())
and I had to change it to:
doAnswer(...).with(myMockObject).thisFuncTakesAnInt(anyInt())
I have no idea why that produced a NullPointerException. Maybe this will help the next poor soul.
I had the same problem and my issue was simply that I had not annotated the class properly using @RunWith. In your example, make sure that you have:
@RunWith(MockitoJUnitRunner.class)
public class Test {
...
Once I did that, the NullPointerExceptions disappeared.
Method `Only.verify` throws `NullPointerException`
java - Mockito @Mock and @InjectMocks are null - Stack Overflow
How do I avoid the NullPointerException in Mockito, jUnit testing?
NullPointerException when using Mockito - OpenMRS Talk
@Test
public void testAddJobDescription()throws InvalidInputException
{
NewJobDescription newJobDescription = mock(NewJobDescription.class);
User user = new User();
when( jobDescriptionService.addJobDescription( newJobDescription, user ) ).thenReturn( new JobDescription());
assertEquals( new JobDescription().getExperience(), jobDescriptionService.addJobDescription( newJobDescription, user ).getExperience() );
}
Why wouldn't the when().thenReturn() clause work here? There shouldn't be any exception thrown here.