JUnit TemporaryFolder不会创建该文件夹

时间:2015-06-04 11:43:53

标签: java junit

在以下代码中,我在java.lang.IllegalStateException: the temporary folder has not yet been created电话中获得了例外getRoot()

我找到了this帖子,根据这个帖子应该有效。另外,我注意到如果我在temporaryFolder.create();调用之前添加getRoot(),代码就可以正常工作。

public class MainTest extends TestCase {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void testMethod() throws Exception {
        File folder = temporaryFolder.getRoot();
    }
}

为什么会这样?

1 个答案:

答案 0 :(得分:5)

您正在混合使用JUnit 3(junit.framework。)和JUnit 4(org.junit。)代码。如果您仅使用JUnit 4,问题应该会消失。

删除extends TestCase(不需要它们,因为您的测试使用@Test注释。)