【发布时间】:2019-01-23 05:44:52
【问题描述】:
我有一个多模块 maven spring-boot maven 项目。其中一个子模块是一个存根(一个 Spring Boot 应用程序),我想在 myRealApp 的集成测试期间启动然后停止它。
Parent Pom
|
|----myRealApp module(spring boot app)
|----stub-of-some-remote-rest-api module(This is also a spring-boot app)
这是 myRealApp 模块中 pom 文件的样子。其中也有所有的集成测试。它试图在集成测试阶段之前启动存根模块。但是当我在子模块目录中运行 maven 目标时出现错误:
mvn integration-tests -X
错误:无法找到或加载主类 io.swagger.MyStub
org.apache.maven.lifecycle.LifecycleExecutionException: 失败 执行 目标 org.springframework.boot:spring-boot-maven-plugin:1.5.8.RELEASE:run (启动启动)项目:无法执行 java
我可以在调试模式下看到工作目录设置正确。
myRealApp 的 Pom:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
<executions>
<execution>
<configuration>
<workingDirectory>${project.parent.basedir}/module-of-my-stub/src/main/java</workingDirectory>
<mainClass>io.swagger.MyStub</mainClass>
</configuration>
<id>start-boot</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>stop-boot</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
也许我不允许在单个执行中设置workingDirectory,或者引用其他模块?
存根就像我的应用程序所依赖的远程休息服务一样,我在开发过程中使用了它,因为实际远程服务的测试环境不可靠并且有一半的时间下降了。所以决定在集成测试中也使用它。
【问题讨论】:
-
为什么要配置 spring-boot-maven-plugin 不同的执行?如果您喜欢编写集成测试,这很容易做到……此外,您为什么要使用这样一个奇怪的目录
/my-stub/src/main/java?请遵循 Maven 的约定和 Spring Boot 的约定....此外,您应该阅读有关测试 Spring Boot 应用程序的指南,因为不需要为 Spring Boot 应用程序编写存根... -
@khmarbaise 存根就像我的应用程序所依赖的远程休息服务一样,我在开发过程中使用它,因为实际远程服务的测试环境不可靠并且下降了一半时间。那么为什么不将它也用于集成测试呢?为什么它是一个奇怪的目录?我试图从我的应用程序中找到存根模块的主类。恐怕你没听懂我的问题。
-
@khmarbaise 我更新了问题
-
理想情况下,有一种方法可以将
module-of-my-stub依赖项添加到spring-boot-maven-plugin:run目标使用的类路径中。尽管有一个useTestClasspath 选项,但看起来没有直接的方法。 This link 还讨论了另一种方法。 -
反之亦然,但这是猜测。想法是通过将
module-of-my-stub的测试依赖项添加到myRealApp,然后将useTestClasspath添加到现有的spring-boot-maven-plugin:run配置中,这将使MyStub在类路径中对其可见。否则,上面链接中的classesDirectory技巧应该能够将其指向module-of-my-stub下的target/classes目录。
标签: java maven spring-boot spring-boot-maven-plugin