2016-07-21 6 views
1

を作成していない私はjava.lang.IllegalStateException:一時フォルダはまだ

私は

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()); 
      } 
     } 
としてそれを使用する私の Test
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; 
    } 
} 

のように見える私のユースケースのための新しい@Ruleを作成しています

私はこのテストを実行すると、私は

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

それは問題ですか?どのように解決するのですか?

答えて

2

はい、私はあなたがあなたのような「巣」@RuleオブジェクトがやっているようにするのJUnitに内蔵されたものがありますとは思いません。適切な時期にあなたの子供@Rule上のさまざまなメソッドを呼び出して、

  1. カスタム@Ruleで:

    私が最も明白な選択肢があることだと思います。 (本質的には、JUnitライブラリであり、インタフェースごとに@Ruleを使用しているようなふりをしている)。どの程度複雑であるかについては詳細を掘り下げていない。

  2. @RuleをExternalResourceではなくTemporaryFolderに延長してください。オーバーライドしている方法のいずれかでsuper()を必ず呼び出してください。これは、あなたが "TemporaryFolderが何であってもいくつか"を行うことを可能にします。これはおそらく完全なOO理論ではありませんが(実際のタイプのTemporaryFolderではありません)、あなたが探している方法で動作するはずです。私はこのアプローチを、テストのために特定の環境でセットアップする必要のある特定のフォルダを設定するときに使用しました。これはかなりうまく機能しました。
  3. カスタム@RuleにコンストラクタパラメータとしてTemporaryFolder参照を取り込み、それをフィールドに保存して必要に応じて使用します。これには@Ruleのすべてのユーザーに両方の@Ruleオブジェクトが含まれている必要がありますが、おそらくテストで実際に作業するための一時フォルダと特定のカスタムセットアップが必要であることが明らかになります。
+1

オプション#3は、最高のデザインのように聞こえます。しかし、私は実装するのが難しいと思う:あなたは 'TemporaryFolder'が最初にインスタンス化されていることを保証したいので、カスタム' Rule'に渡すことができます。その目的のために、JUnitには 'RuleChain'(http://www.hascode.com/2012/02/ordering-your-junit-rules-using-a-rulechain/)があります。ここでは、順序を定義できますが、一方のインスタンスを他方のコンストラクターに渡すことはできません。本当のジレンマ! オプション#1は実用的です: 'apply(...)'をオーバーライドし、 'TemporaryFolder'で生成された' Statement'をラップして、子ルールの後にロジックを実行してください。 –

+0

私はアプローチ#3も好きです。私は 'RuleChain'について読む – daydreamer

0

ルール内に@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; 
    } 
} 
関連する問題