Below you will find five ways of using Mockito that will help you write better tests. We are going to find out how to mock a constructor, mock static methods, have stricter mocking rules, mock final methods and classes, and how to use verify with argument captors.
For the examples, I used version
4.2.0 of Mockito.
We also need Mock maker inline to mock constructors, final and static methods. To enable it, you either add this dependency or create the file
Option 1: Mock maker inline dependency
Add this dependency in your pom.
Option 2: Adding a file to enable the Mock maker inline
resources directory in your
test directory if you do not have one. In
resources create the directory
mockito-extensions and in
that directory the file
org.mockito.plugins.MockMaker. The complete path should look like this:
In the file, add the following line
mock-maker-inline. Now the mock maker inline is available to use.
mockConstruction you can mock calls made to the constructor. For example, we mock the constructor for the class
Dog when your code calls the constructor, it returns a mock object. Keep this code in a try with resources to limit the scope, so you only mock the constructor in that test method.
If the method that you are testing calls the constructor of a class, that you want to mock, you don't have direct access to change its mocking behavior. To get a reference of the created mock, you can call the
constructed() method on the
MockedConstruction instance. It returns a list of all the created mock objects. The example below uses it to see how often the constructor is called.
Mocking Static methods
With the inline mock maker, it's possible to mock static methods. The example shows how to mock the static method
Instant.now(). Every call made to it will return a mock of
Instant. Use a try with resources to limit the scope of the static method mocking to its test method.
I also included a version that uses
LocalDateTime, because I often want to mock one of these classes without changing production code or using PowerMock.
Use strict mocking
Mockito is a loose mocking framework by default which means that you are allowed to create Mocks that have no interaction. Making Mockito stricter forces you to be explicit about what you want to test, or the test will fail. Setting the Strictness to
STRICT_STUBS will result in higher quality and cleaner tests.
Below is an example of how we can achieve this. The code is run before and after each test so that mockito can tell which test is invalid.
Mocking final methods and classes
With the inline mock maker, you can mock final classes and methods. You can do this using
you would with every other object you want to mock. In the example, we see it is the same as with non-final classes and methods.
Combine verify with argument matchers
Normally you would use an instance of an ArgumentCaptor to verify the input of a mocked method. But that results in
multiple lines of code that you need to maintain. While it could be worth it if you do a lot of assertions. There is an easier way for less complex cases. With
argThat we create a custom matcher that is validated against the input of each method invocation.
If we mock a method that has multiple arguments, we need to add an extra
argThat like this:
I hope these few methods will help you write better tests, it helped me a lot to write higher-quality tests.
If you are curious and want to know more about what you can do with Mockito, please check out their documentation https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html. It lists all the Mockito features and how to use them.
More about testing in Java: