Java 模块化Spring Boot 2应用程序导致集成测试失败
我有一个更大的Spring Boot应用程序(Nr.1),它由几个模块组成,大约20+(多模块设置) 在里面,我有另一个Spring Boot应用程序(Nr.2),它包含几个服务等Java 模块化Spring Boot 2应用程序导致集成测试失败,java,spring-boot,java-8,spring-boot-test,Java,Spring Boot,Java 8,Spring Boot Test,我有一个更大的Spring Boot应用程序(Nr.1),它由几个模块组成,大约20+(多模块设置) 在里面,我有另一个Spring Boot应用程序(Nr.2),它包含几个服务等 app-1 +... module-jpa +-- pom.xml +-- src/main/java/jpa/ (JPA Classes; Entities etc.) module-repos +-- pom.xml +-- src/main/java/repos/ JPA Repositor
app-1
+...
module-jpa
+-- pom.xml
+-- src/main/java/jpa/ (JPA Classes; Entities etc.)
module-repos
+-- pom.xml
+-- src/main/java/repos/ JPA Repositories; XYZ extends CrudRepository<..>.
module-app
+-- pom.xml (dependency on module-jpa and module-repos)
+-- src
+-- main
+-- java
+-- xyz
+-- application
+-- SpringBootApp.java
+-- services
+-- Service1.java
+-- Service2.java
+-- src
+-- test
+-- java
+-- services
+-- Service1IT.java
+-- Service2IT.java
+-- resources
+-- application.properties
但是现在Service1IT.java
等的集成测试不再有效,出现以下异常:
xyzTest(xyz.abcIT) Time elapsed: 0.141 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'firstService':
Unsatisfied dependency expressed through field 'secondService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name '.........'repos.ABCRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
因此,在深入分析之后,我发现测试缺少
@EnableAutoConfiguration
注释,该注释解决了问题(其他注释除外),但这是最重要的注释
xyzTest(xyz.abcIT) Time elapsed: 0.141 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'firstService':
Unsatisfied dependency expressed through field 'secondService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name '.........'repos.ABCRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
service1IT(services.Service1IT) Time elapsed: 0.004 s <<< ERROR!
java.lang.IllegalStateException:
Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'xyzService': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available