gpt4 book ai didi

java - 将测试文件导入 JUnit 的简单方法

转载 作者:IT老高 更新时间:2023-10-28 11:49:08 25 4
gpt4 key购买 nike

有人可以建议一种简单的方法来获取对文件的引用作为 junit 测试类中的 String/InputStream/File/etc 类型对象吗?显然,我可以将文件(在这种情况下为 xml)粘贴为一个巨大的字符串或将其作为文件读入,但是有这样特定于 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);
}
}

您只需要创建 FileResource 类扩展 ExternalResource

完整示例

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();
}
}

}

关于java - 将测试文件导入 JUnit 的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com