Maven不执行任何单元测试

Kir*_*Yao 18 unit-testing maven maven-surefire-plugin

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

foo(the parent project)
foo-core
foo-bar
Run Code Online (Sandbox Code Playgroud)

我在foopom中配置所有依赖项和插件:

<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>
Run Code Online (Sandbox Code Playgroud)

有几个基类和util类用于单元测试foo-core,所以我添加了maven-jar-pluginin 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>
Run Code Online (Sandbox Code Playgroud)

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

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

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Run Code Online (Sandbox Code Playgroud)

我的项目中有测试.但为什么它不运行任何一个?

Grz*_*Żur 22

从命名测试文件**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>
Run Code Online (Sandbox Code Playgroud)

  • 是否有任何理由它不能只扫描src/test/java中的所有类并默认查找@Test注释方法? (3认同)
  • 你是对的。问题是错误的导入。我使用了 `org.junit.Test` 的意图是 `org.testng.annotations.Test`,所以我反复收到错误。无论如何,谢谢你;-) (2认同)

Dis*_*ble 8

此外,这种行为的另一个原因是junit-vintage-engine您的pom.xml依赖项中有排除项。

就我而言,我已将其从spring-boot-starter-test我的依赖项中排除pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
  </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

因此,只需删除排除部分并再次运行 maven 命令。


Par*_*war 6

有类似的问题能够通过添加对 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>
Run Code Online (Sandbox Code Playgroud)

上面通过在 mvn install 、 mvn test 和所有上执行测试用例来正常工作