title: Mocking void method returning a null pointer exception
body: <p>I am facing an issue when trying to unit test a function call. The call is failing for a void method invocation <code>messageProducer.sendMessage()</code> even though it has been stubbed.</p>
  
  <p>Please find below a simplified snapshot of my code. I am using a doAnswer() stubbing to mock the void method (based on earlier answers on StackOverflow).</p>
  
  <p>I even tried the other options of <code>doThrow()</code> and <code>doNothing()</code> stubbings, but they also fail with the same NPE when calling the stubbed method :(.  </p>
  
  <p>Appreciate if someone could suggest a solution/workaround. Many thanks.</p>
  
  <p>Test Class</p>
  
  <pre class="lang-java prettyprint-override"><code>// Test class
  @RunWith(MockitoJUnitRunner.class)
  public class RetriggerRequestTest {
      @Mock
      private MessageProducer messageProducer;
   
      @InjectMocks
      private MigrationRequestServiceImpl migrationRequestService;
   
      @Before
      public void init() {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void sendRetriggerRequest() throws Exception {
          // Below two stubbings also not Work, NPE encountered!
          //doNothing().when(messageProducer).sendMessage(any(), anyLong());
          //doThrow(new Exception()).doNothing().when(messageProducer).sendMessage(any(), anyLong());
  
          doAnswer(new Answer&lt;Void&gt;() {
              public Void answer(InvocationOnMock invocation) {
                  Object[] args = invocation.getArguments();
                  System.out.println("called with arguments: " + Arrays.toString(args));
                  return null;
              }
          }).when(messageProducer).sendMessage(any(EMSEvent.class), anyLong());
  
          try {
              // Gets Null pointer exception
              migrationRequestService.retriggerRequest(emsRetriggerRequest);
          }
          catch (Exception ex) {
              fail(ex.getMessage());
          }
      }
  
  </code></pre>
  
  <p>Implementation Class being tested, the stubbed method call from this class throws NPE as indicated in code comments</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  @Transactional
  public class MigrationRequestServiceImpl implements MigrationRequestService {
      @Autowired
      MessageProducer messageProducer;
  
      @Override
      public void retriggerRequest(EMSRetriggerRequestData emsRetriggerRequestData) throws EMSException {
          // Does a bunch of things
          submitTaskScheduledEventsToQueue(taskList);
      }
  
      private void submitTaskScheduledEventsToQueue(List&lt;Task&gt; taskList) {
          System.out.println("Debugging 1...");
          taskList.stream().forEach(task -&gt; {
              System.out.println("Debugging 2...");
              Map&lt;String, Object&gt; detailsMap = new HashMap&lt;String, Object&gt;();
              EMSEvent event = new EMSEvent(EMSEventType.TASK_SCHEDULED);
              event.setDetails(detailsMap);
  
              LOGGER.info(ContextRetriever.getServiceContext(), ContextRetriever.getRequestContext(), "*** Re-submitting Task: *** " + task.getId());
  
              // ****Gives a null pointer exception here****
              messageProducer.sendMessage(event, eventsConfigProperties.getScheduledEventDelay());
          });
          System.out.println("Debugging 3...");
      }
  }
  </code></pre>
  
  <p>Autowired class that is injected into the test class and whose method is throwing the NPE</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class MessageProducer {
  private static final Logger logger = LoggerFactory.getLogger(MessageProducer.class);
  
          private final RabbitTemplate rabbitTemplate;
  
          @Autowired
          public MessageProducer(RabbitTemplate rabbitTemplate) {
                  this.rabbitTemplate = rabbitTemplate;
      }   
  
      public void sendMessage(EMSEvent emsEvent, Long delay) {
          // code to send message to RabbitMQ here
      }   
  }
  </code></pre>