Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Mockito says MonoJust cannot be returned by toString() while you stub another method

Your error

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: MonoJust cannot be returned by toString()2 more lines it can show

Fix it fast

Most common cause, 1 of 2

A spy is stubbed with when(spy.method())

Stub spies with the doReturn family.

Fix
import static org.mockito.Mockito.doReturn;

OrderService spyService = spy(new OrderService(repository));
doReturn(Mono.just(order)).when(spyService).findOrder("42");
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

WrongTypeOfReturnValue, such as MonoJust cannot be returned by toString(), means thenReturn() attached your value to a different mock call than the one you meant. Mockito names two causes: a spy stubbed with when(spy.method()), which runs the real method first, and stubbing or verifying a shared mock from several threads. Use doReturn(...).when(spy).method() and stub before starting threads. Mockito 5.24.

  • Applies to Mockito 5.24.0, Reactor Core 3.9.0 with reactor-test, Maven Surefire
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Open the test line from the stack trace and look at the object inside when(...). If it is a spy (spy(...) or @Spy), cause 1 applies. If it is a plain mock and the code under test starts threads or schedulers, look at cause 2.

Run this first
grep -nE "@Spy|spy\(|when\(" src/test/java/com/example/OrderServiceTest.java

Then find the line you see in this list and open its fix:

  1. 1A spy is stubbed with when(spy.method())The receiver in when(...) was created with spy() or @Spy, and the method named in the error (toString() here) is not the one you stubbed
  2. 2A shared mock is stubbed or verified from more than one threadThe failure is intermittent, and the code under test already runs on another thread (an executor, a parallel stream or a Reactor scheduler) while the test is still stubbing

What does the error look like?

  • org.mockito.exceptions.misusing.WrongTypeOfReturnValue: MonoJust cannot be returned by toString()
  • toString() should return String
  • 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -

Cause 1 of 2

A spy is stubbed with when(spy.method())

You seeThe receiver in when(...) was created with spy() or @Spy, and the method named in the error (toString() here) is not the one you stubbed

Check

WrongTypeOfReturnValue on a spy happens because when(spy.method()) calls the real method before stubbing. That real code may call other mocks on the way, for example toString() while logging, and the error then names that call instead of the method you meant. Mockito's own message lists this case and its javadoc calls when() on spies impossible or impractical at times.

Check
grep -nE "when\((spy|.*Spy)" src/test/java/com/example/OrderServiceTest.java

Fix

Stub spies with the doReturn family. The real method is not called while the stub is set up, so the value lands on the right method.

Fix
import static org.mockito.Mockito.doReturn;

OrderService spyService = spy(new OrderService(repository));
doReturn(Mono.just(order)).when(spyService).findOrder("42");

Verify

The test reaches its assertions, and spyService.findOrder("42") emits the stubbed order.

Verify
./mvnw test -Dtest=OrderServiceTest

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

A shared mock is stubbed or verified from more than one thread

You seeThe failure is intermittent, and the code under test already runs on another thread (an executor, a parallel stream or a Reactor scheduler) while the test is still stubbing

Check

Mockito is thread-safe only when a single thread stubs and verifies a shared mock. Its FAQ says that stubbing or verifying a shared mock from different threads gives intermittent behaviour and occasional exceptions such as WrongTypeOfReturnValue, because another thread's call can slip in between when() and thenReturn().

Check
grep -nE "subscribeOn|publishOn|parallel\(|ExecutorService" src/main/java/com/example/OrderService.java

Fix

Finish every when() or doReturn() before the code under test starts its threads or subscribes, and keep stubbing on the test thread only. Calls from other threads are fine once stubbing is done.

Fix
// stub first, on the test thread
when(repository.findById("42")).thenReturn(Mono.just(order));

// then start the work that may switch threads
StepVerifier.create(service.loadOrder("42"))
    .expectNext(order)
    .verifyComplete();

Verify

Running the test class several times in a row passes every run, with no WrongTypeOfReturnValue.

Verify
for i in 1 2 3 4 5; do ./mvnw -q test -Dtest=OrderServiceTest || break; done

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Make doReturn().when(spy) the team rule for spies and flag when(spy.x()) in code review.
  • Keep all stubbing in the arrange part of the test, before anything subscribes or submits work to an executor.
  • Prefer plain mocks over spies for collaborators, so no real method runs during stubbing.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.