title: Testing Interface methods using Mockito
body: <p>I am writing a JUnit test case for a method which internally invokes another method through interface. I am using Mockito to mock the interface but for some reason it gives me <code>NPE</code>. I debugged through but wasn't able to get any clue to fix it. getAllVendors() method throws exception which comes through an Interface.</p>
  
  <p>MUT</p>
  
  <pre><code>public void prepare() throws AccountServiceException, ManagerException {
  vendors = getVendorManager().getAllVendors();
  
  microsites = new ArrayList&lt;VendorMicrositeTO&gt;();
  microsites.add( new VendorMicrositeTO( "http://www.docusign.com", "docuSign" ) );
  
  clientUser = createClientUserObject();
  
  }
  </code></pre>
  
  <p>JUnit</p>
  
  <pre><code>@Test
  public void testPrepare() throws Exception {
      AccountAction accountAction = new AccountAction();
      Map&lt;String, Object&gt; actionMap = new HashMap&lt;&gt;();
      actionMap.put("application", "ESignatureIntegrationAction");
  
      ActionContext.setContext(new ActionContext(actionMap));
      String beanName = Constants.VENDOR_MANAGER_SPRING_BEAN;
      PowerMockito.mockStatic(AppContext.class);
      PowerMockito.when(AppContext.containsBean( beanName )).thenReturn( true );
  
      IVendorDto iVendorDto = new VendorDto();
      iVendorDto.setActive(true);
      iVendorDto.setCreatedBy("9/15/2016");
      iVendorDto.setName("CorpESignClientUser");
      iVendorDto.setCreatedBy("SYSTEM");
  
      List&lt;IVendorDto&gt; vendorList = new ArrayList&lt;&gt;();
      vendorList.add(iVendorDto);
  
      IVendorManager iManager = Mockito.mock((IVendorManager.class));
      Mockito.when(iManager.getAllVendors()).thenReturn(vendorList);
  
      accountAction.setVendors(vendorList);
      accountAction.prepare();
  }
  </code></pre>
  
  <p>Stack trace</p>
  
  <pre><code>java.lang.NullPointerException
  at com.mercuryinsurance.esignature.ui.webapp.action.AccountAction.prepare(AccountAction.java:65)
  at test.com.mercuryinsurance.esignature.ui.webapp.action.TestAccountAction.testPrepare(TestAccountAction.java:58)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
  at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
  at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
  at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
  at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
  at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
  at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
  at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
  at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
  at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>
  
  <p>Thanks, in advance</p>