Java - Spring boot - 集成测试 - TestEntityManager 没有被注入

F.d*_*doe 7 java integration-testing spring-boot

我有一个集成测试来测试我的 Spring Boot 服务器的 REST 端点。

我需要创建一些数据(不使用 REST 端点),所以我尝试使用 TestEntityManager,所以我用 @SpringBootTest 注释了我的测试类。到目前为止一切顺利,测试启动了 spring 引导上下文,所以我的服务器,测试通过了。

问题:我需要在此集成测试之外启动我的 Spring Boot 服务器,以便为所有集成测试运行一个实例(而不是每个测试中的新实例)。为此,我在预集成测试中使用 spring-boot-maven-plugin 启动服务器。到目前为止一切顺利,它开始了。但是,为了防止我的集成测试启动它自己的 Spring Boot 服务器,我需要从我的 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)
Run Code Online (Sandbox Code Playgroud)

这是代码

@RunWith(SpringRunner.class)
@ComponentScan(basePackages = {"be.mycompany"})
@AutoConfigureTestEntityManager
@Transactional
//@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MediaRestControllerIT extends AbstractIntegrationTest {
    @Autowired
    TestEntityManager testEntityManager;

    ...
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*NUK 2

“我需要在此集成测试之外启动我的 Spring boot 服务器,以便为所有集成测试运行一个实例(而不是每次测试都运行一个新实例)。”

\n\n

你不需要这样做。当您运行一组用@SpringBootTest注释的测试时,上下文将在测试之间被缓存。来自文档:

\n\n

“Spring\xe2\x80\x99s 测试框架会在测试之间缓存应用程序上下文。因此,只要您的测试共享相同的配置(无论 \xe2\x80\x99s 如何发现),加载上下文只会发生一次。”

\n\n

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

\n