스프링 부트 서버의 REST 엔드 포인트를 테스트하는 통합 테스트를했습니다.Java - 스프링 부트 - 통합 테스트 - TestEntityManager 주입 안됨
(REST 엔드 포인트를 사용하지 않고) 일부 데이터를 생성해야하므로 TestEntityManager를 사용하려고합니다. 그래서 @SpringBootTest로 테스트 클래스에 주석을 추가했습니다. 지금까지는 테스트가 봄 부팅 컨텍스트를 시작하므로 내 서버와 테스트가 성공합니다.
문제점 :이 통합 테스트 외부에서 스프링 부트 서버를 시작하여 모든 통합 테스트 (각 테스트마다 새로운 인스턴스가 아닌)를 실행해야합니다. 이렇게하려면 사전 통합 테스트에서 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.springframewor[email protected]56bf6f1e] to prepare test instance [[email protected]]
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;
...
}
나는, 그에게 감사를 시도해 것이다. Btw, 내 지식으로, SpringBootTest 주석을 사용하여 TestEntityManager를 주입하는 방법은 무엇이겠습니까? 그리고 AutoConfigureTestEntityManager 주석이 작동하지 않는 이유는 무엇입니까? 종류. –
그냥 구성 클래스에서 하나를 반환하고 내가 추측 할 수있는 @Bean으로 주석을 달아주는 메서드를 만듭니다. 그러나 실제 컨테이너와 테스트 엔티티 관리자가 같은 컨테이너에 있으면 문제가 발생할 수 있으므로 켜기와 끄기를 제어하려면 스프링 프로파일을 사용해야 할 수도 있습니다. 하지만 스프링 부트 테스트 프레임 워크는 훌륭합니다. 나는 그것을 반대하지 않을 것입니다. 특히 "응용 프로그램 조각 테스트"지원이 유용 할 수 있습니다. AutoConfigureTestEntityManager가 작동하지 않는 이유는 무엇입니까? 패스; 어노테이션 처리를 켜기 위해 다른 어노테이션이 필요할 수도 있습니다. – PaulNUK
도움이 될 수도 있습니다. http://www.devthoughts.pl/2016/08/02/datajpatest-from-spring-boot/ – PaulNUK