[英]Modularize Spring Boot 2 App Results in Failing Integration Tests
我有一个较大的Spring Boot Application(Nr.1),其中包含约20多个模块(多模块设置)。
在里面,我有另一个Spring Boot Application(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 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和Service2IT)。
现在,我已经将module-app
重构为两个独立的模块module-app
和module-app-cli
因为我想重用module-app
。 结果如下所示:
module-app
+-- pom.xml (dependency on module-jpa and module-repos)
+-- src
+-- main
+-- java
+-- services
+-- Service1.java
+-- Service2.java
+-- src
+-- test
+-- java
+-- services
+-- Service1IT.java
+-- Service2IT.java
+-- resources
+-- application.properties
module-app-cli
+-- pom.xml (dependency on module-jpa, module-repos, module-app)
+-- src
+-- main
+-- java
+-- xyz
+-- application
+-- SpringBootApp.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)}
目前我不了解的是,仅通过将SpringBootApp
分离到一个单独的模块中会导致集成测试失败,这些测试与SpringBootApp.java
不相关,但基于结果看来它们是相关的? 问题是:这可以解决吗?
我想我疏忽了一些我不清楚的东西? 有人有什么建议可能导致这种行为吗?
一些细节:
使用Spring Boot版本2.1.3.RELEASE
如果您需要更多信息,请发表评论。
更新1
我正在尝试其他方法来解决此问题。 所以我从头开始,只是从module-app
删除了主要的SpringBootApp类,还删除了pom.xml文件中对spring-boot-maven-plugin的调用,结果很简单:
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
因此,在深入分析了这一点之后,我发现测试中缺少@EnableAutoConfiguration
批注,该批注解决了该问题(其他情况除外),但这是最重要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.