yeg*_*256 196 java unit-testing
我有一个单元测试,需要使用位于的XML文件src/test/resources/abc.xml
.获取文件内容的最简单方法是String
什么?
yeg*_*256 212
最后,我发现了一个简洁的解决方案,感谢Apache Commons:
package com.example;
import org.apache.commons.io.IOUtils;
public class FooTest {
@Test
public void shouldWork() throws Exception {
String xml = IOUtils.toString(
this.getClass().getResourceAsStream("abc.xml"),
"UTF-8"
);
}
}
Run Code Online (Sandbox Code Playgroud)
完美的工作.文件src/test/resources/com/example/abc.xml
已加载(我正在使用Maven).
如果替换"abc.xml"
为,例如,"/foo/test.xml"
将加载此资源:src/test/resources/foo/test.xml
你也可以使用Cactoos:
package com.example;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.TextOf;
public class FooTest {
@Test
public void shouldWork() throws Exception {
String xml = new TextOf(
new ResourceOf("/com/example/abc.xml") // absolute path always!
).asString();
}
}
Run Code Online (Sandbox Code Playgroud)
pab*_*vix 109
正确的观点:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
Run Code Online (Sandbox Code Playgroud)
Gle*_*est 53
假设UTF8编码在文件中 - 如果没有,只省略"UTF8"参数,并将在每种情况下使用底层操作系统的默认字符集.
JSE 6中的快捷方式 - 简单且无第三方库!
import java.io.File;
public class FooTest {
@Test public void readXMLToString() throws Exception {
java.net.URL url = MyClass.class.getResource("test/resources/abc.xml");
//Z means: "The end of the input but for the final terminator, if any"
String xml = new java.util.Scanner(new File(url.toURI()),"UTF8").useDelimiter("\\Z").next();
}
}
Run Code Online (Sandbox Code Playgroud)
JSE 7(未来)的快捷方式
public class FooTest {
@Test public void readXMLToString() throws Exception {
java.net.URL url = MyClass.class.getResource("test/resources/abc.xml");
java.nio.file.Path resPath = java.nio.file.Paths.get(url.toURI());
String xml = new String(java.nio.file.Files.readAllBytes(resPath), "UTF8");
}
Run Code Online (Sandbox Code Playgroud)
虽然没有打算用于大量文件.
Kir*_*oll 13
首先确保将abc.xml
其复制到输出目录.然后你应该使用getResourceAsStream()
:
InputStream inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("test/resources/abc.xml");
Run Code Online (Sandbox Code Playgroud)
拥有InputStream后,您只需将其转换为字符串即可.该资源说明了这一点:http://www.kodejava.org/examples/266.html.但是,我会摘录相关代码:
public String convertStreamToString(InputStream is) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
使用Google Guava:
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
public String readResource(final String fileName, Charset charset) throws Exception {
try {
return Resources.toString(Resources.getResource(fileName), charset);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
例:
String fixture = this.readResource("filename.txt", Charsets.UTF_8)
Run Code Online (Sandbox Code Playgroud)
你可以试试:
String myResource = IOUtils.toString(this.getClass().getResourceAsStream("yourfile.xml")).replace("\n","");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
234535 次 |
最近记录: |