|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package com.baeldung.ddd.event; |
| 5 | + |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.Mockito.times; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 16 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 17 | + |
| 18 | +@SpringJUnitConfig |
| 19 | +@SpringBootTest |
| 20 | +class Aggregate2EventsIntegrationTest { |
| 21 | + @MockBean |
| 22 | + private TestEventHandler eventHandler; |
| 23 | + @Autowired |
| 24 | + private Aggregate2Repository repository; |
| 25 | + |
| 26 | + // @formatter:off |
| 27 | + @DisplayName("given aggregate with @AfterDomainEventPublication," |
| 28 | + + " when do domain operation and save twice," |
| 29 | + + " then an event is published only for the first time") |
| 30 | + // @formatter:on |
| 31 | + @Test |
| 32 | + void afterDomainEvents() { |
| 33 | + // given |
| 34 | + Aggregate2 aggregate = new Aggregate2(); |
| 35 | + |
| 36 | + // when |
| 37 | + aggregate.domainOperation(); |
| 38 | + repository.save(aggregate); |
| 39 | + repository.save(aggregate); |
| 40 | + |
| 41 | + // then |
| 42 | + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); |
| 43 | + } |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void beforeEach() { |
| 47 | + repository.deleteAll(); |
| 48 | + } |
| 49 | + |
| 50 | + // @formatter:off |
| 51 | + @DisplayName("given aggregate with @DomainEvents," |
| 52 | + + " when do domain operation and save," |
| 53 | + + " then an event is published") |
| 54 | + // @formatter:on |
| 55 | + @Test |
| 56 | + void domainEvents() { |
| 57 | + // given |
| 58 | + Aggregate2 aggregate = new Aggregate2(); |
| 59 | + |
| 60 | + // when |
| 61 | + aggregate.domainOperation(); |
| 62 | + repository.save(aggregate); |
| 63 | + |
| 64 | + // then |
| 65 | + verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class)); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments