【问题标题】:Maven will not find junit tests to execute, says it ran 0 tests and the build succeededMaven 找不到要执行的 junit 测试,说它运行了 0 个测试并且构建成功
【发布时间】:2018-01-02 05:41:31
【问题描述】:

首先,为什么 maven 带有 test 目标,而且我看到人们运行 mvn surefire:test 就好像它在做其他事情一样。两者有什么区别?

其次,我似乎无法让 maven 运行我的单元测试。无论我尝试什么,我总是得到 Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 。我的myproject/target/generated-tests-sources/test-annotations 目录是空的,但我的myproject/target/test-classes 目录包含我的每个测试类的.class 文件。

项目结构:

  • myproject/src/main/java/MyClass.java
  • myproject/src/test/java/MyClassTest.java
  • myproject/pom.xml

示例 MyclassTest.java 内容:

import org.junit.jupiter.api.Test;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MyClassTest {
    @Test
    void testFunction() {
        assertEquals(0, 0);
    }
}

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>co.blocanse.pa.myproject</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.0.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

mvn compile的输出

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ myproject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to D:\projects\myproject\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.141 s
[INFO] Finished at: 2018-01-01T23:32:27-06:00
[INFO] Final Memory: 14M/213M
[INFO] ------------------------------------------------------------------------

mvn test的输出

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject  ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ myproject  ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myproject  ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ myproject  ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\projects\myproject\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myproject  ---
[INFO] Surefire report directory: D:\projects\myproject\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.362 s
[INFO] Finished at: 2018-01-01T23:38:07-06:00
[INFO] Final Memory: 15M/213M
[INFO] ------------------------------------------------------------------------

这就是问题所在。我希望测试结果会说它运行了 1 次测试,但它声称它通过并且没有运行任何测试,同时仍然成功构建。 mvn surefire:test 产生相同的结果。对于大量复制粘贴的内容,我深表歉意,但我想提供尽可能多的信息,因为我不确定下一步该尝试什么。如果有帮助,我在 Windows 上并使用 IntelliJ IDEA。

【问题讨论】:

    标签: java maven intellij-idea junit


    【解决方案1】:

    org.junit.jupiter.api.Test 是 junit 5(可能尚未完全支持),而不是 junit 4。删除 jupiter 依赖项并暂时使用 junit 4。来自https://github.com/junit-team/junit4/wiki/getting-started 的示例 jUnit 4 测试:

    import static org.junit.Assert.assertEquals;
    import org.junit.Test;
    
    public class CalculatorTest {
      @Test
      public void evaluatesExpression() {
        Calculator calculator = new Calculator();
        int sum = calculator.evaluate("1+2+3");
        assertEquals(6, sum);
      }
    }
    

    【讨论】:

    • 正在尝试,如果可行,会尽快报告。
    • 就是这样。从我的测试中删除 org.junit.jupiter.api.Test 依赖,pom.xml 做到了。谢谢!
    猜你喜欢
    • 2011-09-04
    • 2010-09-30
    • 2018-06-18
    • 2013-04-07
    相关资源
    最近更新 更多