### Question:
JPAContext { public void closeTxn(boolean rollback) { if (isTxnOpen()) { if (rollback || readOnly || entityManager.getTransaction().getRollbackOnly()) { entityManager.getTransaction().rollback(); } else { entityManager.getTransaction().commit(); } } } JPAContext(JPAConfig jpaConfig, boolean readOnly); EntityManager getEntityManager(); void close(); void beginTxn(); void closeTxn(boolean rollback); boolean isTxnOpen(); boolean isReadOnly();  }### Answer:
@Test public void shouldRollback() { EntityTransaction txn = mockTransaction(context, true); context.closeTxn(true); verify(txn).rollback(); }
@Test public void shouldRollbackIfTransactionIsMarkedForRollback() { EntityTransaction txn = mockTransaction(context, true); when(txn.getRollbackOnly()).thenReturn(true); context.closeTxn(true); verify(txn).rollback(); }
@Test public void shouldRollbackIfReadOnlyisSet(){ JPAContext context = new JPAContext(config, true); EntityTransaction txn = mockTransaction(context, true); when(txn.getRollbackOnly()).thenReturn(false); context.closeTxn(true); verify(txn).rollback(); }
@Test public void shouldCommit() { EntityTransaction txn = mockTransaction(context, true); context.closeTxn(false); verify(txn).commit(); }
@Test public void shouldNotCommitIfTxnIsNotOpen() { EntityTransaction txn = mockTransaction(context, false); context.closeTxn(false); verify(txn, never()).commit(); }
@Test public void shouldNotRollbackIfTxnIsNotOpen() { EntityTransaction txn = mockTransaction(context, false); context.closeTxn(false); verify(txn, never()).rollback(); }