具有许多这样的集成测试实现:
// no @Annotations at all
class SomeIntegrationTest extends AbstractIntegrationTest {
...
}
使用(Spring Boot 1.5,JUnit 5)
@SpringBootTest(classes = {CoreConfiguration.class, RestTemplateAutoConfiguration.class, JacksonAutoConfiguration.class})
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@Transactional
public abstract class AbstractIntegrationTest {
...
}
这总是失败
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
除非我用
注释每个 IntegrationTest-实施@EnableAutoConfiguration
class SomeIntegrationTest extends AbstractIntegrationTest {
...
}
我想知道为什么我不能@EnableAutoConfiguration
AbstractIntegrationTest
并完成它。
(这样做会失败,并显示IllegalArgumentException: No auto-configuration attributes found. Is package.SomeIntegrationTest annotated with EnableAutoConfiguration?
)
我们的普通应用程序如下所示:
@SpringBootApplication
@Import({CoreConfiguration.class, OtherConfiguration.class})
public class WebApp {
在这里@SpringBootApplication
显然暗示着@EnableAutoConfiguration
,但我想避免以此为注释每个*IntegrationTest
,而是在AbstractIntegrationTest
上对其进行一次配置。
这是以任何方式与spring-boot对抗还是有某种方法可以实现这一目标?谢谢。
答案 0 :(得分:1)
您可以创建将AbstractIntegrationTest
抽象类更新为具有较小的内部配置类,例如TestConfiguration
使用@Import(TestConfiguration.class)
注释加载。
@SpringBootTest(classes = {CoreConfiguration.class, RestTemplateAutoConfiguration.class, JacksonAutoConfiguration.class})
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@Transactional
@Import(AbstractIntegrationTest.TestConfiguration.class) // <---- import the configuration
public abstract class AbstractIntegrationTest {
@EnableAutoConfiguration
// Any other applicable annotations e.g. @EntityScan
static class TestConfiguration {
}
....
}