首页 > 解决方案 > 模块化 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-appmodule-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-apppom.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

标签: javaspring-bootjava-8spring-boot-test

解决方案


因此,在深入分析之后,我发现测试缺少@EnableAutoConfiguration解决问题的注释(除了其他问题),但这是最重要的。


推荐阅读