如何通过maven-failsafe-plugin运行基于spring-boot的应用程序的集成测试?
问题描述:
我有一个基于spring-boot的应用程序,并且pom.xml
文件的配置如下。如何通过maven-failsafe-plugin运行基于spring-boot的应用程序的集成测试?
<?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>
的main
方法位于DemoApplication
类,如下称为DemoIT
如下
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);
}
}
我的集成测试类。
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-maven-plugin
包括在pom.xml
,所以我需要做的是添加下面的配置,以确保我们的春天启动应用程序的生命周期良好围绕管理。
<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
,弹簧启动的应用程序将与我们的集成测试代码运行。
这是默认情况下发生的。对??原谅我,如果我错了。我是新来的! –