javaproject-reactorassertj

Asserting Mono completion softly


Suppose I need to softly assert against multiple object properties, one of which is a Mono. Typically, you use StepVerifier to assert on that. However, the verifyComplete() call will not be soft. In case the hard assertion fails, previous "soft" assertion errors will not be displayed.

How do I achieve my goal and retain all the assertion errors?

import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.assertj.core.api.SoftAssertions.assertSoftly;

public class SoftAssertionTest {

    @Test
    void testSoftAssertion() {
        Mono<String> stringMono = Mono.just("some value");
        SomeObject someObject = new SomeObject(42, stringMono);
        assertSoftly(soft -> {
            soft.assertThat(someObject.anInt()).isEqualTo(24); // this is soft
            StepVerifier.create(someObject.stringMono())
                    .verifyComplete(); // but this is hard, hogs all the attention
        });
    }

    record SomeObject(int anInt, Mono<String> stringMono) {}
}

Solution

  • You could try wrapping the StepVerifier call inside the satisfies assertion, something like:

     assertSoftly(soft -> {
                soft.assertThat(someObject.anInt()).isEqualTo(24);
                soft.assertThat(someObject).satisfies(obj -> StepVerifier.create(obj.stringMono()).verifyComplete());
                // or 
                // soft.assertThat(someObject.stringMono()).satisfies(strMono -> StepVerifier.create(strMono).verifyComplete());
    
            });
    

    I'm assuming here that StepVerifier.create(obj.stringMono()).verifyComplete() is throwing an AssertionError.