I have transactional test cases:
Works fine in the "good" case. The UserService participates in the transaction declared on test1() (via the class level annotation @Transactional) and the data is being committed at the end of the test1 method.Code:@ContextConfiguration(locations={"/applicationContext.xml"}) @TestExecutionListeners(TransactionalTestExecutionListener.class) @TransactionConfiguration(defaultRollback=false) @Transactional public class TestCase extends AbstractTestNGSpringContextTests { @Autowired private UserService userService; @Test public void test1() { ... this.userService.doSomethingTransactional(...); ... this.userService.doSomethingElseTransactional(...); } } @Service public class UserService { @Transactional(readOnly = false) public void doSomethingTransactional(...) { ... } }
Now in a "bad" case, either when an internal exception happens or some Assert... statement fails, I want the transaction being rolled back. The TransactionalTestExecutionListener, however, either rolls back all transactions or commits them all, depending on the configuration. It does not take exceptions into account.
When changing the code of the TransactionalTestExecutionListener like so:
it does pretty much what I wanted. Unfortunately, I need to copy the whole code of TransactionalTestExecutionListener and patch it in my own class, as there are no suitable (accessible) override points.Code:public class TransactionalTestExecutionListener extends AbstractTestExecutionListener { ... protected final boolean isRollback(TestContext testContext) throws Exception { ... if (testContext.getTestException() != null) rollback = true; return rollback; } ... }
Now my questions:
Did I miss something fundamental?
Is it not a common use case to have a commit for successful test cases and a rollback for exceptions?
Is there another way to get the desired behavior without patching framework classes?
Regards,
Flo


Reply With Quote