spring-bootunit-testingreactive-programmingspring-webfluxwebflux

Reactive Test case with Mono Stepverifier


I'm new to Reactive programming in spring webflux. I have the below scenario.

While trying to write a test case for Mono<List<MyClass>> I'm getting an error.

List<MyResponse> response= List.of(MyResponse.builder()
    .separator("value").build());
MyRequest request= StubGenerator.getRequest();

mockServer.enqueue(new MockResponse()
        .setBody("{\"separator\":\"value\"}")
        .addHeader("Content-Type", "application/json"));

Mono<List<MyResponse>> responseMono = MyService.getResponse(request, "token");

StepVerifier.create(responseMono)
    .expectNext(response)
    .verifyComplete();

I'm getting JSON deserializer error

java.lang.AssertionError: expectation "expectNext([MyResponse(errorReason=null, separator=value)])" failed 
(expected value: [MyResponse(errorReasonInvSeparator=null, invoiceSeparator=value)]; actual value: [MyResponse(errorReasonInvSeparator=JSON decoding error: Cannot deserialize value of type `java.util.ArrayList<net.model.MyResponse>` from Object value (token `JsonToken.START_OBJECT`); 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<net.model.MyResponse>` from Object value (token `JsonToken.START_OBJECT`)

Solution

  • You are setting the body as a single object, but you are expecting it to be a list. You need to change your JSON in the body as follows (mind the [ (...) ]):

    mockServer.enqueue(new MockResponse()
            .setBody("[ {\"separator\":\"value\"} ]")
            .addHeader("Content-Type", "application/json"));