Pulling out into a separate discussion from: https://community.jboss.org/message/777483
Currently the Arquillian Transaction Extension (ATE) starts the transaction before invoking @Before and then completes it after @After is invoked. This has the following issues/implications:
- The same transaction is used for the @Begin, @Test and @After methods.
- You can't use the @After method to undo any work. If you update a resource in the test and then negate the update in @After, you would leave nothing to commit when the transaction commits.
- You can't access any data that is only available after the transaction commits. For example, you may need to consume a message, produced by the test, in order to tidy up from the test. This message will not be available until the after the transaction has committed, and thus not available in the @After.
- You can't Assert the environment after the transaction commits.
From the above discussion with Aslak, I think this is the solution that could solve the above issues:
Have the transaction just span the @Test method by default. Allow @Transactional to be added to @Before and @After, but use a separate transaction for each, which completes at the end of the method. We could also add an API to allow the transaction to achieve the current behavior of spanning @Before, @Test and @After. This would need some thought as I can think of a few issues that would need addressing.
Is @After the right place to run assertions? Ideally you would run assertions in the @Test method, but this isn't possible if you want to assert something after the transaction completes. Assertions in @After would need to be general enough to hold for all @Test methods in the class. I think this still leaves the issue open for test specific assertions.
Here's an example of where I use @Test specific assertions after a transaction completes: https://github.com/jbosstm/narayana/blob/master/txframework/src/test/java/org/jboss/narayana/txframework/functional/BACoordinatorCompletionTest.java. In this test I have a service that participates in a transaction. The service logs what protocol messages it received when the termination protocol was invoked. After the transaction has completed, I assert that the right protocol messages were received and in the right order.