Overview - doReturn and doThrow
What is it?
In JUnit testing with Mockito, doReturn and doThrow are methods used to control how mocked objects behave. doReturn lets you specify a value to return when a method is called, while doThrow lets you specify an exception to be thrown. These help simulate different scenarios without running real code.
Why it matters
Without doReturn and doThrow, tests would rely on actual implementations, making tests slow, unreliable, or hard to isolate. They let you test how your code reacts to different responses or errors, ensuring your program handles all cases safely.
Where it fits
Before learning doReturn and doThrow, you should understand basic JUnit tests and Mockito mocks. After mastering these, you can explore advanced mocking techniques like spying, argument captors, and verifying call order.