title: Spring Unit Testing: Using @SpyBean to mock a void method: getting NullPointerException for that bean unless I use @Mock instead
body: <p>i am trying to make @SpyBean or @MockBean work in this test as they are working in all my other tests. the only difference here is this test uses an active profile because it mocks some AWS libs </p>
  
  <p>I shouldnt have to instantiate the class it if I am using SpyBean, i've seen countless examples similar to mine below that seem to work fine.</p>
  
  <p>The unit I'm testing:</p>
  
  <pre><code>@Component
  public class SqsAdapter {
  
      @Autowired
      QueueMessagingTemplate queueMessagingTemplate;
  
      @Autowired
      MyProcessor processor;
  
      @SqsListener("${queue.name}")
      public void onEvent(String message) {
          if (!StringUtils.isEmpty(message)) {
              processor.process(message);
          } else { 
              log.error("Empty or null message received from queue");
          }
      }
  }
  </code></pre>
  
  <p>and a simple unit test to expect process(...) to be called:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = Application.class)
  @ActiveProfiles("local")
  public class SqsAdapterTest {
  
      @Autowired
      AmazonSQSAsync amazonSQS;
  
      @SpyBean
      MyProcessor processor;
  
      @InjectMocks
      SqsAdapter adapter;
  
      @Test
      public void testOnEvent() {
          doNothing().when(processor).process(any()); // &lt;---- process() is void
          String event = "royale with cheese";
          adapter.onEvent(event);
          verify(processor, times(1)).process(event);
      }
  }
  </code></pre>
  
  <p>when i debug in the unit test, i get a <code>java.lang.NullPointerException
  </code> on <code>processor</code> as if it never gets initialized:</p>
  
  <p>ive also tried switching it to @MockBean with the same results</p>
  
  <p>furthermore, i also tried stripping the testing of the @SpringBootTest and the @ActiveProfiles ... and removed the AmazonSQSAsync dependency since im not mocking the Listener itself, and it still throws the same NPE</p>
  
  <p>what am i missing here?</p>