Skip to content
Sharp

Testing · interview question

In a unit test using Mockito, why would you use 'when-thenReturn'?

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

Correct answer

A. To specify the expected outcome of a method call on a mock

Why

'when-thenReturn' is used to define the behavior of a method call on a mock object, specifying what should be returned when that method is invoked. This allows you to simulate different scenarios and test the response of your code.

Test yourself on Testing

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

Start free test
All Testing questions →