gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:55 24 4
gpt4 key购买 nike

首先,为什么 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编译的输出

[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 测试的输出

[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(可能尚未完全支持),而不是 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);
}
}

关于java - Maven 找不到要执行的 junit 测试,表示它运行了 0 个测试并且构建成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48056067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com