繁体   English   中英

Maven找不到要执行的junit测试,说它运行了0个测试,并且构建成功

[英]Maven will not find junit tests to execute, says it ran 0 tests and the build succeeded

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

org.junit.jupiter.api.Test是junit 5(可能尚未完全支持),而不是junit4。删除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);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM