Java Spring boot:如何在单元测试中从类路径读取资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43428305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot: How to read resource from classpath in unit test
提问by Nithin Satheesan
I'm trying to read a file from classpath like this in my unit test:
我试图在我的单元测试中从类路径中读取一个文件:
@Value("classpath:state.json")
Resource stateFile;
I have state.json
file in src/test/resources
directory.
When I try to read this file using stateFile.getInputStream()
, it doesn't return any contents. What am I doing wrong?
我state.json
在src/test/resources
目录中有文件。当我尝试使用 读取此文件时stateFile.getInputStream()
,它不会返回任何内容。我究竟做错了什么?
My test class is annotated like this
我的测试类是这样注释的
@RunWith(SpringRunner.class)
@SpringBootTest
I can see that the code fails if I try with a incorrect file. So I think its seeing the file in classpath but for some reason not reading contents.
如果我尝试使用不正确的文件,我可以看到代码失败。所以我认为它在类路径中看到了文件,但由于某种原因没有读取内容。
采纳答案by Collin Krawll
I just ran into this. I'm using Maven. I took a look at my target/test-classes folder and my resource file wasn't in there (even though it was in my src/test/resources folder).
我刚遇到这个。我正在使用 Maven。我查看了我的 target/test-classes 文件夹,我的资源文件不在那里(即使它在我的 src/test/resources 文件夹中)。
I ran mvn clean install
and then rechecked my target/test-classes folder and the resource file was now there. After that, my test was able to find the file and the test worked.
我跑了mvn clean install
然后重新检查了我的目标/测试类文件夹,资源文件现在就在那里。之后,我的测试能够找到该文件并且测试成功了。
So it seems that your resources aren't copied until you do a mvn clean
. JUnit is looking in the classpath built by maven and until the file actually makes it into the target/test-classes folder, JUnit won't be able to find it.
因此,在您执行mvn clean
. JUnit 正在查找 maven 构建的类路径,直到该文件真正进入 target/test-classes 文件夹,JUnit 才能找到它。
回答by Praneeth Ramesh
You cant access a @Value resource unless its a property defined. It should be this way.
除非定义了属性,否则您无法访问 @Value 资源。应该是这样。
@Value("${stateJsonPath}")
Resource stateFile;
If you have to get the resource from hardcoded path then use this way.
如果您必须从硬编码路径获取资源,请使用这种方式。
Resource stateFile = new ClassPathResource("state.json");
回答by Alan Richards
Found this in another post. This works for being able to use the file.
在另一个帖子中找到了这个。这适用于能够使用该文件。
@SpringBootTest
public class SomeTest {
File catalogFile;
@Before
public void setUp() {
catalogFile = new File("src/test/java/resources/catalog.json");
}
@Test
public void catalog_model_is_useable () {
assert(catalogFile.exists());
}
}
}
回答by Levent Divilioglu
This should simply work, notice that the path starts with a dot, indicating current directory;
这应该很简单,注意路径以点开头,表示当前目录;
@Test
public void testFile() {
String path = "./src/test/resources";
String fileName = "test.zip";
File file = new File(path, fileName);
assertTrue(file.exists());
}