Introduction
This post builds further on this earlier post where we added testing to a Java project. In this tutorial, we are going to create a test and mock a dependency the production code uses with Mockito as our mocking library.
The code we want to test
To show you how to create a mock inside a unit test we need to have some code we can test. For the examples in this post, I am going to use the following example of the Car and Engine class.
|
|
The Car class has a dependency on the Engine class. The Car uses the Engine class inside the makeSound() method. If the engine starts we get a sound, if not we get a string saying the engine could not start. We are going to mock the Engine class so the start method returns false when we want to.
The testing code
In the following example, I created two test cases. In the first example, the engine starts and returns true, and in the second example the engine fails to start and returns false. The mocking of the Engine class is done on line 13 in the first example and on line 28 in the second example. On those lines, the mock is created, and we can change the behavior of those mocks. I change the behavior on line 14 and 29. With the when method, I can pass a method that I want to stub. For the first test I want to return true and for the second test, I want to return false.
|
|
Throughout the tests only the mockEngine is used and not a real instance of the Engine class. When I create a Car I pass the mock and not a newly created engine instance. This is very important as the Car instance has to use the mocks for the tests to succeed.
Conclusion
This post covered how to create mock instances inside unit tests. This makes testing different branches inside your code a lot easier as you can influence the behavior of the dependencies of a class.