我正在为我的用例创建一个新的@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
这就是问题所在吗?我该如何解决?
是的,我认为JUnit中没有任何内置的东西可以让你像现在这样"嵌套"@Rule对象。
我认为最明显的选择是:
- 在自定义@Rule中,在适当的时间调用子@Rule上的各种方法。(本质上,假设你是JUnit库,每个接口使用@Rule。)我还没有深入了解细节,看看它会有多复杂
- 让@Rule扩展
TemporaryFolder
而不是ExternalResource
,确保在要重写的任何方法中调用super()
。这可以让你做"TemporaryFolder
所做的一切,然后再做一些",这可能不是完美的OO理论(因为它实际上不是TemporaryFolder的一种类型),但应该按照你想要的方式工作。我在设置一个特定的文件夹时使用了这种方法,该文件夹需要为我的测试设置一个特殊的环境,而且效果相当好 - 让您的自定义@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;
}
}