Skip to content
Sharp

Testing · interview question

In a test using Mockito, what is the effect of calling 'doReturn()' instead of 'when()'?

Multiple choiceMediumchapter-10-curatedpremiumtestingmocking-with-mockito
java
doReturn(true).when(mockPaymentGateway).processPayment(any());
Choose one answer
Answer and explanation

Correct answer

D. It allows stubbing of void methods.

Why

'doReturn()' is used to stub methods, including those that return void, and offers a different syntax than 'when()'. It is particularly useful when working with methods that might throw exceptions or when you prefer an alternative to the 'when-then' syntax.

Test yourself on Testing

A free test with instant, explained results. No account needed.

Start free test
All Testing questions →