title: Why is my test method returning null when I used Mockito's when?
body: <p>I am trying to mock, using Mockito 1.9.x, the following code which happens to be in the advice method of a Spring AOP join point</p>
  
  <pre><code>protected void check(ProceedingJoinPoint pjp) {
     final Signature signature = pjp.getSignature();
     if (signature instanceof MethodSignature) {
        final MethodSignature ms = (MethodSignature) signature;
        Method method = ms.getMethod();
        MyAnnotation anno = method.getAnnotation(MyAnnotation.class);
        if (anno != null) {
          .....
  }
  </code></pre>
  
  <p>Here is what I have for the mock so far</p>
  
  <pre><code>ProceedingJoinPoint pjp = mock(ProceedingJoinPoint.class);
  Signature signature = mock(MethodSignature.class);
  when(pjp.getSignature()).thenReturn(signature);
  
  MethodSignature ms = mock(MethodSignature.class);
  Method method = this.getClass().getMethod("fakeMethod");
  when(ms.getMethod()).thenReturn(method);
  
  ....
  </code></pre>
  
  <p>So I have to create a Method instance using a fakeMethod() within my test class since you cannot mock/spy final classes. Using the debugger I see that the method instance is good after the call to "this.getClass().getMethod("fakeMethod");" but within my check() method, method is null after it executes the line "Method method = ms.getMethod();" which causes an NPE on the very next line. </p>
  
  <p>Why would my method object be non-null in the test case but null in the method I am testing when using the when().thenReturn()?</p>