我认为无法使用来对另一个模块进行集成测试
spring-boot-maven-plugin,因为
start目标似乎并未给您提供从本地存储库或Maven反应器解析应用程序的方法,而这可能正是您想要的。
project您尝试使用的配置属性并非旨在以这种方式覆盖。只能使用插件目标文档中列出的属性来配置插件执行。
相反,我认为您至少有两种可能的方法:
- 使用其他插件来控制服务器;要么
- 直接通过代码中的测试运行服务器。
选项1
为此,我认为您需要一种在您想要运行的服务器工件中复制的方法,以及一些用于启动和停止它的更常规的方法,例如cargo-
maven2-plugin或process-exec-maven-
plugin。
只需
repackage在服务器工件构建中配置目标:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludeDevtools>true</excludeDevtools> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions></plugin>
然后,从集成测试模块中,您可以执行以下操作:
<plugin> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-server-artifact</id> <goals> <goal>copy</goal> </goals> <phase>pre-integration-test</phase> <configuration> <artifactItems> <artifactItem> <groupId>${project.groupId}</groupId> <artifactId>SpringBoot2App</artifactId> <version>${project.version}</version> <classifier>jar</classifier> <outputDirectory>${project.build.directory}</outputDirectory> <destFileName>app.jar</destFileName> </artifactItem> </artifactItems> </configuration> </execution> </executions></plugin><plugin> <groupId>com.bazaarvoice.maven.plugins</groupId> <artifactId>process-exec-maven-plugin</artifactId> <version>0.7</version> <executions> <execution> <id>start-server</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> <configuration> <name>run-server</name> <waitForInterrupt>false</waitForInterrupt> <healthcheckUrl>http://localhost:8080</healthcheckUrl> <arguments> <argument>java</argument> <argument>-jar</argument> <argument>${project.build.directory}/app.jar</argument> </arguments> </configuration> </execution> <execution> <id>stop-server</id> <phase>post-integration-test</phase> <goals> <goal>stop-all</goal> </goals> </execution> </executions></plugin>选项2
只需在测试工件上声明对服务器工件的正常Maven依赖关系,然后
@SpringBootApplication在进行钩子或其他操作之前在JUnit中运行服务器的类,例如
private static ConfigurableApplicationContext context;@BeforeClasspublic static void setUp() { context = new SpringApplicationBuilder(SimpleServiceApplication.class).run();}@AfterClasspublic static void tearDown() { if (context != null) { context.close(); }}这可能足以满足您的需求。



