Introduction
Mockito is an awesome framework that offers multiple ways to create a mock instance. Each way has its pros and cons. In this post, I will show you every way (that I know of) to create a mock instance.
For the examples, I will use this small Car
class. It has everything we need to create a mock instance of it.
|
|
I used Mockito version 4.6.1 for the examples. You can find the Maven dependency below. The Junit4 examples use an older Mockito version that you can find later in the article.
|
|
Using Mockito.mock()
The Mockito.mock(Car.class)
method is the most straightforward way to create a mock object of a class or interface.
We can use the object it creates to alter the behavior of the methods and how it is used.
This is an example of how you could use the method:
|
|
But we can make this example a little less verbose and more compact.
Using a static import
When you include the following static import we can call the mock()
method directly.
|
|
With the static import included, we can drop the Mockito.
from line 5 and use the method directly to create a mock for us.
|
|
Using @Mock and Junit5
We can combine the power of Junit with Mockito. To do that, we need to add this dependency:
|
|
With the mockito-junit-jupiter
dependency included, we can use the @ExtendWith(MockitoExtension.class)
annotation.
This will create a mock for every field in your test class that is annotated with @Mock
. This increases the readability
of your test class because you don't have to call mock()
for each mock anymore.
Below you will see an example of using Junit5 with Mockito:
|
|
Using @MockitoSettings
@MockitoSettings
extends the MockitoExtension.class
, meaning it can do the same and more. It will create a mock
for each @Mock annotated field in your test class.
With @MockitoSettings
you can also change the strictness of your tests. The strictness helps you by giving hints to
improve how you use your mocks. You can read more about it in this post about setting the strictness of Mockito
|
|
Using @Mock and Junit4
For the Junit4 examples, I these dependencies of Junit4 and Mockito:
|
|
As with Junit5, we can combine the power of Junit with Mockito. To use @Mock
with Junit4, we need a different annotation
above the class @RunWith(MockitoJUnitRunner.class)
. This will create a mock for every @Mock
annotated field in your test class.
|
|
Using Junit rule
We can also use a @Rule
to create mock objects for the @mock annotated field. This is handy for when you can't or don't want
to use the Junit4 test runner.
|
|
Using @Mock without a test framework
You can also initialize all your annotated fields by calling the MockitoAnnotations.openMocks(this).close();
method. It
behaves the same as @ExtendWith(MockitoExtension.class)
, it will create a mock for each annotated field in your test class.
In earlier versions of Mockito, you could also use MockitoAnnotations.initMocks(this);
. The method is still available
but marked as deprecated. Calling openMocks(...).close()
is recommended and behaves mostly the same.
|
|
Conclusion
This article looked at the possibilities available to create Mock objects and how to use them.
Further reading
More about testing in Java: