简单的方法将testing文件导入JUnit

有人可以build议一个简单的方法来获得一个文件的引用作为一个String / InputStream /文件/ etctypes的对象在junittesting类? 很明显,我可以粘贴文件(在这种情况下的XML)作为一个巨大的string或阅读它作为一个文件,但是有一个特定于Junit这样的捷径吗?

public class MyTestClass{ @Resource(path="something.xml") File myTestFile; @Test public void toSomeTest(){ ... } } 

你可以试试@Rule注解。 以下是文档中的示例:

 public static class UsesExternalResource { Server myServer = new Server(); @Rule public ExternalResource resource = new ExternalResource() { @Override protected void before() throws Throwable { myServer.connect(); }; @Override protected void after() { myServer.disconnect(); }; }; @Test public void testFoo() { new Client().run(myServer); } } 

您只需创build扩展ExternalResource FileResource类。

完整的例子

 import static org.junit.Assert.*; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExternalResource; public class TestSomething { @Rule public ResourceFile res = new ResourceFile("/res.txt"); @Test public void test() throws Exception { assertTrue(res.getContent().length() > 0); assertTrue(res.getFile().exists()); } } 

 import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import org.junit.rules.ExternalResource; public class ResourceFile extends ExternalResource { String res; File file = null; InputStream stream; public ResourceFile(String res) { this.res = res; } public File getFile() throws IOException { if (file == null) { createFile(); } return file; } public InputStream getInputStream() { return stream; } public InputStream createInputStream() { return getClass().getResourceAsStream(res); } public String getContent() throws IOException { return getContent("utf-8"); } public String getContent(String charSet) throws IOException { InputStreamReader reader = new InputStreamReader(createInputStream(), Charset.forName(charSet)); char[] tmp = new char[4096]; StringBuilder b = new StringBuilder(); try { while (true) { int len = reader.read(tmp); if (len < 0) { break; } b.append(tmp, 0, len); } reader.close(); } finally { reader.close(); } return b.toString(); } @Override protected void before() throws Throwable { super.before(); stream = getClass().getResourceAsStream(res); } @Override protected void after() { try { stream.close(); } catch (IOException e) { // ignore } if (file != null) { file.delete(); } super.after(); } private void createFile() throws IOException { file = new File(".",res); InputStream stream = getClass().getResourceAsStream(res); try { file.createNewFile(); FileOutputStream ostream = null; try { ostream = new FileOutputStream(file); byte[] buffer = new byte[4096]; while (true) { int len = stream.read(buffer); if (len < 0) { break; } ostream.write(buffer, 0, len); } } finally { if (ostream != null) { ostream.close(); } } } finally { stream.close(); } } } 

如果您需要实际获取File对象,则可以执行以下操作:

 URL url = this.getClass().getResource("/test.wsdl"); File testWsdl = new File(url.getFile()); 

正如本博文中所述, 这有利于跨平台工作。

我知道你说过你不想手动读取文件,但是这很容易

 public class FooTest { private BufferedReader in = null; @Before public void setup() throws IOException { in = new BufferedReader( new InputStreamReader(getClass().getResourceAsStream("/data.txt"))); } @After public void teardown() throws IOException { if (in != null) { in.close(); } in = null; } @Test public void testFoo() throws IOException { String line = in.readLine(); assertThat(line, notNullValue()); } } 

所有你需要做的是确保有问题的文件在类path中。 如果您使用的是Maven,只需将该文件放在src / test / resources中,Maven将在运行testing时将其包含在类path中。 如果你需要做很多这样的事情,你可以把打开文件的代码放在一个超类中,然后让你的testinginheritance它。

你可以尝试做:

 String myResource = IOUtils.toString(this.getClass().getResourceAsStream("yourfile.xml")).replace("\n",""); 

如果你想用一行代码加载一个testing资源文件作为一个string,并且没有任何额外的依赖关系,这就实现了这个function:

 public String loadResourceAsString(String fileName) throws IOException { Scanner scanner = new Scanner(getClass().getClassLoader().getResourceAsStream(fileName)); String contents = scanner.useDelimiter("\\A").next(); scanner.close(); return contents; } 

“\\ A”匹配input的开始,并且只有一个。 所以这将parsing整个文件内容并以stringforms返回。 最重要的是,它不需要任何第三方库(如IOUTils)。