### Question:
IdempotencyBarrier { @Around("@within(io.fency.IdempotentConsumer)") public synchronized Object execute(ProceedingJoinPoint pjp) throws Throwable { Object proceed = null; MessageContext context = idempotentMessageContextService.get(); Optional<Message> message = idempotentMessageService.find(context.getMessageId(), context.getConsumerQueueName()); if (message.isPresent()) { logError(context); } else { log.debug(String .format("Message with id %s for consumer queue %s not found, processing it", context.getMessageId(), context.getConsumerQueueName())); proceed = pjp.proceed(); Message newMessage = new Message(context); idempotentMessageService.save(newMessage); } return proceed; }  @Around("@within(io.fency.IdempotentConsumer)") synchronized Object execute(ProceedingJoinPoint pjp);  }### Answer:
@Test void testExecuteWhenNoMessageFound() throws Throwable { ProceedingJoinPoint mProceedingJoinPoint = mock(ProceedingJoinPoint.class); MessageContext messageContext = IdempotencyTestUtils.createIdempotentContext(); given(mIdempotentMessageContextService.get()).willReturn(messageContext); given(mIdempotentMessageService.find(messageContext.getMessageId(), messageContext.getConsumerQueueName())) .willReturn(Optional.empty()); aspect.execute(mProceedingJoinPoint); verify(mIdempotentMessageService).find(messageContext.getMessageId(), messageContext.getConsumerQueueName()); verify(mProceedingJoinPoint).proceed(); verify(mIdempotentMessageService).save(any(Message.class)); }
@Test void testExecuteWhenMessageFound() throws Throwable { ProceedingJoinPoint mProceedingJoinPoint = mock(ProceedingJoinPoint.class); MessageContext messageContext = IdempotencyTestUtils.createIdempotentContext(); Message message = IdempotencyTestUtils.createIdempotentMessage(); given(mIdempotentMessageContextService.get()).willReturn(messageContext); given(mIdempotentMessageService.find(messageContext.getMessageId(), messageContext.getConsumerQueueName())) .willReturn(Optional.of(message)); aspect.execute(mProceedingJoinPoint); verify(mIdempotentMessageService).find(messageContext.getMessageId(), messageContext.getConsumerQueueName()); verify(mProceedingJoinPoint, never()).proceed(); }