title: Mock an Interface with Mockito return a NullPointerException
body: <p>I m trying create unit tests for one project.I m facing a problem because when I try control the result of an interface(mock). When the code get the Interface variable that return a NullPointerException.</p>
  
  <p>Firstly I tried @Override the method in my test class (ClassA), but it don't work. After that I tried mock the interface object and control the comportment with Mockito.When().tehnReturn();</p>
  
  <p>I will put here my code, I read some solutions but none works. </p>
  
  <p>My Interface:</p>
  
  <pre class="lang-java prettyprint-override"><code>    @FunctionalInterface
      public interface Interface {
          UpdateXResponse process(UpdateXRequest request) throws Exception;
      }
  </code></pre>
  
  <p>The class I want to test:</p>
  
  <pre class="lang-java prettyprint-override"><code>    @Service(ClassA.class)
      public class ClassA extends VService implements UpdateX {
  
          @Reference
          @Inject
          private Interface interface;
  
          @Inject
          public ClassA(...) {...}
  
          @Override
          public UpdateXResponse process(UpdateXRequest request) throws Exception {
              UpdateXResponse response = initResponse(context, request, new UpdateXResponse());
              UpdateXInput input = request.getInput();
              UpdateXOutput output = new UpdateXOutput();
              response.setOutput(output);
  
              try {
                  firstMethodCall(...);
  
              } catch (Exception t) {
                  throwCorrectException(t, logger);
              }
              return response;
          }
  
          private void firstMethodCall(...) throws Exception {
              TypeF typeF = callInterfaceMethod(...);
              ...
          }
  
          /**
           * Orchestrates Interface service
           */
          protected TypeF callInterfaceMethod(...) {
              ...
              request.setInput(input);
              request.setHeader(header);
  
              InterfaceResponse response = interface.process(request); // LINE ERROR - In this step interface is NULL when the test get this
  
              return response;
          }
      }
  </code></pre>
  
  <p>And finally my class test:</p>
  
  <pre class="lang-java prettyprint-override"><code>    @RunWith(PowerMockRunner.class)
      @PrepareForTest(value = {ClassA.class,Interface.class} )
      public class WithPowerMockUnitTest{
  
          @InjectMocks
          private ClassA classA;
  
          private Interface interface;
  
          @Before
          public void setUp() throws Exception {
              InterfaceRequest InterfaceRequest = createInterfaceRequest();
              InterfaceResponse serviceUnavailableResponse = createInterfaceResponse();
  
              Interface = Mockito.mock(Interface.class);
              when(Interface.process(Mockito.any(InterfaceRequest.class))).thenReturn(serviceUnavailableResponse);  
          }
  
          @Test
          public void testh() throws SOAException {
              InterfaceResponse res = interface.process(Mockito.any(InterfaceRequest.class)); // There all run ok. The interface is not null and return what i expected.
              System.out.println("RES "+res);
          }
          @Test
          public void test() {
              assertNotNull(classA);  // not null
              assertNotNull(interface); // not null
          }
  
          @Test
          public void newTest() throws Exception {
  
              InterfaceRequest InterfaceRequest = createInterfaceRequest();
              InterfaceResponse serviceUnavailableResponse = createInterfaceResponse();
  
              UpdateXResponse response = ClassA.process(updateXRequest()); // PROBLEM!! When that get the LINE ERROR the interface is null! WHY?      
          }
      }
  </code></pre>
  
  <p>I put some comments in the lines where the problem exists for me. </p>
  
  <hr>
  
  <pre class="lang-java prettyprint-override"><code>    public interface A{
          Response process(Request r) throws Exception;
      }
  
      public class B{
  
      private Class_That_Override_Interface_method  ctoim;
  
          public Response function(){
            X res = method_B();
          }
          protected X method_B(){
            response res = ctoim.process(request); // That ctoim is always NULL when the test get that line/call
          }
  
      }
  </code></pre>
  
  <p>Thanks</p>