【发布时间】:2017-08-04 14:14:36
【问题描述】:
我有一个集成测试来测试我的 Spring 引导服务器的 REST 端点。
我需要创建一些数据(不使用 REST 端点),所以我尝试使用 TestEntityManager, 所以我用@SpringBootTest 注释了我的测试类。到目前为止一切顺利,测试启动了 spring boot 上下文,所以我的服务器,测试通过了。
问题:
我需要在此集成测试之外启动我的 Spring 引导服务器,以便为所有集成测试运行一个实例(而不是在每个测试中运行一个新实例)。
为此,我在预集成测试中使用 spring-boot-maven-plugin 启动服务器。
到目前为止一切顺利,它开始了。但是,为了防止我的集成测试启动它自己的 Spring 引导服务器,我需要从我的 IT 类中删除 @SpringBootTest 注释。
然后问题就来了。
即使使用注释 @AutoConfigureTestEntityManager,我的 TestEntityManager 也不会再被注入。
有什么想法吗? 非常感谢
2017-03-14 10:42:31,371 ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@56bf6f1e] to prepare test instance [be.mycompany.controllers.mediaRestControllerIT@340ef431]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
....
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testEntityManager' defined in class path resource [org/springframework/boot/test/autoconfigure/orm/jpa/TestEntityManagerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'testEntityManager' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
... 26 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
这里是代码
@RunWith(SpringRunner.class)
@ComponentScan(basePackages = {"be.mycompany"})
@AutoConfigureTestEntityManager
@Transactional
//@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MediaRestControllerIT extends AbstractIntegrationTest {
@Autowired
TestEntityManager testEntityManager;
...
}
【问题讨论】:
标签: java spring-boot integration-testing