diff --git a/documentation/src/test/java/guides/TestSubscribersTest.java b/documentation/src/test/java/guides/TestSubscribersTest.java index 22f585736..b755ec7e5 100644 --- a/documentation/src/test/java/guides/TestSubscribersTest.java +++ b/documentation/src/test/java/guides/TestSubscribersTest.java @@ -51,4 +51,37 @@ void failing() { .assertFailedWith(IOException.class, "Boom"); // } + + @Test + void uniFunction() { + // + Uni uni = Uni.createFrom().item(63); + + UniAssertSubscriber subscriber = uni + .subscribe().withSubscriber(UniAssertSubscriber.create()); + + subscriber.assertSubscribed().assertItem(String::valueOf, "63"); + // + } + + @Test + void uniFunctionWithObjects() { + // + class Person { + String name; + int age; + } + + Person person = new Person(); + person.name = "John"; + person.age = 42; + Uni uni = Uni.createFrom().item(person); + + UniAssertSubscriber subscriber = uni + .subscribe().withSubscriber(UniAssertSubscriber.create()); + + subscriber.assertSubscribed().assertItem(p -> p.name, person.name); + subscriber.assertSubscribed().assertItem(p -> p.age, person.age); + // + } } diff --git a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java index e1de1d646..b9be19086 100644 --- a/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java +++ b/implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.concurrent.*; import java.util.function.Consumer; +import java.util.function.Function; import io.smallrye.mutiny.Context; import io.smallrye.mutiny.subscription.UniSubscriber; @@ -341,6 +342,17 @@ public UniAssertSubscriber assertItem(T expected) { return this; } + /** + * Assert that the {@link io.smallrye.mutiny.Uni} has received an item. + * @param function the function to apply the item to assert on + * @param expected the expected item + * @param the return type of the item + */ + public void assertItem(Function function, R expected) { + shouldHaveCompleted(hasCompletedSuccessfully, failure, null); + shouldHaveReceived(function.apply(getItem()), expected); + } + /** * Assert that the {@link io.smallrye.mutiny.Uni} has failed. *