Skip to content

Commit

Permalink
add function interface to assertItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Alvernaz committed Dec 18, 2023
1 parent 72e36ac commit afdea73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions documentation/src/test/java/guides/TestSubscribersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,37 @@ void failing() {
.assertFailedWith(IOException.class, "Boom");
// </failing>
}

@Test
void uniFunction() {
// <uni-function>
Uni<Integer> uni = Uni.createFrom().item(63);

UniAssertSubscriber<Integer> subscriber = uni
.subscribe().withSubscriber(UniAssertSubscriber.create());

subscriber.assertSubscribed().assertItem(String::valueOf, "63");
// </uni-function>
}

@Test
void uniFunctionWithObjects() {
// <uni-function-with-objects>
class Person {
String name;
int age;
}

Person person = new Person();
person.name = "John";
person.age = 42;
Uni<Person> uni = Uni.createFrom().item(person);

UniAssertSubscriber<Person> subscriber = uni
.subscribe().withSubscriber(UniAssertSubscriber.create());

subscriber.assertSubscribed().assertItem(p -> p.name, person.name);
subscriber.assertSubscribed().assertItem(p -> p.age, person.age);
// </uni-function-with-objects>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -341,6 +342,17 @@ public UniAssertSubscriber<T> 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 <R> the return type of the item
*/
public <R> void assertItem(Function<T, R> function, R expected) {
shouldHaveCompleted(hasCompletedSuccessfully, failure, null);
shouldHaveReceived(function.apply(getItem()), expected);
}

Check warning on line 355 in implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java

View check run for this annotation

Codecov / codecov/patch

implementation/src/main/java/io/smallrye/mutiny/helpers/test/UniAssertSubscriber.java#L353-L355

Added lines #L353 - L355 were not covered by tests
/**
* Assert that the {@link io.smallrye.mutiny.Uni} has failed.
*
Expand Down

0 comments on commit afdea73

Please sign in to comment.