The order in which JUnit rules are run was changed between Spring 3.0 and 3.1 to match JUnit (see
SPR-7705). As a side effect of this, @Rule callbacks (such as the one developed for SPR-6593) are now executed after the callbacks in TestExecutionListeners. This can be problematic if your rule is running within a transaction as the TransactionalTestExecutionListener will perform the rollback before the rule runs. The opposite may also be true: a transaction might not be started before the rule runs.
Ideally, one could argue that in most circumstances the TransactionalTestExecutionListener should always be the first and last thing to run.
- Consider changing the SpringJUnit4ClassRunner.methodBlock() method to call TestExecutionListener callbacks outside of JUnit @Before calls, something like:
This would ensure that the before and after callbacks of TransactionalTestExecutionListener (and any TestExecutionListener) get called around the @Rule.
Another option would be to introduce a RuleAwareTestExecutionListener interface that adds beforeRules() and afterRules() methods and have TransactionalTestExecutionListener implement this interface as well (or possibly instead of) TestExecutionListener.