I was wondering if this is possible. This is my first question so bear with me if I do not exactly follow protocol. I am able to rollback my transactions with a transaction template. But now what I want to do is save a status in the method labeled @before and then roll it back in the @after method.
For example this is what I had before:
_transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus)
{
Object transactionStatusSavepoint = transactionStatus.createSavepoint();
//do some work
transactionStatus.rollbackToSavepoint(transactionStatusSavepoint);
}
});
This all works well and good. Now I would like to do this:
@Before
public void beforeTest() {
_template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
_transactionStatusSavepoint = _transactionStatus.createSavepoint();
}
});
@Test
public void foo(){//do some stuff}
@After
public void afterTest() {
_transactionStatus.rollbackToSavepoint(_transactionStatusSavepoint);
}
Please let me know if you do not understand or would like to elaborate more.
Thanks in advance!
The transaction status will not be valid once doInTransactionWithoutResult is complete, so you cannot use it in your afterTest method.
It looks like you are using spring for your test. Spring comes with test runner that can rollback a transaction when your tests complete.
Spring testing