【问题标题】:how to execute only integration test in spring boot using maven如何使用 Maven 在 Spring Boot 中仅执行集成测试
【发布时间】:2018-03-10 15:05:55
【问题描述】:

我正在尝试通过 maven 命令分离单元测试和集成测试。

pom.xml

....

        <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>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Fast*</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这是我的集成测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StudentApplication.class,
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StudentControllerIT {

...

这是单元测试

@RunWith(SpringRunner.class)
@WebMvcTest(value = StudentController.class, secure = false)
public class StudentFastControllerTest {

....

现在,当我尝试运行命令 mvn test 时,只执行 StudentFastControllerTest 测试,但是当我运行命令 mvn integration-test 或 mvn verify 时,两个测试类都被执行,而不是仅执行 StudentControllerIT。

【问题讨论】:

标签: maven spring-boot junit4 maven-failsafe-plugin


【解决方案1】:

编辑:有两种方法对我有用:

1) 使用 maven-failsafe 配置(我得到了来自 this answer 的帮助):

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
            <execution>
                <id>it-tests</id>
                <phase>none</phase> 
                <configuration>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
    </plugin>

请注意,此执行 i) 由词 it-tests 标识,并且 ii) 其阶段设置为 none。后者将这些测试从默认生命周期中分离出来,而前者使我们能够使用以下命令按需运行它们:

mvn failsafe:integration-test@it-tests

以IT 为后缀的集成测试也很重要,就像我们将它们包含在&lt;includes&gt; 部分中一样,我们对其他插件(例如surefire)的&lt;exclude&gt; 部分执行相同的操作。


2) 使用配置文件

在配置文件部分,您可以为集成测试添加配置文件:

            <profile>
            <id>it-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/*IT.java</include>
                            </includes>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

此外,将以下内容添加到配置文件配置部分如果您在单独的测试源目录中进行集成测试,在本例中为test-integration:

<testSourceDirectory>test/test-integration/java</testSourceDirectory>

现在返回插件部分,在您的maven-surefire-plugin 上,通过将以下内容添加到其配置部分来排除mvn test 中的集成测试:

<excludes>
    <exclude>**/*IT.java</exclude>
</excludes>

最后,将以下执行添加到您的 maven-failsafe-plugin:

<executions>
    <execution>
        <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
        </goals>
    </execution>
</executions>

因此(至少在我的情况下),在mvn test 期间没有执行集成测试,但在我运行时:

mvn failsafe:integration-test -Pit-tests

只有我的集成测试会被执行。

我希望这对你也有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2020-09-29
    • 1970-01-01
    • 2019-12-27
    • 2018-02-01
    • 2020-02-10
    • 2020-12-05
    • 2018-12-18
    相关资源
    最近更新 更多