Testing the Deadline

Testing behaviour related to deadlines is easy by using either the SagaTestFixture or the AggregateTestFixture. This makes it possible to simulate passing of time, and checking the behaviour.

class ExpiringGiftCardTest {

    private static final String CARD_ID = UUID.randomUUID().toString();
    private static final int DAYS = 30;
    private static final int AMOUNT = 1377;

    private AggregateTestFixture<ExpiringGiftCard> testFixture;

    @BeforeEach
    public void setUp() {
        testFixture = getTestFixture();
    }

    @Test
    void testCardExpiredAfterThirtyOneDays() {
        testFixture.givenNoPriorActivity()
                   .andGivenCommands(new IssueExpiringCardCommand(CARD_ID, DAYS, AMOUNT))
                   .whenTimeElapses(Duration.ofDays(31L))  (1)
                   .expectSuccessfulHandlerExecution()
                   .expectEvents(new ExpiringCardExpiredEvent(CARD_ID))
                   .expectTriggeredDeadlinesWithName("EXPIRED_GIFT_CARD");
    }

    private AggregateTestFixture<ExpiringGiftCard> getTestFixture() {
        return new AggregateTestFixture<>(ExpiringGiftCard.class);
    }
}
1 By elapsing time by 31 days, the deadline should trigger and publish an ExpiringCardExpiredEvent.