title: @RunWith(MockitoJUnitRunner.class) mocks are null
body: <p>I am using the &quot;RunWith(MockitoJUnitRunner.class)&quot;, the @Mock notationt to mock the service and the @InjectMocks notation to inject the mock service to the controller. I get a NullPointerException in the ChargingStationsControllerTest:40, in the &quot;when&quot;. I debugged and realized that the mocks are null.
  Here is my code:</p>
  <pre><code>package com.example.assignmentchargingstations.controllers;
  
  
  import com.example.assignment.chargingStations.controllers.ChargingStationsController;
  import com.example.assignment.chargingStations.models.AddressInfo;
  import com.example.assignment.chargingStations.models.ChargingStation;
  import com.example.assignment.chargingStations.models.StatusType;
  import com.example.assignment.chargingStations.services.OpenChargeMapService;
  import org.junit.jupiter.api.Assertions;
  import org.junit.jupiter.api.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.junit.MockitoJUnitRunner;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import static org.mockito.ArgumentMatchers.anyDouble;
  import static org.mockito.Mockito.when;
  
  @RunWith(MockitoJUnitRunner.class)
  public class ChargingStationsControllerTest {
  
      @Mock
      private OpenChargeMapService openChargeMapService;
      @InjectMocks
      private ChargingStationsController chargingStationsController;
  
      @Test
      public void testGetNearestChargingStations() throws Exception {
          Double latitude = 90.0;
          Double longitude = 90.0;
          List&lt;ChargingStation&gt; chargingStationsListExpected = new ArrayList&lt;&gt;();
          StatusType statusTypeExpected = StatusType.builder().IsOperational(true).build();
          AddressInfo addressInfoExpected = AddressInfo.builder().Latitude(latitude).Longitude(longitude).Title(&quot;Test Charging Station&quot;).build();
          chargingStationsListExpected.add(ChargingStation.builder().StatusType(statusTypeExpected).AddressInfo(addressInfoExpected).build());
          when(openChargeMapService.getNearestChargingStations(anyDouble(), anyDouble())).thenReturn(chargingStationsListExpected);
          ResponseEntity&lt;List&lt;ChargingStation&gt;&gt; responseExpected = chargingStationsController.getNearestChargingStations(latitude, longitude);
          Assertions.assertEquals(responseExpected.getStatusCode(), HttpStatus.OK);
          Assertions.assertEquals(responseExpected.getBody(), chargingStationsListExpected);
      }
  }
  </code></pre>
  <p>The controller that I'm testing:</p>
  <pre><code>package com.example.assignment.chargingStations.controllers;
  
  
  import com.example.assignment.chargingStations.models.ChargingStation;
  import com.example.assignment.chargingStations.services.OpenChargeMapService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  
  import java.util.List;
  
  @Controller
  public class ChargingStationsController {
  
      private final OpenChargeMapService openChargeMapService;
  
      public ChargingStationsController(OpenChargeMapService openChargeMapService) {
          this.openChargeMapService = openChargeMapService;
      }
  
      @RequestMapping(path = &quot;/nearest-charging-stations&quot;, method = RequestMethod.GET)
      public ResponseEntity&lt;List&lt;ChargingStation&gt;&gt; getNearestChargingStations(@RequestParam Double latitude, @RequestParam Double longitude) {
          List&lt;ChargingStation&gt; nearestChargingStations = openChargeMapService.getNearestChargingStations(latitude, longitude);
          return new ResponseEntity&lt;&gt;(nearestChargingStations, HttpStatus.OK);
      }
  }
  </code></pre>
  <p>The service:</p>
  <pre><code>package com.example.assignment.chargingStations.services;
  
  import com.example.assignment.chargingStations.clients.OpenChargeMapClient;
  import com.example.assignment.chargingStations.models.ChargingStation;
  import org.springframework.http.HttpStatus;
  import org.springframework.stereotype.Service;
  import org.springframework.web.server.ResponseStatusException;
  
  import java.util.List;
  
  @Service
  public class OpenChargeMapService {
  
      private static final Double MAX_LATITUDE = 90.0000000;
      private static final Double MIN_LATITUDE = -90.0000000;
      private static final Double MAX_LONGITUDE = 180.0000000;
      private static final Double MIN_LONGITUDE = -180.0000000;
      private final OpenChargeMapClient openChargeMapClient;
  
      public OpenChargeMapService(OpenChargeMapClient openChargeMapClient) {
          this.openChargeMapClient = openChargeMapClient;
      }
  
      public List&lt;ChargingStation&gt; getNearestChargingStations(Double latitude, Double longitude) {
          validateLatitudeLongitude(latitude, longitude);
          List&lt;ChargingStation&gt; chargingStationsList = openChargeMapClient.getNearestChargingStations(latitude, longitude);
          return chargingStationsList;
      }
  
      private void validateLatitudeLongitude(Double latitude, Double longitude) {
          if (isLatitudeOutOfRange(latitude) || isLongitudeOutOfRange(longitude)) {
              throw new ResponseStatusException(HttpStatus.BAD_REQUEST, &quot;Values for latitude and longitude are out of range.&quot;);
          }
      }
  
      private boolean isLatitudeOutOfRange(Double latitude) {
          return (latitude.compareTo(MIN_LATITUDE) &lt; 0) || (latitude.compareTo(MAX_LATITUDE) &gt; 0);
      }
  
      private boolean isLongitudeOutOfRange(Double longitude) {
          return (longitude.compareTo(MIN_LONGITUDE) &lt; 0) || (longitude.compareTo(MAX_LONGITUDE) &gt; 0);
      }
  }
  </code></pre>
  <p>The build.gradle:</p>
  <pre><code>plugins {
      id 'org.springframework.boot' version '2.5.1'
      id 'io.spring.dependency-management' version '1.0.11.RELEASE'
      id 'java'
  }
  
  group = 'com.example'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '11'
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      implementation 'org.springframework.boot:spring-boot-starter'
      implementation 'org.springframework.boot:spring-boot-starter-web'
      implementation 'org.projectlombok:lombok:1.18.18'
      implementation 'junit:junit:4.13.1'
      annotationProcessor 'org.projectlombok:lombok:1.18.18'
      developmentOnly 'org.springframework.boot:spring-boot-devtools'
      testImplementation 'org.springframework.boot:spring-boot-starter-test'
  }
  
  test {
      useJUnitPlatform()
  }
  </code></pre>