java - 模块化 Spring Boot 2 应用程序导致集成测试失败
问题描述
我有一个更大的 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 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
但是现在 etc. 的集成测试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
pom.xml 文件中删除了主 SpringBootApp 类并删除了对 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
解决问题的注释(除了其他问题),但这是最重要的。
推荐阅读
- ios - Swift 在结构中制作 init 字典
- bootstrap-4 - 如何在引导程序中制作导航标签?
- javascript - 如何在anychart.js中为树图赋予自定义颜色
- firefox - Firefox 开发者工具:将消息限制在当前站点
- microsoft-dynamics - D365FO - 在到达日志中的装箱单上初始化自定义字段
- file - GNU 科学图书馆中的“文件格式无法识别”错误
- flutter - 颤振圆形滑块
- flutter - 如何在 Flutter 中从 App 导航到网站?
- javascript - 如何在 JavaScript 中编辑 .TXT 文件?
- angular - 在ngrx效果中获取json数据的问题