为什么我会看到 "the temporary folder has not yet been created" 消息?

Why do I see "the temporary folder has not yet been created" message?

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

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

    public class MainTest extends TestCase {
    
        @Rule
        public TemporaryFolder temporaryFolder = new TemporaryFolder();
    
        @Test
        public void testMethod() throws Exception {
            File folder = temporaryFolder.getRoot();
        }
    }

为什么会这样?

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

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