title: No bean found for definition [SpyDefinition... when using @SpyBean
body: <p>I have an application that listens for Kafka messages using <code>@KafkaListener</code> inside of a <code>@Component</code>. Now I'd like to make an integration test with a Kafka test container (which spins up Kafka in the background). In my test I want to verify that the listener method was called and finished, however when I use <code>@SpyBean</code> in my test I get:</p>
  
  <blockquote>
    <p><code>No bean found for definition [SpyDefinition@7a939c9e name = '', typeToSpy = com.demo.kafka.MessageListener, reset = AFTER]</code></p>
  </blockquote>
  
  <p>I'm using Kotling, important classes:</p>
  
  <p><strong>Class to test</strong></p>
  
  <pre><code>@Component
  class MessageListener(private val someRepository: SomeRepository){
  
      @KafkaListener
      fun listen(records: List&lt;ConsumerRecord&lt;String, String&gt;&gt;) {
           // do something with someRepository
      }
  }
  </code></pre>
  
  <p><strong>Base test class</strong></p>
  
  <pre><code>@ExtendWith(SpringExtension::class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
  @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  class KafkaContainerTests {
  
      // some functionality to spin up kafka testcontainer
  
  }
  </code></pre>
  
  <p><strong>Test class</strong></p>
  
  <pre><code>class MessageListenerTest @Autowired constructor(
          private val someRepository: SomeRepository
  ) : KafkaContainerTests() {
  
  
      @SpyBean
      private lateinit var messageListenerSpy: MessageListener
  
      private var messageListenerLatch = CountDownLatch(1)
  
      @BeforeAll
      fun setupLatch() {
          logger.debug("setting up latch")
  
          doAnswer {
              it.callRealMethod()
              messageListenerLatch.count
          }.whenever(messageListenerSpy).listen(any())
      }
  
      @Test
      fun testListener(){
          sendKafkaMessage(someValidKafkaMessage)
  
          // assert that the listen method is being called &amp; finished
          assertTrue(messageListenerLatch.await(20, TimeUnit.SECONDS))
          // and assert someRepository is called
      }
  }
  </code></pre>
  
  <p>The reason I am confused is that when I add the MessageListener to the <code>@Autowired</code> constructor of the <code>MessageListenerTest</code> it does get injected successfully.</p>
  
  <p>Why is the test unable to find the bean when using <code>@SpyBean</code>?</p>