0
当我的测试类位于默认包中时,下面的测试方法通过,当我将它移入包时失败。当移动到包中时,getResources()停止工作
的目录结构如下:
src/
main/
java/
some.package/
resources/
test/
FooTest.java // works here
some.package/
FooTest.java // does not work here
public class FooTest {
@Test
public void TestLoadImageFromFile() {
BufferedImage loadedImage = null;
try {
loadedImage = ImageIO.read(this.getClass().getResource("someImage.png"));
} catch(IOException e) {
e.printStackTrace();
}
assertNotNull(loadedImage);
}
...
getClass()。getResource()使用类加载器来加载资源。这意味着资源必须位于要加载的类路径中。 – StackFlowed 2014-11-03 16:52:00
为什么它会在一个位置上工作,但如果它从类路径中丢失,它不会在另一个位置上工作?它不应该不工作吗? – AnthonyW 2014-11-03 16:57:08
链接的问题完全不相关。它使用ClassLoader#getResource并解决了不同的问题。 – AnthonyW 2014-11-03 17:05:24