0

I'm writing tests for my pet-project. Right now, I'm stuck with unit testing. I have a big method called "check-ownership", that returns an enum value.

During a run, this method changes numerous amount of objects.

Part of my method:

if(aruCalls.size() == 0) {
    aruCallDtoRequest.setDopFlag(0);
}

Take a look at aruCallDtoRequest.setDopFlag(0); line

Here is my test:

@ExtendWith(MockitoExtension.class)
class ReRegistrationServiceImplTest {



    @InjectMocks
    private ReRegistrationServiceImpl reRegistrationServiceImpl;

    @Mock
    private IntegrationClient integrationClient;
    @Mock
    private AruCallsService aruCallsService;
    @Mock
    private AruRequestService aruRequestService;
    @Mock
    private DealerService dealerService;

    private Dealer dealer;
    private Location location;
    private LocalDateTime currentTime;
    private CheckRegistrationDtoRequest checkRegistrationDtoRequest;
    private AruCallDtoRequest aruCallDtoRequest;
    private AruRequest aruRequest;
    private AruRequestDtoRequest aruRequestDtoRequest;
    private ResponseEntity<List<UsageDetailDtoResponse>> outgoingCalls;

    @BeforeEach
    public void init() throws H1NotAllowed, H1NotFound {
        aruCallDtoRequest = AruCallDtoRequest.builder()
                .customerMsisdn(TEST_NUMBER)
                .aruRequestsId(null).build();
    }


    @Test
    @DisplayName("Test for checking of Aru Request Creating at the initialization of the checkOwnership method")
    public void test_aruCallsSizeIsEqualToZeroAndAruRequestHasBeenCreated() throws H1NotAllowed, H1NotFound {
        verify(dealerService).findMe();
        verify(integrationClient).getLocation(TEST_NUMBER);
        verify(aruCallsService).findAllByOnRegisteredBetween(currentTime.minusHours(24),TEST_NUMBER);
        verify(aruRequestService).create(new AruRequestDtoRequest());
        assertEquals(result,CheckOwnershipDtoResponse.MORE_NUMBERS_NEEDED);
    }

Every time I run it, I receive a NPE at the line with the "aruCallDtoRequest.setDopFlag(0)". Any ideas how to get rid of this exception?

LordArnur
  • 31
  • 7
  • 1
    Please reduce this code to a [mre] and include the stacktrace. The usual causes for NPEs when using Mockito is that you didn't properly configure the mock, so it returns `null` when you call an unconfigured method as `null` is used as the default response. – Mark Rotteveel Dec 09 '22 at 08:54
  • Where does `aruCallDtoRequest` in your method come from? I don't see the field in your test being passed to your real service in any way. Probably another case of [Why is my class not calling my mocked methods in unit test?](https://stackoverflow.com/q/74027324/112968) – knittl Dec 09 '22 at 14:08

0 Answers0