final XAConnection mockXAConnection = mock(XAConnection.class);
// Handle XAConnection.close(), first time we answer, after that we throw
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
EventRecorder eventRecorder = EventRecorder.getEventRecorder(mockXAConnection);
eventRecorder.addEvent(new XAConnectionCloseEvent(mockXAConnection));
return null;
}
}).doThrow(new SQLException("XAConnection is already closed")).when(mockXAConnection).close();
when(mockXAConnection.getXAResource()).thenReturn(xaResource);