JUnit TemporaryFolder不会创建该文件夹

tra*_*veh 3 java junit

在下面的代码中,我java.lang.IllegalStateException: the temporary folder has not yet been created在getRoot()调用时得到了异常.

我发现这个帖子根据这个代码应该工作.另外,我注意到如果我temporaryFolder.create();在getRoot()调用之前添加,代码工作正常.

public class MainTest extends TestCase {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void testMethod() throws Exception {
        File folder = temporaryFolder.getRoot();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

mp9*_*1de 5

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

删除extends TestCase(它们很简单,不需要,因为您的测试都带有注释@Test).