Описание тега mockito
Mockito is a mocking framework for java (recent versions support Android via dexmaker), and like all other mocking frameworks is used as a helper in creating mock-objects for the use of isolated unit tests.
Mockito is used to mock interfaces so that a dummy functionality can be added to a interface that can be used in unit testing.
Its creator, Szczepan Faber, wanted to create a mocking framework that is simpler to use. It is inspired and was first built upon easymock's code.
import static org.mockito.Mockito.*;
// mock creation
List mockedList = mock(List.class);
// using mock
mockedList.add("one");
mockedList.clear();
// verification
verify(mockedList).add("one");
verify(mockedList).clear();
// stubbing and verification of stub
when(mockedList.get(0)).thenReturn("one");
assertEquals("one", mockedList.get(0));
As Android uses a different class format and a different VM, called Dalvik, running Mockito under Android requires dexmaker (Dexmaker jars must be in the same classpath as Mockito jars). As of v 1.9.5, no other workarounds are required.
Mockito is generally thought not to support mocking of private, final, or static injection. Although this is mostly true, there is a caveat: from v2.1.0 onwards Mockito does support mocking final classes and methods although this feature is not available by default, instead it must be explicitly enabled.
It is more common to use powermock as a Mockito extension to support mocking of private, final, or static injection.
Resources
- Source code: GitHub ( Issue Tracker)
- Documentation: Main reference documentation, Declaring mockito dependency