如何通过 maven-failsafe-plugin 运行 对基于 spring-boot 的应用程序进行集成测试?
How to run integration test of a spring-boot based application through maven-failsafe-plugin?
我有一个基于 spring-boot 的应用程序,pom.xml
文件配置如下。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
位于classDemoApplication
的main
方法如下
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println("This is a demo application+++++++++++++++++++");
SpringApplication.run(DemoApplication.class, args);
}
}
我的集成测试 class 调用 DemoIT
如下。
package com.example.demo;
import org.junit.Test;
public class DemoIT {
@Test
public void test() {
System.out.println("This is a integration test.==============");
}
}
现在这是我的问题,当我发出 mvn clean verify
命令时,集成 class DemoIT
应该被执行,它确实执行了。但是,我的 DemoApplication
不是 运行。所以我想知道我的集成测试是否需要在 spring-boot 应用程序上下文下执行(DemoApplication 需要 运行),我应该怎么做才能实现?
由于我的应用是基于Spring-Boot,而spring-boot-maven-plugin
包含在pom.xml
中,所以我需要做什么是添加以下配置以确保我们的 Spring 启动应用程序的生命周期得到很好的管理。
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
然后当我发出 mvn clean verify
时,spring 引导应用程序将是 运行 我们的集成测试代码。
Spring Boot Maven Plugin
Last Published: 2021-01-22| Version:
2.5.x
它说
While you may start your Spring Boot application very easily from your
test (or test suite) itself, it may be desirable to handle that in the
build itself. To make sure that the lifecycle of you Spring Boot
application is properly managed around your integration tests, you can
use the start and stop goals as described below:
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.2.RELEASE</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
对于不熟悉集成测试的人,我发现this answer也很有帮助。
我有一个基于 spring-boot 的应用程序,pom.xml
文件配置如下。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
位于classDemoApplication
的main
方法如下
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println("This is a demo application+++++++++++++++++++");
SpringApplication.run(DemoApplication.class, args);
}
}
我的集成测试 class 调用 DemoIT
如下。
package com.example.demo;
import org.junit.Test;
public class DemoIT {
@Test
public void test() {
System.out.println("This is a integration test.==============");
}
}
现在这是我的问题,当我发出 mvn clean verify
命令时,集成 class DemoIT
应该被执行,它确实执行了。但是,我的 DemoApplication
不是 运行。所以我想知道我的集成测试是否需要在 spring-boot 应用程序上下文下执行(DemoApplication 需要 运行),我应该怎么做才能实现?
由于我的应用是基于Spring-Boot,而spring-boot-maven-plugin
包含在pom.xml
中,所以我需要做什么是添加以下配置以确保我们的 Spring 启动应用程序的生命周期得到很好的管理。
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
然后当我发出 mvn clean verify
时,spring 引导应用程序将是 运行 我们的集成测试代码。
Spring Boot Maven Plugin
Last Published: 2021-01-22| Version: 2.5.x
它说
While you may start your Spring Boot application very easily from your test (or test suite) itself, it may be desirable to handle that in the build itself. To make sure that the lifecycle of you Spring Boot application is properly managed around your integration tests, you can use the start and stop goals as described below:
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.2.RELEASE</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
对于不熟悉集成测试的人,我发现this answer也很有帮助。