您如何告诉Maven运行哪些测试?

Teo*_*tus 2 java spring maven

我正在关注本教程,并且已经到达测试部分。当我在测试运行时创建HelloControllerTest文件以及HelloControllerIT测试目录中的文件时。但是,如果我重命名第二个文件以单词开头,例如,则它也将运行。我在使用Maven构建过程中从命令行运行测试。我在OSX上运行Maven 3.3.9。src/test/java/hello/HelloControllerTestTestHelloController2Testmvn clean verify

我有两个主要问题:Maven如何知道要运行哪些测试?而且,更重要的是,我如何告诉Maven运行其他测试?下面是我的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>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>
Run Code Online (Sandbox Code Playgroud)

dcs*_*ohl 5

Maven的万无一失,插件会,在默认情况下,尝试执行遵循的模式的所有测试Test*.java*Test.java*TestCase.javaHelloControllerIT按照标准的maven约定,它非常有意地忽略了您,这不是单元测试,而是集成测试。默认情况下,所有maven项目中都启用了maven-surefire-plugin。

有一个单独的插件maven-failsafe-plugin,用于运行集成测试,默认情况下以命名模式IT*.java*IT.java或表示*ITCase.java。它在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>
Run Code Online (Sandbox Code Playgroud)

将maven-failsafe-plugin添加到您的项目中,您的集成测试应该可以正常运行。