Unit tests with Mockito - Tutorial
Eliminate side effects by using test doubles.
- dummy object – is passed around but never used, i.e., its methods are never called.
- fake object – have working implementations, but are usually simplified. For example, they use an in memory database and not a real database.
- stub – is an partial implementation for an interface or class with the purpose of using an instance of this stub class during testing. Stubs usually don’t respond to anything outside what’s programmed in for the test. Stubs may also record information about calls.
- mock object is a dummy implementation for an interface or a class in which you define the output of certain method calls. Mock objects are configured to perform a certain behavior during a test. They typically record the interaction with the system and tests can validate that.
@Mock injects the dependencies.
@InjectMocks is the injectee class (typically the class being tested). The
MockitoAnnotations.initMocks(this); inside the
setup() is where these instances are created.