我正在做一个小的Gradle项目,并且希望在Spock测试中使用我的Reader类。当我开始测试时,Reader类会抛出:
java.nio.file.NoSuchFileException在我的Gradle项目中,/src/test/resources中有/src/test/resources文件。当我使用我的Reader运行测试时,Gradle告诉我:
java.nio.file.NoSuchFileException: file:project_path/build/resources/test/test.txt即使在该路径中有一个文件。我使用终端和浏览器进行了检查。
我在Ubuntu上使用Java 11。
//Reader class read lines:
try (Stream<String> stream = Files.lines(Path.of(String.valueOf(getClass().getClassLoader().getResource(fileName)))))
//gradle.build without test dependencies
apply plugin: "java"
apply plugin: "groovy"
group = "com.example"
version = "1.0"
sourceCompatibility = "1.11"
repositories {
mavenCentral()
}发布于 2019-02-18 06:45:16
更改您的Reader读取逻辑:
try (Stream<String> stream = Files.lines(Path.of(ClassLoader.getSystemResource(fileName).toURI()))) {
...
}当使用URI: / Path.of() _ path /build/resources/test/test.txt时,Path.of()将创建正确的路径,因为它将从URI中协议指定的提供程序中提取路径(文件:在您的例子中)。
但是,当您为(file:project_path/build/resources/test/test.txt参数调用Path.of()时,它假设它是您的实际路径String )。
https://stackoverflow.com/questions/54737839
复制相似问题