Skip to content

Commit 777a5c9

Browse files
committed
GH-1131 - Non-empty collections do not indicate scenario completion anymore.
Similarly to our treatment of Optional, we now only consider non-empty collections a trigger for a state change based Scenario conclusion.
1 parent fa946c1 commit 777a5c9

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

spring-modulith-test/src/main/java/org/springframework/modulith/test/Scenario.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.time.Duration;
21+
import java.util.Collection;
2122
import java.util.Optional;
2223
import java.util.concurrent.Callable;
2324
import java.util.function.BiConsumer;
@@ -34,8 +35,6 @@
3435
import org.springframework.modulith.core.util.CheckReturnValue;
3536
import org.springframework.modulith.test.PublishedEvents.TypedPublishedEvents;
3637
import org.springframework.modulith.test.PublishedEventsAssert.PublishedEventAssert;
37-
import org.springframework.modulith.test.Scenario.When.EventResult;
38-
import org.springframework.modulith.test.Scenario.When.StateChangeResult;
3938
import org.springframework.transaction.TransactionDefinition;
4039
import org.springframework.transaction.support.DefaultTransactionDefinition;
4140
import org.springframework.transaction.support.TransactionOperations;
@@ -63,6 +62,10 @@ public class Scenario {
6362
return o.isPresent();
6463
}
6564

65+
if (it instanceof Collection<?> c) {
66+
return !c.isEmpty();
67+
}
68+
6669
if (it instanceof Boolean b) {
6770
return b;
6871
}

spring-modulith-test/src/test/java/org/springframework/modulith/test/ScenarioUnitTests.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import java.lang.Thread.UncaughtExceptionHandler;
2525
import java.time.Duration;
26+
import java.util.Collections;
2627
import java.util.List;
28+
import java.util.Optional;
2729
import java.util.function.BiConsumer;
2830
import java.util.function.Consumer;
2931
import java.util.function.Function;
@@ -433,6 +435,57 @@ void failsAttachedEventConstraintsIfNoEventsPublished() {
433435
verify(runnable).run();
434436
}
435437

438+
@Test // GH-1131
439+
void concludesForNonEmptyOptional() {
440+
441+
givenAScenario(it -> it.publish(new Object())
442+
.andWaitForStateChange(delayed(Optional.of("value"))))
443+
.expectSuccess();
444+
}
445+
446+
@Test // GH-1131
447+
void doesNotConcludeForEmptyOptional() {
448+
449+
givenAScenario(it -> it.publish(new Object())
450+
.andWaitAtMost(Duration.ofMillis(500))
451+
.andWaitForStateChange(delayed(Optional.empty())))
452+
.expectFailure();
453+
}
454+
455+
@Test // GH-1131
456+
void concludesForNonEmptyCollection() {
457+
458+
givenAScenario(it -> it.publish(new Object())
459+
.andWaitForStateChange(delayed(List.of("value"))))
460+
.expectSuccess();
461+
}
462+
463+
@Test // GH-1131
464+
void doesNotConcludeForEmptyCollection() {
465+
466+
givenAScenario(it -> it.publish(new Object())
467+
.andWaitAtMost(Duration.ofMillis(500))
468+
.andWaitForStateChange(delayed(Collections.emptyList())))
469+
.expectFailure();
470+
}
471+
472+
@Test // GH-1131
473+
void concludesForNonNull() {
474+
475+
givenAScenario(it -> it.publish(new Object())
476+
.andWaitForStateChange(delayed(new Object())))
477+
.expectSuccess();
478+
}
479+
480+
@Test // GH-1131
481+
void doesNotConcludeForNull() {
482+
483+
givenAScenario(it -> it.publish(new Object())
484+
.andWaitAtMost(Duration.ofMillis(500))
485+
.andWaitForStateChange(delayed(null)))
486+
.expectFailure();
487+
}
488+
436489
private Fixture givenAScenario(Consumer<Scenario> consumer) {
437490
return new Fixture(consumer, DELAY, null, new DefaultAssertablePublishedEvents());
438491
}

0 commit comments

Comments
 (0)