【问题标题】:java.lang.IllegalStateException: the temporary folder has not yet been createdjava.lang.IllegalStateException:尚未创建临时文件夹
【发布时间】:2016-07-21 16:19:27
【问题描述】:

我正在为我的用例创建一个新的@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$1.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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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

是这个问题吗?以及如何解决?

【问题讨论】:

    标签: java junit junit-rule


    【解决方案1】:

    是的,我认为 JUnit 中没有内置任何东西可以让您像您正在做的那样“嵌套”@Rule 对象。

    我认为最明显的选择是:

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

    【讨论】:

    • 选项#3 听起来是最好的设计。但是,我认为这很难实现:您要保证 TemporaryFolder 已被首先实例化,因此您可以将其传递给您的自定义 Rule。为此,JUnit 有一个 RuleChain (hascode.com/2012/02/ordering-your-junit-rules-using-a-rulechain)。在这里您可以定义顺序,但不能将一个实例传递给另一个实例的构造函数。一个真正的困境!我认为选项#1 是实用的:覆盖apply(...),将Statement 包裹在TemporaryFolder 生成的那个周围,并在您的子规则之后执行您的逻辑。
    • 我也喜欢方法#3,我会读到RuleChain
    【解决方案2】:

    不支持在规则内声明带有@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;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多