【问题标题】:How to run integration test of a spring-boot based application through maven-failsafe-plugin?如何通过 maven-failsafe-plugin 运行基于 spring-boot 的应用程序的集成测试?
【发布时间】:2018-03-05 14:01:48
【问题描述】:

我有一个基于 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>

main 方法位于类 DemoApplication 中,如下所示

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.==============");
    }
}

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

【问题讨论】:

    标签: java spring-boot integration-testing maven-failsafe-plugin spring-boot-maven-plugin


    【解决方案1】:

    由于我的应用基于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 boot 应用程序将与我们的集成测试代码一起运行。

    【讨论】:

    • 默认情况下会发生这种情况。正确的??如果我错了,请原谅我。我是新手!
    【解决方案2】:

    Here is a document

    Spring Boot Maven 插件
    最后发表:2021-01-22|版本: 2.5.x

    它说

    虽然您可以从您的 测试(或测试套件)本身,可能需要在 建立自己。确保 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>
    

    对于不熟悉集成测试的人,我发现this answer 也很有帮助。

    【讨论】:

    • 为了启动集成测试,您调用了哪些目标?您所说的配置是否每组 IT 仅启动/停止服务器一次?
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2016-12-30
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多