title: Mockito - throws nullpointerException when mocking a service
body: <p>I'm new to Mockito and testclasses in general.</p>
  
  <p>I'm trying to write a test class for my controller. When I run my test I want to mock my service to return a list of Dto objects. But when I do this I get the error.</p>
  
  <p>my code: </p>
  
  <p><strong>Controller class</strong></p>
  
  <pre><code>@Controller
  public class CalendarController {
  
  @Resource
  private CalendarService calendarService;
  
  @RequestMapping(method = RequestMethod.GET,value = RequestMappings.CALENDAR, produces = ContentType.APPLICATION_JSON)
  public ResponseEntity&lt;List&lt;CalendarDto&gt;&gt; getCalendarMonthInfo(@PathVariable final String userId, @PathVariable final String year)
  {
      List&lt;CalendarDto&gt; result = new ArrayList&lt;CalendarDto&gt;();
      result = calendarService.getMonthInfo(userId,Integer.parseInt(year));
  
      return new ResponseEntity&lt;&gt;(result, HttpStatus.OK);
  }
  </code></pre>
  
  <p><strong>Test class</strong></p>
  
  <pre><code>public class CalendarControllerTest extends BaseControllerIT {
  
      List&lt;CalendarDto&gt; calendarDto;
      CalendarDto test1 , test2;
      String userId = "20";
      String year = "2014";
  
      @Mock
      public CalendarService calendarService;
  
      @Before
      public void setUp() throws Exception {
          calendarDto = new ArrayList&lt;CalendarDto&gt;();
          test1 = new CalendarDto();
          test1.setStatus(TimesheetStatusEnum.APPROVED);
          test1.setMonth(1);
          test2 = new CalendarDto();
          test2.setMonth(2);
          test2.setStatus(TimesheetStatusEnum.REJECTED);
          calendarDto.add(test1);
          calendarDto.add(test2);
      }
  
      @Test
      public void testGet_success() throws Exception {
          when(calendarService.getMonthInfo(userId,Integer.parseInt(year))).thenReturn(calendarDto);
          performGet(UrlHelper.getGetCalendarMonthInfo(userId,year)).andExpect(MockMvcResultMatchers.status().isOk());
      }
  }
  </code></pre>
  
  <p>I get a nullPointerException in the test (When I call the "when" part). Looking further into it I saw that all the variables are oke but the service that I mock remains null.</p>
  
  <p>Am i forgetting to instantiate something or am I just completely wrong in how I'm doing this. </p>
  
  <p>Any help or pointers you can give are welcome.</p>