spring-boottestingjunitmockito

Cannot invoke "" because "" is null mockito /junit


as per title I am facing an issue when testing a service method.I suppose it should be some config problem or much more some wrong usage of mockito.

There is the code of the CustomerDataService

@Service
    public class CustomerDataService {

@Autowired
CustomerDataRepository customerDataRepository;

...

@Transactional
public void methodTotest(boolean consent, Long dialogId) {
    CustomerData customer = customerDataRepository.findCustomerByDialogId(dialogId);

    if (!consent) {
        
    customerDataRepository.deleteById(customer.getCustomerId());
    }else{
        customer.setConsentGiven(true);
        customerDataRepository.save(customer);
    }
}

In the test CustomerDataServiceTest

@SpringBootTest
class CustomerDataServiceTest {

@Autowired
CustomerDataService customerDataService;

@MockBean
CustomerDataRepository customerDataRepository;

@Test
void removeCustomerDataWhenConsentIsNotGiven() {
    CustomerData customerDataTest = customerData;
    //when
    customerDataService.giveConsent(false,22L);
    

    //then
    verify(customerDataRepository,times(1)).save(customerDataTest);
}

The error is

java.lang.NullPointerException: Cannot invoke "com.mypackage.CustomerData.getCustomerId()" because "customer" is null

this is the second line of the myMethod


Solution

  • 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 ;)