What are mock objects in Java?

A Mock object is something used for unit testing. If you have an object whose methods you want to test, and those methods depend on some other object, you create a mock of the dependency rather than an actual instance of that dependency. This allows you to test your object in isolation. Common Java frameworks … Read more

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

What’s the difference between a mock & stub?

Stub I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They would not do anything … 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