我正在关注this tutorial并已到达测试部分。当我在HelloControllerTest
的测试目录中创建HelloControllerIT
文件和src/test/java/hello/
文件时,只运行HelloControllerTest
测试。但是,如果我将第二个文件重命名为单词Test
,例如HelloController2Test
,那么它也会运行。我在使用mvn clean verify
的Maven构建期间从命令行运行测试。我在OSX上运行Maven 3.3.9。
我有两个主要问题:Maven如何知道要运行哪些测试?而且,更重要的是,我如何告诉Maven运行其他测试?以下是我的pom.xml
,直接来自tutorial:
<?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>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<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>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:4)
maven-surefire-plugin将by default尝试执行遵循Test*.java
或*Test.java
或*TestCase.java
模式的所有测试。它非常有意地忽略了你的HelloControllerIT
,就像标准的maven惯例那样,它不是单元测试,而是集成测试。默认情况下,maven-surefire-plugin在所有maven项目中都已启用。
有一个单独的插件maven-failsafe-plugin,用于运行集成测试,用命名模式IT*.java
或*IT.java
或*ITCase.java
表示(by default) 。它在构建的integration-test
阶段而不是test
阶段运行。但是,与maven-surefire-plugin不同,您需要明确启用它。 (我不知道为什么会这样。)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
将maven-failsafe-plugin添加到您的项目中,您的集成测试应该运行得很好。
答案 1 :(得分:2)
1)Maven如何知道要运行哪些测试?默认情况下,maven执行所有测试。我的意思是,Maven发现的所有测试。
2)如何告诉Maven运行其他测试?要执行特定的测试类(例如HelloController2Test),您可以执行以下操作:
mvn -Dtest=HelloController2Test test
答案 2 :(得分:0)
Maven surefire插件负责运行测试。以下链接应该回答您的问题:Inclusions and Exclusions of Tests