Is it correct to test assertions on a Uni running subscription on another thread in Mutiny?

Viewed 23

I am using Mutiny outside of Quarkus in a personal project.

I was writing some test cases on some Unis and begin to notice that some assertions that should be passing, weren't reproducing the expected behavior.

A silly example:

@Test
void test_retrieving_value_async() {
    Uni<String> uni = Uni.createFrom().item("Hello");
    var pool = ForkJoinPool.commonPool();
    var test = uni
            .runSubscriptionOn(pool)    
            .subscribe().withSubscriber(UniAssertSubscriber.create());
    test.assertCompleted().assertItem("Hello");
}

logs:

Expected a completion event, but didn't received it.
java.lang.AssertionError: 
Expected a completion event, but didn't received it.
    at io.smallrye.mutiny.helpers.test.AssertionHelper.fail(AssertionHelper.java:146)
    at io.smallrye.mutiny.helpers.test.AssertionHelper.shouldHaveCompleted(AssertionHelper.java:24)
    at io.smallrye.mutiny.helpers.test.UniAssertSubscriber.assertCompleted(UniAssertSubscriber.java:315)
...

The thing is that if I just comment out the .runSubscriptionOn(pool) line, the test pass as expected.

I am wondering if the best approach to test Unis in Mutiny is to leave that asynchronous behavior out of the test cases.

1 Answers

assertCompleted checks that the Uni has already been completed. In your case, you need awaitCompletion, which will await for the completion event or fails after a timeout.

BTW, awaitCompletion will pass even if the completion event has already been received.

Related