Java 如何通过maven failsafe插件运行基于spring引导的应用程序的集成测试?
我有一个基于spring引导的应用程序,Java 如何通过maven failsafe插件运行基于spring引导的应用程序的集成测试?,java,spring-boot,integration-testing,maven-failsafe-plugin,spring-boot-maven-plugin,Java,Spring Boot,Integration Testing,Maven Failsafe Plugin,Spring Boot Maven Plugin,我有一个基于spring引导的应用程序,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:
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>
我的集成测试类名为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
命令时,应该执行集成类DemoIT
,它确实执行了。但是,我的DemoApplication
没有运行。因此,我想知道我的集成测试是否需要在spring boot应用程序上下文下执行(DemoApplication需要运行),我应该怎么做才能实现它?因为我的应用程序基于spring boot,并且spring boot maven插件
包含在pom.xml
中,因此,我需要做的是添加以下配置,以确保Spring Boot应用程序的生命周期得到良好的管理
<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 boot应用程序将使用我们的集成测试代码运行。
Spring Boot Maven插件最后发布日期:2021-01-22 |版本: 2.5.x 上面说 虽然您可以从计算机上轻松启动Spring Boot应用程序 测试(或测试套件)本身,最好在 建造自己。确保Spring Boot的生命周期 如果应用程序围绕集成测试进行了适当的管理,您可以 使用如下所述的开始和停止目标:
...
...
org.springframework.boot
也很有帮助。默认情况下会发生这种情况。正确的??如果我错了,请原谅我。。我是新手!为了启动集成测试,您会调用哪些目标?您正在谈论的配置是否仅在一组ITs中启动/停止服务器一次?
<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>