Testing TransactionSynchronizationManager.. not sure how to?
I have the following class:
public class Service {
// consutructors and setter for entity manager and platform transaction manager
public void persistOrder() throws Exception {
this.transactionTemplate = new TransactionTemplate(platformTransactionManager);
this.transactionTemplate.setPropagationBehavior(Tr ansactionDefinition.PROPAGATION_REQUIRES_NEW);
try {
final Order order = this.transactionTemplate.execute(new TransactionCallback<Order>() {
@Override
public Order doInTransaction(TransactionStatus status) {
Order order = new Order();
order.setCustomer("Customer");
entityManager.persist(order);
TransactionSynchronizationManager.registerSynchron ization(new TransactionSynchronization(){
public void afterCommit() {
System.out.println("====> AFTER SUCCESSFUL COMMIT TO DB...");
//notifier.notify();
}
public void afterCompletion(int arg0) {
System.out.println("AFTER COMPLETION...");
}
public void beforeCommit(boolean arg0) {
// TODO Auto-generated method stub
}
public void beforeCompletion() {
// TODO Auto-generated method stub
}
public void flush() {
// TODO Auto-generated method stub
}
public void resume() {
// TODO Auto-generated method stub
}
public void suspend() {
// TODO Auto-generated method stub
}
});
throw new IllegalAccessError();
// return order;
}
});
}
catch(Exception ex) {
System.out.println("Exception ex...");
}
}
and the corresponding junit test case... I have mocked the entity manager and transaction manager
context.checking(new Expectations() {
{
allowing(platformTransaction).getTransaction(defin ition);
will((returnValue(status)));
oneOf(em).persist(order);
oneOf(platformTransaction).commit(status);
// check that the notifier mock is called
}
});
this.fakeService.persistOrder(outcome);
context.assertIsSatisfied();
my fakeService class is a subless class of service in which i have called
TransactionSynchronizationManager.initSynchronizat ion();
This allows the test to pass, but i want to be able to mock and check that the call after commit takes place?...
Im kinda stuck on how to check the to test TransactionSynchronizationManager are working as required... none of the methods seem to get called when i mock the commit....
Im thinking maybe i need to have it to test with real transactions and not mocks...??