Sandy
Sandy

Reputation: 1

Even when mocking with Mockito I am getting Null Pointer Exception

I am using Mockito to mock a call to a method of a different service in Test code. However, the code where the actually call is made, for some reason instead of having my mocked value, it is null so my test fails with a Null Pointer Exception.

Here is a section of code of actual function. This code renews a jobTimeout for long running queries in a sepearate thread

CompanionTask.java 
public void run()
{ 
 try
 { 
    String jobLeaseTimeoutString = header.get(JOB_LEASE_TIMEOUT).get(0)
    Instant jobLeaseTimeout = Instant.parse(jobLeaseTimeoutString)
   if (jobLeaseTime != null)
   {
     if (isJobLeaseExpiring(jobLeaseTimeout))
     {
      ResponseEntity<ReportJob> reply = ctx.getReportingManagerClient().refreshReportJobLease(ctx.getJob().getId());
      jobLeaseTimeoutString = reply.getHeaders().get(JOB_LEASE_TIMEOUT).get(0);
    }
    else
    {
      sleep();
    }
  }
}

This is the Test where I mocked the call to ReportManagerClient but it is showing up as NULL when debugging

CompanionTaskTest.java
@MockBean
protected ReportingManagerIntegration reportingManagerClient;
@MockBean
protected NamedParameterJdbcTemplate jdbcTemplate;
@Test
public void testCompanionTask() 
{
  ReportJob reportJob = generateReportJob();
  QueryContext ctx = QueryContext.builder()
          .jdbc(jdbcTemplate)
          .busy(new AtomicInteger())
          .reportingManagerClient(reportingManagerClient)
          .job(reportJob)
          .build();

  ReportJob reportJobMock = ReportJob.builder()
         .createdAt(Instant.now())
         .expiresAt(Instant.now().plusSeconds(60)
         .build();

  Instant leaseRenewTimeout = Instant.now().plusSeconds(10);
  ResponseEntity responseEntity = ResonseEntity.status(HttpStatus.OK)
        .contentType(MediaType.APPLICATION_JSON)
        .header(JOB_LEASE_TIMEOUT, leaseTimeout.toString())
        .body(reportJobMock);

 Instant leaseRenewTimeout = Instant.now(0.plusSeconds(300);
 ResponseEntity<ReportJob> responseReportJob = ResponseEntity.ok()
       .contentTyoe(MediaType.APPLICATION_JSON)
       .header(JOB_LEASE_TIMEOUT, leaseTimeout.toString())
       .body(reportJobMock);
   when(ctx.getReportingManagerClient().refreshReportJobLease(anyString())).thenReturn(responseReportJob);

when(reportingManagerClient.refreshReportJobLease(anyString())).thenReturn(responseReportJob);
CompanionTask task = new CompanionTask(ctx, responseEntity.getHeaders());
something
task.run();

x

My work has stackvoerflow blocked , so I had to manually write all this code. Sorry for any typo I might have overlooked. So even though I have these two lines when(ctx.getReportingManagerClient().refreshReportJobLease(anyString())).thenReturn(responseReportJob); when(reportingManagerClient.refreshReportJobLease(anyString())).thenReturn(responseReportJob);

when the control is going to ResponseEntity reply = ctx.getReportingManagerClient().refreshReportJobLease(ctx.getJob().getId()); reply is NULL and that is giving me a NullPointerException.

Upvotes: 0

Views: 382

Answers (1)

knittl
knittl

Reputation: 265251

ArgumentMatchers such as Mockito.anyString() do not match null. Make sure your code is not passing null (and thus the stubbed method is not matched). If your stub needs to handle nulls use any() (matches anything, including null) or nullable(String.class) (matches any string and nulls).

Upvotes: 1

Related Questions