我正在@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
是这个问题吗?我该如何解决?