title: How to mock a void method with PowerMockito?
body: <p>This is a duplicate of <a href="https://stackoverflow.com/questions/2276271/how-to-make-mock-to-void-methods-with-mockito">this question</a>. There are bunch of similar questions/answers as well but none helped me. As there are hundreds of developers accepted some answers I am probably wrong somewhere and have no idea where is my problem!</p>
  
  <p>This is my sample class and I want to test its method. </p>
  
  <pre><code>final class NavigationBuilder {
  
      @VisibleForTesting List&lt;Intent&gt; mIntentList = new ArrayList&lt;&gt;(5);
  
      @VisibleForTesting
      void addNextScreenBasedOnBookingStatus(final Booking booking) {
          final ChatMsgDbAsyncHelper helper = new ChatMsgDbAsyncHelper();
  
          if (booking == null) {
              helper.cleanAllMessages(mContext); // &lt;= Crash here
          } 
  
      }
  }
  </code></pre>
  
  <p>This is my test class:</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  @PrepareForTest({ChatMsgDbAsyncHelper.class, SplashActivity.class})
  public class NavigationBuilderTest {
  
      private SplashActivity mActivity;
      private NavigationBuilder mNavBuilder;
  
      @Before
      public void setUp() throws Exception {
  
          mActivity = new SplashActivity();
          ISplashView view = mock(ISplashView.class);
          PassengerStorage passengerStorage = mock(PassengerStorage.class);
  
          mNavBuilder = new NavigationBuilder(mActivity, view, passengerStorage);
      }
  
      @Test
      public void addNextScreenBasedOnBookingStatus_whenBookingIsNull() throws Exception {
          ChatMsgDbAsyncHelper spy = PowerMockito.spy(new ChatMsgDbAsyncHelper());
          PowerMockito.doNothing().when(spy).cleanAllMessages(mActivity);
  
          mNavBuilder.addNextScreenBasedOnBookingStatus(null);
  
          assertTrue(mNavBuilder.mIntentList.isEmpty());
      }
  }
  </code></pre>
  
  <p>Test fails and the reason is <code>NullPointerException</code> because test is running logic inside of <code>helper.cleanAllMessages(mContext);</code>. My expectation from above mock is those logic should not be performed.</p>
  
  <blockquote>
    <p>Caused by: java.lang.NullPointerException     at
    com.xxx.xxx.db.entities.ChatMessageTable.(ChatMessageTable.java:23)</p>
  </blockquote>