EDIT: With the recently released Junit 4.10, you can use RuleChain
to chain rules correctly (see at the end).
You could introduce another private field without the @Rule annotation, then you can reorder your code as you wish:
public class FolderRuleOrderingTest {
private TemporaryFolder privateFolder = new TemporaryFolder();
@Rule
public MyNumberServer server = new MyNumberServer(privateFolder);
@Rule
public TemporaryFolder folder = privateFolder;
@Test
public void testMyNumberServer() throws IOException {
server.storeNumber(10);
assertEquals(10, server.getNumber());
}
...
}
The cleanest solution is to have a compound rule, but the above should work.
EDIT: With the recently released Junit 4.10, you can use RuleChain
to chain rules correctly:
public static class UseRuleChain {
@Rule
public TestRule chain = RuleChain
.outerRule(new LoggingRule("outer rule"))
.around(new LoggingRule("middle rule"))
.around(new LoggingRule("inner rule"));
@Test
public void example() {
assertTrue(true);
}
}
writes the log
starting outer rule
starting middle rule
starting inner rule
finished inner rule
finished middle rule
finished outer rule