Testing · interview question
In a unit test using Mockito, why would you use 'when-thenReturn'?
Multiple choiceMediumchapter-10-curatedpremiumtestingmocking-with-mockito
when(mockPaymentGateway.processPayment(any())).thenReturn(true);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.
More Testing questions
- How can build tools like Maven help manage library dependencies in Java projects?
- Why might you choose to use integration tests over unit tests for certain components in an e-commerce application?
- Which benefit of automated unit testing is most critical for maintaining large Java projects?
- How would you explain a significant advantage of using mocks at the boundary of a system, rather than the internals in…
- Why do teams use Mockito for mocking dependencies in tests in production code?
- When using Testcontainers for integration testing, why might a developer choose to reuse containers across tests?