-2

I am writing unit test for my service layer by mocking the object behaviour using mockito framework, and right now, I am stuck in the middle of NullPointerException coming from one of my test, I have checked everything that needs to be checked but I can't seem to figure out why the test is failing.

I had this MockitoAnnotations.openMocks(this); at the beginning of the test before, If I take it off, another line in my test is throwing an exception, the exception is getting thrown as a result of the repository calling the actual database instead of the mocked object.

Cannot invoke "com.repository.CreatedRequestRepository.save(Object)" because "this.createdRequestRepository" is null java.lang.NullPointerException: Cannot invoke "com.repository.CreatedRequestRepository.save(Object)" because "this.createdRequestRepository" is null

Here is the failing test

  @Test
    public void creates_RequestTest() {
//        MockitoAnnotations.openMocks(this);
        Request requestWithServiceType = Request.builder()
                .id(1L).name("Aliyah").requestType(RequestType.SERVICE)
                .requestId("01278").requestStatus(RequestStatus.RECEIVED)
                .deptStatus(Dept.UNASSIGNED).submittedOn(LocalDate.now()).build();

        JSONObject jsonObject = new JSONObject();
        when(requestRepository.findById(Mockito.eq(1L))).thenReturn(Optional.of(requestWithServiceType));

        ApiResponse<?> response = requestServiceImpl.createRequest(jsonObject);

        Assertions.assertThat(response.getMessage()).isEqualTo("1");
        Assertions.assertThat(response.getDetails()).isNotNull();

        verify(requestRepository).findById(1L);

    }

the null pointer is coming from this method


@Override
    public ApiResponse<?> createRequest(JSONObject data) {
        JSONObject jsonObject = new JSONObject();
        for (String key : data.keySet()) {
            Object value = data.get(key);

            jsonObject.put(key, value);
        }

        CreatedRequest createdRequest = CreatedRequest.builder()
                .createdAt(LocalDateTime.now())
                .fields(jsonObject)
                .build();


 CreatedRequest createdRequestId = createdRequestRepository.save(createdRequest); // this line is throwing the null pointer
        return requestCreatedResponse(createdRequestId.getId(), jsonObject);
    }


here is the top of the class

    @Service
    @AllArgsConstructor
    public class RequestServiceImpl implements RequestService {

    private final RequestRepository requestRepository;

    private final CreatedRequestRepository createdRequestRepository;


    }
APY
  • 1
  • 2
  • Can you share some code from your test class please? – IveGotCodeButImNotACoder Aug 09 '23 at 15:28
  • Please share the test code – vnk Aug 09 '23 at 15:31
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Filburt Aug 09 '23 at 15:32
  • 2
    Please try to clarify and understand your problem one time instead of posting again for every time to encounter this issue: [I am getting a NullPointer exception from test unit and I can't seem to figure out where the issue is coming from](https://stackoverflow.com/q/75656269/205233) ... you are back to square one here with the same comments asking for relevant code. – Filburt Aug 09 '23 at 15:36
  • The method you're testing calls the repository save method, but you've defined a mock response for the repository findById method – lane.maxwell Aug 09 '23 at 17:10
  • Please show how you create `requestServiceImpl` and what annotations you have on your test class. Why did you remove `MockitoAnnotations.openMocks(this);`? – tgdavies Aug 10 '23 at 02:49

0 Answers0