[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结尾或开头,我测试了这两种情况都没有改变),并且测试类应该放在适当的文件夹中。
当我运行: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 中正确生成。
这里可能的罪魁祸首是什么?
initializationError(package.maven.MyTest): The class package.maven.MyTest is not public.
你能否尝试在你的类中添加public修饰符? - SilverNak