[英]How to run integration test of a spring-boot based application through maven-failsafe-plugin?
I have a spring-boot based application, and the pom.xml
file is configured as below.我有一个基于 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>
The main
method located in class DemoApplication
as below位于类DemoApplication
的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);
}
}
And my integration test class called DemoIT
as following.我的集成测试类称为DemoIT
,如下所示。
package com.example.demo;
import org.junit.Test;
public class DemoIT {
@Test
public void test() {
System.out.println("This is a integration test.==============");
}
}
Now here is my question, when I issue mvn clean verify
command, the integration class DemoIT
is supposed to be executed, and it does.现在这是我的问题,当我发出mvn clean verify
命令时,应该执行集成类DemoIT
,它确实执行了。 However, my DemoApplication
isn't running.但是,我的DemoApplication
没有运行。 So I'm wondering if my integration test needs to be executed under the spring-boot application context (the DemoApplication needs running), what should I do to make it happen?所以我想知道我的集成测试是否需要在 spring-boot 应用程序上下文下执行(DemoApplication 需要运行),我应该怎么做才能让它发生?
Spring Boot Maven Plugin Spring Boot Maven 插件
Last Published: 2021-01-22|最后发布:2021-01-22| Version: 2.5.x版本:2.5.x
It says它说
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.虽然您可以很容易地从您的测试(或测试套件)本身启动您的 Spring Boot 应用程序,但在构建本身中处理它可能是可取的。 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:为了确保围绕集成测试正确管理 Spring Boot 应用程序的生命周期,您可以使用启动和停止目标,如下所述:
<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>
For who not familiar with integration test, I found this answer also quite helpful.对于不熟悉集成测试的人,我发现这个答案也很有帮助。
Since my application is based on Spring-Boot , and spring-boot-maven-plugin
is included in pom.xml
, so what I need to do is to add following configuration to make sure the lifecycle of our Spring Boot application is well managed around.由于我的应用程序是基于Spring-Boot 的,并且spring-boot-maven-plugin
包含在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>
Then when I issue mvn clean verify
, the spring boot application will be running with our integration test code.然后当我发出mvn clean verify
,spring boot 应用程序将与我们的集成测试代码一起运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.