title: @WebMvcTest with @Import does not work. Test context always asks for @Repository beans
body: <p>Using Spring Boot <code>2.7.3</code> I can not create a simple integration test for my API using <code>@WebMvcTest</code>.</p>
  <p>Here is my setup:</p>
  <pre><code>// GameServerApplicationTests.kt
  @SpringBootTest
  class GameServerApplicationTests {
      @Test
      fun contextLoads() { }
  }
  </code></pre>
  <pre><code>// CraftService.kt
  @Service
  class CraftService {
      fun getAll(): List&lt;String&gt; {
          return listOf(&quot;foo&quot;, &quot;bar&quot;)
      }
  }
  </code></pre>
  <pre><code>// CraftApiTest.kt
  @WebMvcTest
  @Import(value = [CraftService::class])
  class CraftApiTest {
      @Autowired
      private lateinit var testRestTemplate: TestRestTemplate
  
      @Test
      fun `should do accept craft all endpoint`() {
          val response = testRestTemplate.getForEntity(&quot;/craft/all&quot;, String::class.java)
          assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
      }
  }
  </code></pre>
  <p>When I run the test I see this exception:</p>
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemRepository' defined in com.gameserver.item.ItemRepository defined in @EnableJpaRepositories declared on GameServerApplication: Cannot create inner bean '(inner bean)#3fba233d' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3fba233d': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
  </code></pre>
  <p>I have no idea why it is looking for the <code>itemRepository</code> bean at all. I never asked for that.</p>
  <p>I then added this</p>
  <pre><code>@WebMvcTest
  @ComponentScan(excludeFilters = [ComponentScan.Filter(Repository::class)]) // &lt;&lt;
  @Import(value = [CraftService::class])
  </code></pre>
  <p>Which resulted in this exception:</p>
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'playerRepository' defined in com.gameserver.player.PlayerRepository defined in @EnableJpaRepositories declared on GameServerApplication: Cannot create inner bean '(inner bean)#30c1da48' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#30c1da48': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
  </code></pre>
  <p>Which confuses me even more. I explictly excluded all <code>@Repository</code> beans - but it just skipped <code>ItemRepository</code> and then asked for <code>PlayerRepository</code> now.</p>
  <p>I am totally lost and have no idea why I am not able to setup a simple integration test for my API endpoint.</p>
  <p>EDIT #1:</p>
  <p>Other tests run just fine:</p>
  <p><a href="https://i.stack.imgur.com/5y6J6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5y6J6.png" alt="enter image description here" /></a></p>
  <p>EDIT #2:</p>
  <p>I tried to use a <code>@Configuration</code> bean for <code>@Import</code>.</p>
  <pre><code>// CraftApiTestConfiguration
  @Configuration
  class CraftApiTestConfiguration {
      @Bean
      fun getCraftService(): CraftService {
          return CraftService()
      }
  }
  </code></pre>
  <pre><code>// CraftApiTest.kt
  @WebMvcTest
  @Import(CraftApiTestConfiguration::class)
  class CraftApiTest { // ... }
  </code></pre>
  <p>That did not help either. It just gave me the second exception mentioned above (the one asking for <code>playerRepository</code>)</p>