我在测试资源包下的同一路径中有一个文件作为我的测试类。当我尝试在更改资源文件后使用./gradlew test
运行测试用例时,在读取该文件时(使用调试器验证)不会反映更改。我试过清理项目,但这没有用。
对于我在这里缺少的资源,是否还有其他一些缓存?
这是我正在阅读文件的方式:
File testFile = new File(this.getClass().getResource("namr_two").getFile());
getClass()。getResource()从类路径加载资源,而不是从文件系统加载资源。类加载器负责在运行时动态地将Java类和资源加载到JVM(Java虚拟机)。
我建议使用ClassLoader.getSystemResource():
File testFile = new File(ClassLoader.getSystemResource("namr_two").getFile());
要么:
Class class1 = null;
try {
class1 = Class.forName("class1");
ClassLoader cl = class1.getClassLoader();
File testFile = new File(cl.getResource("namr_two").getFile());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}