如何通过maven-faila-securee-plugin运行基于弹簧启动的应用程序的集成测试?

我有一个基于弹簧引导的应用程序,该文件的配置如下。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>

位于类中的方法如下所示mainDemoApplication

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);
    }
}

我的集成测试类调用如下。DemoIT

package com.example.demo;

import org.junit.Test;

public class DemoIT {

    @Test
    public void test() {
        System.out.println("This is a integration test.==============");
    }
}

现在这是我的问题,当我发出命令时,集成类应该被执行,它确实执行了。但是,我的没有运行。所以我想知道我的集成测试是否需要在spring-boot应用程序上下文下执行(DemoApplication需要运行),我该怎么做才能实现它?mvn clean verifyDemoITDemoApplication


答案 1

这是一个文档

春季启动专家插件
上次发布时间: 2021-01-22|版本: 2.5.x

它说

虽然您可以从测试(或测试套件)本身非常轻松地启动Spring Boot应用程序,但在构建本身中处理它可能是可取的。要确保围绕集成测试正确管理 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>

对于那些不熟悉集成测试的人来说,我发现这个答案也很有帮助。


答案 2

由于我的应用程序基于Spring-Boot,并且包含在 中,因此我需要做的是添加以下配置,以确保Spring Boot应用程序的生命周期得到很好的管理。spring-boot-maven-pluginpom.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


推荐