Java 在单元测试中访问资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24499692/
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
Access Resources in Unit Tests
提问by kwiqsilver
I'm using JUnit 4, Java 8, and Gradle 1.12.
I have a file with default json that I need to load. My project has src/main/java/
(containing the project source), src/main/resources/
(empty), src/test/java/
(unit test source), and src/test/resources/
(the json data file to load) directories. The build.gradle
file is in the root.
我使用的是 JUnit 4、Java 8 和 Gradle 1.12。我有一个需要加载的默认 json 文件。我的项目有src/main/java/
(包含项目源)、src/main/resources/
(空)、src/test/java/
(单元测试源)和src/test/resources/
(要加载的 json 数据文件)目录。该build.gradle
文件位于根目录中。
In my code I have:
在我的代码中,我有:
public class UnitTests extends JerseyTest
{
@Test
public void test1() throws IOException
{
String json = UnitTests.readResource("/testData.json");
// Do stuff ...
}
// ...
private static String readResource(String resource) throws IOException
{
// I had these three lines combined, but separated them to find the null.
ClassLoader c = UnitTests.class.getClassLoader();
URL r = c.getSystemResource(resource); // This is returning null. ????
//URL r = c.getResource(resource); // This had the same issue.
String fileName = r.getFile();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName)))
{
StringBuffer fileData = new StringBuffer();
char[] buf = new char[1024];
int readCount = 0;
while ((readCount = reader.read(buf)) != -1)
{
String readData = String.valueOf(buf, 0, readCount);
fileData.append(readData);
}
return fileData.toString();
}
}
}
From what I read, that should give me access to the resource file. However, I get a null pointer exception when I try to use the URL, because the getSystemResource()
call returns null.
从我读到的内容来看,这应该可以让我访问资源文件。但是,当我尝试使用 URL 时,我得到了一个空指针异常,因为getSystemResource()
调用返回 null。
How do I access my resource files?
如何访问我的资源文件?
采纳答案by Peter Niederwieser
Resource names don't start with a slash, so you'll need to get rid of that. The resource should preferably be read with UnitTests.getClassLoader().getResourceAsStream("the/resource/name")
, or, if a File
is required, new File(UnitTests.getClassLoader().getResource("the/resource/name").toURI())
.
资源名称不以斜杠开头,因此您需要去掉它。最好使用 读取资源UnitTests.getClassLoader().getResourceAsStream("the/resource/name")
,或者,如果File
需要,则使用new File(UnitTests.getClassLoader().getResource("the/resource/name").toURI())
。
On Java 8, you could try something like:
在 Java 8 上,您可以尝试以下操作:
URI uri = UnitTests.class.getClassLoader().getResource("the/resource/name").toURI();
String string = new String(Files.readAllBytes(Paths.get(uri)), Charset.forName("utf-8"));
回答by Joshua Goldberg
I think you want getResource instead of getSystemResource. The latter is used to, for example, read a file from the file system, where the path would not be specified in terms of the jar.
我认为你想要 getResource 而不是 getSystemResource。例如,后者用于从文件系统读取文件,其中路径不会根据 jar 指定。
You can also skip the class loader: UnitTests.class.getResource("...")
你也可以跳过类加载器: UnitTests.class.getResource("...")
Edit: there are some more detailed comments in the answers here.
编辑:有在回答一些更详细的评论在这里。