我收到错误"尚未创建临时文件夹",它来自IllegalStateException
方法抛出的TemporaryFolder.getRoot()
。看起来它没有初始化,但是我的研究表明,当在setUp() - 方法中初始化临时文件夹时通常会出现这种情况。但是像我一样使用它@Rule
应该在我看来是有用的。有什么想法吗?
测试类
public class FileReaderTest extends TestCase {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
public FileReaderTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCSVWriterAndReader() throws Exception{
testWriterAndReader(new CSVFileWriter(), new CSVFileReader());
}
private void testWriterAndReader(FileWriteService writer, FileReader reader) throws Exception {
folder = new TemporaryFolder();
File tempFile = folder.newFile("test.csv");
DataSet initializedData = createMockData();
writer.writeDataSetToFile(initializedData, tempFile.getPath());
DataSet readData = reader.getDataFromFile(new FileInputStream(tempFile));
assertEquals(initializedData, readData);
}
}
答案 0 :(得分:3)
您正在使用不支持规则的JUnit 3测试。你必须使用JUnit 4测试。因此
extends TestCase
。@Test
注释添加到所有测试方法(以test开头的公共方法。)应该进行迁移。之后你必须删除
行folder = new TemporaryFolder();
来自testWriterAndReader
。
有关迁移的详细信息:Best way to automagically migrate tests from JUnit 3 to JUnit 4?