title: Mocking method in mockito returns a Null Pointer Exception?
body: <p>I'm writing a test that calls a method from another service:</p>
  
  <pre><code>  // Setup mocks
    private GoalService goalService;
    private Principal principal;
    private Goal goal;
    private SecurityService securityService;
  
    @Before
    public void setup() {
      goalService = new GoalService();
      principal = mock(Principal.class);
      goal = mock(Goal.class);
      securityService = mock(SecurityService.class, Mockito.RETURNS_DEEP_STUBS);
    }
  
    @Test
    public void testRequesterOwnsGoalIsTrue() {
      doReturn(1).when(securityService).getUserIdByPrincipal(principal);
      when(goal.getUsersUserId()).thenReturn(1);
      boolean userOwnsGoal = goalService.requesterOwnsGoal(principal, goal);
      assertEquals(true, userOwnsGoal);
    }
  </code></pre>
  
  <p>During debug, when I call <code>securityService.getUserIdByPrincipal(principal)</code> I get a NPE. I am not sure why. Ideally, what I want is for the method call to return 1, I don't care about testing <em>those</em> function internal in this unit test. </p>
  
  <h2>Actual Code</h2>
  
  <pre><code>  Boolean requesterOwnsGoal(Principal principal, Goal goal) {
      Optional&lt;Goal&gt; optionalGoal = Optional.ofNullable(goal);
      if (optionalGoal.isPresent()) {
        return secService.getUserIdByPrincipal(principal) == optionalGoal.get().getUsersUserId();
      }
      throw new StandardUserException("Requesting user does not have access to goal");
    }
  </code></pre>
  
  <p>Stack:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.habicus.core.service.Goal.GoalService.requesterOwnsGoal(GoalService.java:170)
      at com.habicus.core.service.Goal.GoalServiceTest.testRequesterOwnsGoalIsTrue(GoalServiceTest.java:82)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
      at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
      at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
      at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>