java.lang.IllegalStateException: 临时文件夹尚未创建

java.lang.IllegalStateException: the temporary folder has not yet been created

我正在为我的用例创建一个新的 @Rule,看起来像

public class ActiveDirectoryConfigurationRule extends ExternalResource {

  @Rule
  public TemporaryFolder temporaryFolder = new TemporaryFolder();

  public File addActiveDirectoryConfigurationToFile(ActiveDirectoryConfiguration configuration) throws IOException {
    File file = temporaryFolder.newFile();
    objectMapper.writeValue(file, configuration);
    return file;
  }

  private ObjectMapper registerJdk8ModuleAndGetObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jdk8Module());
    return objectMapper;
  }
}

在我的 Test 中,我将其用作

public class ActiveDirectoryConfigurationStoreTest {

      @Rule
      public ActiveDirectoryConfigurationRule configurationRule = new ActiveDirectoryConfigurationRule();

          @Test
          public void getWhenConfigurationExists() throws Exception {
            ActiveDirectoryConfiguration activeDirectoryConfiguration = //....;
            File configurationToFile = configurationRule.addActiveDirectoryConfigurationToFile(activeDirectoryConfiguration);

            ActiveDirectoryConfigurationStore configurationStore = new ActiveDirectoryConfigurationStore(configurationToFile);
            Optional<ActiveDirectoryConfiguration> mayBeConfiguration = configurationStore.getConfiguration();
            assertTrue(mayBeConfiguration.isPresent());
          }
        }

当我 运行 这个测试时,我得到错误

java.lang.IllegalStateException: the temporary folder has not yet been created

    at org.junit.rules.TemporaryFolder.getRoot(TemporaryFolder.java:145)
    at org.junit.rules.TemporaryFolder.newFile(TemporaryFolder.java:78)
    at com.conf.store.ActiveDirectoryConfigurationRule.addActiveDirectoryConfigurationToFile(ActiveDirectoryConfigurationRule.java:48)
    at com.conf.store.ActiveDirectoryConfigurationStoreTest.getWhenConfigurationExists(ActiveDirectoryConfigurationStoreTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.rules.ExternalResource.evaluate(ExternalResource.java:48)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

似乎在创建自己的 @Rule 时,我无法依赖任何现有的 @Rule

是这个问题吗?我该如何解决?

是的,我认为 JUnit 中没有任何内置功能可以让您 "nest" @Rule 对象像您正在做的那样。

我认为最明显的选择是:

  1. 在您的自定义@Rule 中,在适当的时候调用子@Rule 的各种方法。 (本质上,假装你是 JUnit 库,在其接口上使用 @Rule。)我还没有深入了解它会有多复杂。
  2. 让您的@Rule 扩展TemporaryFolder 而不是ExternalResource,确保在您覆盖的任何方法中调用super()。这让你可以做 "Everything a TemporaryFolder does and then some",这可能不是完美的 OO 理论(因为它不是真正的 TemporaryFolder 类型)但应该按照你正在寻找的方式工作。我在设置需要为我的测试设置特定环境的特定文件夹时使用了这种方法,并且效果很好。
  3. 让您的自定义 @Rule 将 TemporaryFolder 引用作为构造函数参数,然后将其保存在字段中并根据需要使用。这要求您的 @Rule 的所有用户都包含两个 @Rule 对象,但也许可以清楚地表明测试确实需要一个临时文件夹来完成其工作以及您的特定自定义设置。

不支持在规则内部使用 @Rule 声明规则。但是您可以手动 运行 其他规则。

public class ActiveDirectoryConfigurationRule implements TestRule {

  private TemporaryFolder temporaryFolder = new TemporaryFolder();

  @Override
  public Statement apply(Statement base, Description description) {
    Statement testWrappedWithYourCode = new Statement() {
      public void evaluate() {
        before();

        List<Throwable> errors = new ArrayList<Throwable>();
        try {
          base.evaluate();
        } catch (Throwable t) {
          errors.add(t);
        } finally {
          try {
            after();
          } catch (Throwable t) {
            errors.add(t);
          }
        }
        MultipleFailureException.assertEmpty(errors);
      }
    }

    return temporaryFolder.apply(testWrappedWithYourCode, description);
  }

  public File addActiveDirectoryConfigurationToFile(ActiveDirectoryConfiguration configuration) throws IOException {
    File file = temporaryFolder.newFile();
    objectMapper.writeValue(file, configuration);
    return file;
  }

  private ObjectMapper registerJdk8ModuleAndGetObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jdk8Module());
    return objectMapper;
  }
}