Tutorial by Examples

The class we are going to test is: public class Service { private Collaborator collaborator; public Service(Collaborator collaborator) { this.collaborator = collaborator; } public String performService(String input) { return collaborator.transformStri...
The class we are going to test is: public class Service{ private Collaborator collaborator; public Service(Collaborator collaborator){ this.collaborator = collaborator; } public String performService(String input){ return collaborator.transformS...
Installation The preferred way to install Mockito is to declare a dependency on mockito-core with a build system of choice. As of July 22nd, 2016, the latest non-beta version is 1.10.19, but 2.x is already encouraged to be migrated to. Maven <dependency> <groupId>org.mockito</...
Just some methods of an object can be mocked using spy() of mockito. For example, imagine that method class requires some web service to work. public class UserManager { List<User> users; public UserManager() { user = new LinkedLisk<User>(); } ...
This example shows a minimal Mockito test using a mocked ArrayList: import static org.mockito.Mockito.*; import static org.junit.Assert.*; import java.util.ArrayList; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRu...
To validate arguments to methods called on a mock, use the ArgumentCaptor class. This will allow you to extract the arguments into your test method and perform assertions on them. This example tests a method which updates the name of a user with a given ID. The method loads the user, updates the na...
Mockito provides a Matcher<T> interface along with an abstract ArgumentMatcher<T> class to verify arguments. It uses a different approach to the same use-case than the ArgumentCaptor. Additionally the ArgumentMatcher can be used in mocking too. Both use-cases make use of the Mockito.argT...
There are two ways to create object mocked by Mockito: via annotation via mock function Via annotation: With a JUnit test runner: @RunWith(MockitoJUnitRunner.class) public class FooTest { @Mock private Bar barMock; // ... } You can also use Mockito's JUnit @Rule, which...
Mockito.when(mock.returnSomething()).thenReturn("my val"); mock.returnSomething(); // returns "my val" mock.returnSomething(); // returns "my val" again mock.returnSomething(); // returns "my val" again and again and again... If you want different valu...
Lets assume we have this class and we would like to test doSmth method. In this case we want to see if parameter "val" is passed to foo. Object foo is mocked. public class Bar { private final Foo foo; public Bar(final Foo foo) { this.foo = foo; } public ...
To check if a method was called on a mocked object you can use the Mockito.verify method: Mockito.verify(someMock).bla(); In this example, we assert that the method bla was called on the someMock mock object. You can also check if a method was called with certain parameters: Mockito.verify(som...
void methods can be stubbed using the doThrow(), doAnswer(), doNothing(), doCallRealMethod() family of methods. Runnable mock = mock(Runnable.class); doThrow(new UnsupportedOperationException()).when(mock).run(); mock.run(); // throws the UnsupportedOperationException Note thatvoid methods...

Page 1 of 1