【发布时间】:2017-09-22 08:58:20
【问题描述】:
当我在 Maven 中运行测试时,我得到了这个:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
我的测试类 JsonReaderTest.class 放置在 src/test/java 中,据我从 maven-surefire-plugin 了解,它遵循正确的命名约定。
在 Maven 之外运行时测试运行良好。
我的 pom 中包含了这个插件:
<!-- Executes tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
这在我的依赖中:
<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
</dependency>
还有我的测试课:
package org.avalin.optaplanner.test.java;
import org.avalin.optaplanner.json.JsonReader;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
public class JsonReaderTest
{
@Test
@DisplayName("Test: No such file at designated path")
void testloadFromJsonTest() throws Exception
{
Throwable exception = assertThrows(FileNotFoundException.class,
()-> JsonReader.loadFromJson(".json"));
assertEquals(".json (No such file or directory)",
exception.getMessage());
}
@Test
@DisplayName("Test: Load Shifts from JSON (String instead of number)")
void testLoadShiftsFromJson3()
{
Throwable exception = assertThrows(NumberFormatException.class, ()-> JsonReader.loadFromJson(Paths.get("src/main/resources/org/avalin/optaplanner/json/faultyShift-2.json").toAbsolutePath().toString()));
assertEquals("\nOne or more of your \"shift\" elements has a number format exception.\n" +
"Check for errors in your JSON-properties.\n" +
"(Did you insert a string instead of a number in id?)",
exception.getMessage());
}
@Test
@DisplayName("Test: JSON is correctly loaded")
void testJsonIsLoaded()
{
assertFalse(JsonReader.jsonIsLoaded());
}
@AfterEach
void cleanJsonReader()
{
JsonReader.cleanJsonReader();
}
}
当我尝试用谷歌搜索这个问题时,似乎唯一可能出错的是命名约定(类必须以 test 结尾或以 test 开头,我测试了两者都没有改变)并且应该将测试类放入相应的文件夹。
当我运行时:mvn -Dtest=JsonReaderTest test
我得到以下信息:
Failed to execute goal org.apache.maven.plugins:maven-surefire-
plugin:2.20.1:test (default-test) on project optaplanner: No tests were
executed!
JsonReaderTest.class 也在 target/test-classes 中正确生成
这里的罪魁祸首可能是什么?
【问题讨论】:
-
您尝试使用哪个版本的junit?从您的测试类来看,在我看来您想使用 junit 5,但从您的 Maven 配置来看,似乎适用于 junit 4。
-
我在你的测试课中看不到'package'行?你的测试类在默认包中吗?
-
我尝试重新创建您的示例。我收到此错误:
initializationError(package.maven.MyTest): The class package.maven.MyTest is not public.您可以尝试在您的课程中添加公共修饰符吗?
标签: java maven unit-testing junit junit5