TransactionImpl implements Transaction { public void commit() throws TransactionException { boolean successful = false; try { this.service.commit(id); successful = true; } catch (RuntimeException ex) { throw ex; } finally { completeTransaction(TransactionCompletionEvent.CompletionEvent.COMMIT, successful); } } @Inject  TransactionImpl(TransactionService service, @Assisted boolean isolatedTransaction); String getId(); void commit(); void rollback(); boolean isCompleted(); void addTransactionCompletionListener(TransactionCompletionListener completionListener); void removeTransactionCompletionListener(TransactionCompletionListener completionListener); @Override boolean equals(Object obj); @Override int hashCode(); boolean isIsolatedTransaction();  }
@Test public void testCommit() { mockery.checking(new Expectations() { { exactly(1).of(service).getNextTransactionId(); will(returnValue("1")); exactly(1).of(service).commit("1"); } }); final Injector injector = initializeInjector(); final Transaction tx = injector.getInstance(Transaction.class); tx.commit(); mockery.assertIsSatisfied(); }
@Test(expected = TransactionException.class) public void testCommitWithException() { mockery.checking(new Expectations() { { exactly(1).of(service).getNextTransactionId(); will(returnValue("1")); exactly(1).of(service).commit("1"); will(throwException(new TransactionException())); } }); final Injector injector = initializeInjector(); final Transaction tx = injector.getInstance(Transaction.class); RuntimeException ex = null; try { tx.commit(); } catch (TransactionException tex) { ex = tex; } mockery.assertIsSatisfied(); if (ex != null) { throw ex; } }