Maven不会发现执行junit测试,说它运行0次测试并且构建成功

时间:2018-01-02 05:41:31

标签: java maven intellij-idea junit

首先,为什么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。

1 个答案:

答案 0 :(得分:0)

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);
  }
}