29

我正在使用带有多模块的 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,因此我添加了maven-jar-plugininfoo-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

4 回答 4

36

将测试文件从 重命名**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>
于 2013-05-23T07:43:22.600 回答
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>

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

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

于 2020-06-30T15:39:05.900 回答
1

有类似的问题能够通过添加依赖于 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 和所有上执行测试用例,上述工作正常

于 2021-03-19T06:47:28.217 回答
0

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

于 2021-04-12T19:01:57.533 回答