Skip to content

How can I write unit / integration tests?#

Mutiny provides subscribers for Uni and Multi offering helpful assertion methods. You can use them to test pipelines.

Here is an example to test a Uni:

1
2
3
4
5
6
7
8
Uni<Integer> uni = Uni.createFrom().item(63);

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

subscriber
        .awaitItem()
        .assertItem(63);

Testing a Multi pipeline is similar:

1
2
3
4
5
6
7
8
Multi<Integer> multi = Multi.createFrom().range(1, 5)
        .onItem().transform(n -> n * 10);

AssertSubscriber<Integer> subscriber = multi.subscribe().withSubscriber(AssertSubscriber.create(10));

subscriber
        .awaitCompletion()
        .assertItems(10, 20, 30, 40);

The assertions do not just focus on good outcomes, you can also test failures as in:

1
2
3
4
5
6
7
8
Multi<Object> multi = Multi.createFrom().failure(() -> new IOException("Boom"));

AssertSubscriber<Object> subscriber = multi
        .subscribe().withSubscriber(AssertSubscriber.create(10));

subscriber
        .awaitFailure()
        .assertFailedWith(IOException.class, "Boom");