### Question:
SagaEndAspect { @Around("execution(@org.apache.servicecomb.pack.omega.context.annotations.SagaEnded * *(..)) && @annotation(sagaEnd)") Object advise(ProceedingJoinPoint joinPoint, SagaEnd sagaEnd) throws Throwable { Method method = ((MethodSignature) joinPoint.getSignature()).getMethod(); try { Object result = joinPoint.proceed(); sendSagaEndedEvent(); return result; } catch (Throwable throwable) { if (!(throwable instanceof OmegaException)) { LOG.error("Transaction {} failed.", context.globalTxId()); sendSagaAbortedEvent(method.toString(), throwable); } throw throwable; } finally { context.clear(); } } SagaEndAspect(SagaMessageSender sender, OmegaContext context);   }### Answer:
@Test public void sagaEndWithoutError() throws Throwable { aspect = new SagaEndAspect(sender, omegaContext); aspect.advise(joinPoint, sagaEnd); assertThat(messages.size(), is(1)); TxEvent endedEvent = messages.get(0); assertThat(endedEvent.globalTxId(), is(globalTxId)); assertThat(endedEvent.localTxId(), is(globalTxId)); assertThat(endedEvent.parentTxId(), is(nullValue())); assertThat(endedEvent.type(), is(EventType.SagaEndedEvent)); verify(omegaContext).clear(); }
@Test public void sagaEndWithErrors() throws Throwable { aspect = new SagaEndAspect(sender, omegaContext); RuntimeException oops = new RuntimeException("oops"); when(joinPoint.proceed()).thenThrow(oops); try { aspect.advise(joinPoint, sagaEnd); expectFailing(RuntimeException.class); } catch (RuntimeException e) { assertThat(e, is(oops)); } assertThat(messages.size(), is(1)); TxEvent event = messages.get(0); assertThat(event.globalTxId(), is(globalTxId)); assertThat(event.localTxId(), is(globalTxId)); assertThat(event.parentTxId(), is(nullValue())); assertThat(event.type(), is(EventType.SagaAbortedEvent)); verify(omegaContext).clear(); }