### Question:
RequiredTransactionInterceptor extends AbstractTransactionAttributeInterceptor { @AroundInvoke public Object applyTransaction(InvocationContext ic) throws Exception { boolean transactionStarted = beginTransactionIfNeeded(); try { Object result = ic.proceed(); return result; } catch (Exception e) { rollbackTransactionIfNeeded(transactionStarted, e); throw e; } finally { handleTransactionEnd(transactionStarted); } }  @AroundInvoke Object applyTransaction(InvocationContext ic);  }### Answer:
@Test(expected = ApplicationException.class) public void beginAndCommitTransaction() throws Exception { transactionFactory.expectNoTransaction(); transactionFactory.expectBeginTransaction(); transactionFactory.expectCommitTransaction(); transactionFactory.replay(); try { InvocationContext ic = createNiceMock(InvocationContext.class); expect(ic.proceed()).andThrow(new ApplicationException()); replay(ic); interceptor.applyTransaction(ic); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) { throw (Error) e.getTargetException(); } else { throw (Exception) e.getTargetException(); } } finally { transactionFactory.verify(); } }
@Test(expected = ApplicationRuntimeException.class) public void beginAndRollbackTransaction() throws Exception { transactionFactory.expectNoTransaction(); transactionFactory.expectBeginTransaction(); transactionFactory.expectSetRollbackOnly(); transactionFactory.expectRollbackTransaction(); transactionFactory.replay(); try { InvocationContext ic = createNiceMock(InvocationContext.class); expect(ic.proceed()).andThrow(new ApplicationRuntimeException()); replay(ic); interceptor.applyTransaction(ic); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof Error) { throw (Error) e.getTargetException(); } else { throw (Exception) e.getTargetException(); } } finally { transactionFactory.verify(); } }
@Test(expected = ApplicationException.class) public void activeTransaction() throws Exception { transactionFactory.expectActiveTransaction(); transactionFactory.replay(); try { InvocationContext ic = createNiceMock(InvocationContext.class); expect(ic.proceed()).andThrow(new ApplicationException()); replay(ic); interceptor.applyTransaction(ic); } finally { transactionFactory.verify(); } }
@Test(expected = ApplicationRuntimeException.class) public void activeTransactionSetRollbackOnly() throws Exception { transactionFactory.expectActiveTransaction(); transactionFactory.expectSetRollbackOnly(); transactionFactory.replay(); try { InvocationContext ic = createNiceMock(InvocationContext.class); expect(ic.proceed()).andThrow(new ApplicationRuntimeException()); replay(ic); interceptor.applyTransaction(ic); } finally { transactionFactory.verify(); } }