Mockito : doAnswer Vs thenReturn

I am using Mockito for service later unit testing. I am confused when to use doAnswer vs thenReturn. Can anyone help me in detail? So far, I have tried it with thenReturn

throw checked Exceptions from mocks with Mockito

Check the Java API for List.The get(int index) method is declared to throw only the IndexOutOfBoundException which extends RuntimeException.You are trying to tell Mockito to throw an exception SomeException() that is not valid to be thrown by that particular method call. To clarify further.The List interface does not provide for a checked Exception to be thrown from the get(int index) method and that is why Mockito is failing.When … Read more

How does mockito when() invocation work?

The short answer is that in your example, the result of mock.method() will be a type-appropriate empty value; mockito uses indirection via proxying, method interception, and a shared instance of the MockingProgress class in order to determine whether an invocation of a method on a mock is for stubbing or replay of an existing stubbed behavior rather than passing … Read more