use of org.springframework.transaction.ReactiveTransaction in project spring-framework by spring-projects.
the class AbstractReactiveTransactionAspectTests method doTestRollbackOnException.
/**
* Check that the when exception thrown by the target can produce the
* desired behavior with the appropriate transaction attribute.
* @param ex exception to be thrown by the target
* @param shouldRollback whether this should cause a transaction rollback
*/
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
@Override
public boolean rollbackOn(Throwable t) {
assertThat(t).isSameAs(ex);
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
ReactiveTransaction status = mock(ReactiveTransaction.class);
ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
// Gets additional call(s) from TransactionControl
given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
if (shouldRollback) {
given(rtm.rollback(status)).willReturn(Mono.error(tex));
} else {
given(rtm.commit(status)).willReturn(Mono.error(tex));
}
} else {
given(rtm.commit(status)).willReturn(Mono.empty());
given(rtm.rollback(status)).willReturn(Mono.empty());
}
DefaultTestBean tb = new DefaultTestBean();
TestBean itb = (TestBean) advised(tb, rtm, tas);
itb.exceptional(ex).as(StepVerifier::create).expectErrorSatisfies(actual -> {
if (rollbackException) {
assertThat(actual).isEqualTo(tex);
} else {
assertThat(actual).isEqualTo(ex);
}
}).verify();
if (!rollbackException) {
if (shouldRollback) {
verify(rtm).rollback(status);
} else {
verify(rtm).commit(status);
}
}
}
Aggregations