I am the original answerer of How to Re-run failed JUnit tests immediately?
If I understand correctly, the problem that you are having is due to the @Before
being executed before the code in the RetryRule
, and the @After
being executed afterwards.
So your current behaviour is something like:
@Before
@Retry
test code
@Retry
@After
But you can implement your @Before
and @After
as a rule - there is a rule ExternalResource which does exactly that. You would implement @Before
and @After
as a rule:
@Rule public ExternalResource beforeAfter = new ExternalResource() {
public void before() {
// code that was in @Before
}
public void after() {
// code that was in @After
}
}
Then you don't need the @Before
and @After
. You can then chain these rules using RuleChain. This forces an order of execution to your rules:
@Rule public RuleChain chain= RuleChain
.outerRule(new LoggingRule("outer rule")
.around(new LoggingRule("middle rule")
.around(new LoggingRule("inner rule");
so your final solution would be something like:
private ExternalResource beforeAfter = ...
private RetryRule retry = ...
@Rule public RuleChain chain = RuleChain
.outerRule(retry)
.around(beforeAfter);
Note that if you are using RuleChain
, you no longer need the @Rule
annotation on the ExternalResource
and RetryRule
, but you do on the RuleChain
.