GH-185 - Additional simplification of delayed event verification on Scenarios.

This commit is contained in:
Oliver Drotbohm
2023-04-25 14:34:23 +02:00
parent 3a923519f2
commit bb800ba046
4 changed files with 110 additions and 8 deletions

View File

@@ -23,6 +23,7 @@ import lombok.RequiredArgsConstructor;
import java.lang.Thread.UncaughtExceptionHandler;
import java.time.Duration;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -34,6 +35,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opentest4j.AssertionFailedError;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.modulith.test.PublishedEventsAssert.PublishedEventAssert;
@@ -396,6 +398,41 @@ class ScenarioUnitTests {
verify(consumer).accept(any());
}
@Test // GH-185
void attachedEventConstraintsAreVerified() {
var runnable = mock(Runnable.class);
var events = new DefaultPublishedEvents(List.of(new SomeEvent("payload")));
var publishedEvents = new DefaultAssertablePublishedEvents(events);
assertThatNoException().isThrownBy(() -> {
new Scenario(tx, publisher, publishedEvents)
.stimulate(runnable)
.andWaitForStateChange(() -> true)
.andExpect(SomeEvent.class)
.matching(it -> it != null)
.toArrive();
});
verify(runnable).run();
}
@Test // GH-185
void failsAttachedEventConstraintsIfNoEventsPublished() {
var runnable = mock(Runnable.class);
assertThatExceptionOfType(AssertionFailedError.class)
.isThrownBy(() -> new Scenario(tx, publisher, new DefaultAssertablePublishedEvents())
.stimulate(runnable)
.andWaitForStateChange(() -> true)
.andExpect(SomeEvent.class)
.matching(it -> it != null)
.toArrive());
verify(runnable).run();
}
private Fixture givenAScenario(Consumer<Scenario> consumer) {
return new Fixture(consumer, DELAY, null, new DefaultAssertablePublishedEvents());
}