skyho
skyho

Reputation: 1903

MonoJust cannot be returned by toString() toString() should return String (Reactive streams and Mockito)

@ExtendWith(value = SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class BaseAbstractTest {

    @MockBean
    protected TokenService tokenService;

    @MockBean
    protected UserService userService;

    protected AccessService accessService;

}

    @BeforeAll
    protected void init() {

 Mono<Token> tokenExpected = Mono.just(new Token());

      given(
                this.tokenService.createToken(
                        any(Auth.class),
                        any(String.class),
                        any(UUID.class),
                        any(String.class),
                        any(Duration.class)
                )
        )
                .willReturn(tokenExpected);

  this.accessService =
                new AccessService(
                        this.tokenService,             
                        this.userService);

}

public interface MfaTokenService {

  Mono<MfaToken> createNewToken(Auth authentication,
                                     String sessionId,
                                     UUID sessionId,
                                     String phone,
                                     Duration ttl);
}

When initialization occurs and mock behaviors are created, then I get an error ( .willReturn(tokenExpected) ):

MonoJust cannot be returned by toString() toString() should return String *** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:

  1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.
  2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
    • with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: MonoJust cannot be returned by toString() toString() should return String *** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:

  1. This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.
  2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
    • with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

Who has any idea why the error is happening and how it can be fixed ?

Upvotes: 0

Views: 4796

Answers (1)

skyho
skyho

Reputation: 1903

I have used so:

@ExtendWith(value = SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class BaseAbstractTest {

    @MockBean
    protected TokenService tokenService;

    @MockBean
    protected UserService userService;

    protected AccessService accessService;

}

    @BeforeEach
    protected void init() {

 Mono<Token> tokenExpected = Mono.just(new Token());

      given(
                this.tokenService.createToken(
                        any(Auth.class),
                        any(String.class),
                        any(UUID.class),
                        any(String.class),
                        any(Duration.class)
                )
        )
                .willReturn(tokenExpected);

        doReturn(this.tokenExpected)
                .when(this.tokenService)
                .createToken(
                        any(UserAuthentication.class),
                        any(String.class),
                        any(UUID.class),
                        any(String.class),
                        any(Duration.class)
                );

  this.accessService =
                new AccessService(
                        this.tokenService,             
                        this.userService);

}

and than:

@Test
void test(){

RequestDto requestDto = new RequestDto();

 StepVerifier.create(
                    accessService.getAccess(requestDto)
                )
                .assertNext(responseDto -> {

               Assertions.assertEquals(expectedValue, responseDto.getValueActual);

                )
                .verifyComplete();
}

It's important: use an annotation @BeforeEach (when you will creating shared mock beans). Otherwise, you may encounter an error :

expectation "assertNext" failed (expected: onNext(); actual: onError(java.lang.NullPointerException: The mapper returned a null Mono)) java.lang.AssertionError: expectation "assertNext" failed (expected: onNext(); actual: onError(java.lang.NullPointerException: The mapper returned a null Mono))

Upvotes: 0

Related Questions