title: Springs @MockBean is not always used
body: <p>I'm trying to test if my spring boot application correctly receives messages from an AWS SQS queue by using spring clouds aws messaging.</p>
  <p>Listener:</p>
  <pre><code>@Component
  public class RequestListener {
  
      private static final Logger LOG = LoggerFactory.getLogger(RequestListener.class);
  
      private final MyService myService;
  
      public RequestListener(MyService myService) {
          this.myService = myService;
      }
  
      @SqsListener(value = &quot;${aws.queue.url}&quot;, deletionPolicy = SqsMessageDeletionPolicy.ALWAYS)
      void getRequest(MyRequest request) {
          LOG.info(&quot;Fetched new message from SQS: {}&quot;, request);
          myService.processRequest(request);
      }
  }
  </code></pre>
  <p>Test:</p>
  <pre><code>@ExtendWith(SpringExtension.class)
  @ActiveProfiles(SpringProfiles.TEST)
  @SpringBootTest(webEnvironment = RANDOM_PORT)
  class TestClass {
  
  ...
  
  @MockBean
  MyService myService;
  
  @Test
  void testReceiveMessage() {
      var payload = MyRequest.builder().content(&quot;foo&quot;).build();
      messagingTemplate.convertAndSend(testQueueUrl, payload);
  
      when(myService.processRequest(payload)).thenReturn(true);
      verify(myService, Mockito.timeout(2000).atLeastOnce()).processRequest(payload);
  }
  ...
  </code></pre>
  <p>The message is received everytime (I see the log) but there's a 50% chance that the verify will fail as the Mock is never called (Error: zero interactions). I first thought this might be some synchronization issue (thus the timeout) but I can actually see that the real <code>MyService</code> service is called and not the Mock whenever the test fails.</p>
  <p>I run the tests using <code>mvn clean install</code></p>
  <p>So my question is, how is it possible that randomly somtimes the Mock and sometimes the real object is called?</p>
  <p>I also tried deactivating the real service with a custom <code>@Profile</code> annotation for the test but that didn't change the behaviour either.</p>
  <p>Tested with Boot-Version 2.3.9.RELEASE and spring cloud HOXTON.SR10
  (and also with Boot-Version 2.4.1 and cloud version HOXTON.SR10)</p>
  <p>Thanks for any hints</p>