3

我正在@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

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

4

2 回答 2

3

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

我认为最明显的选择是:

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

不支持使用@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;
  }
}
于 2016-07-22T22:00:44.580 回答