Maven不执行任何单元测试

时间:2013-05-23 07:28:00

标签: unit-testing maven maven-surefire-plugin

我正在使用Maven和多模块。有3个项目。

foo(the parent project)
foo-core
foo-bar

我在foo的pom中配置了所有依赖项和插件:

<modules>
    <module>../foo-core</module>
    <module>../foo-bar</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            ...
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14.1</version>
                <dependencies>
                    <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.14.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

foo-core中有几个用于单元测试的基类和util类,因此我在maven-jar-plugin项目中添加foo-core以使其可用于foo-bar

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
               <execution>
                   <goals>
                       <goal>test-jar</goal>
                   </goals>
               </execution>
            </executions>
        </plugin>
    </plugins>
</build>

当我执行test目标时,我得到的结果如下:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

我的项目中有测试。但是为什么它没有运行任何一个呢?

4 个答案:

答案 0 :(得分:21)

将测试文件从**Tests.java重命名为**Test.java,或将以下配置添加到pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.14.1</version>
  <configuration>
    <includes>
      <include>**/*Tests.java</include>
    </includes>
  </configuration>
</plugin>

答案 1 :(得分:5)

另外,导致这种行为的另一个原因是,您在junit-vintage-engine依赖项中是否排除了pom.xml

在我的情况下,我已将其从spring-boot-starter-test的{​​{1}}依赖项中排除了

pom.xml

这导致了同样的问题。即使存在,Maven也无法识别/查找项目中的任何测试用例。

所以只需删除排除部分,然后再次运行maven命令。

答案 2 :(得分:0)

通过向 sunfire 插件添加依赖项,可以运行单元测试用例有类似的问题

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit4</artifactId>
                    <version>2.22.0</version>
                </dependency>
            </dependencies>
        </plugin>

通过在 mvn install 、 mvn test 和所有上执行测试用例,上面工作得很好

答案 3 :(得分:0)

就我而言,有必要在所有带有测试的文件的名称末尾添加单词 "Test"。 例如,如果您有 "LoginTestNegative",那么这将不起作用。您需要将其重命名为 LoginNegativeTest。现在它会起作用了。