In the previous tutorial Overview about Mock in Unit Test, I introduced you all a way to mock an object using the Mockito class. Mockito also supports us another way to initialize the Mock object using @Mock annotation. How is it in details? Let’s find out in this tutorial!
First, I will create a new Maven project as an example:
With Mockito and JUnit dependencies as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.6.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.7.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.0</version> <scope>test</scope> </dependency> |
With JUnit 5, to run Unit Test with Maven, we need to declare the maven-surefire-plugin plugin of Maven with the latest version as follows:
1 2 3 4 5 6 7 8 |
<build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> |
And compile source with target using Java 8 onwards:
1 2 3 4 |
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> |
In this tutorial, I also created an application similar to the previous tutorial, but this time, Calculation class will define a method to calculate the subtraction of two numbers.
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.mockito; public class Calculation { public int sub(int a, int b) { return a - b; } } |
The Application class has a method that uses the method of class Calculation to calculate the subtraction of two numbers, if the result is greater than 0 then returns true, otherwise false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.huongdanjava.mockito; public class Application { private Calculation calculation; public boolean check(int a, int b) { int result = calculation.sub(a, b); return result > 0; } public Calculation getCalculation() { return calculation; } public void setCalculation(Calculation calculation) { this.calculation = calculation; } public static void main(String[] args) { Application a = new Application(); a.setCalculation(new Calculation()); if (a.check(2, 4)) { System.out.println("Positive"); } else { System.out.println("Negative"); } } } |
I will now write Unit Test for the Application class using the mock object of the Calculation class with the mock object of the Calculation class created using the @Mock annotation.
With the @Mock annotation, to initialize the mock object for the Calculation class, we first need to declare class Calculation with the @Mock annotation within the ApplicationTest class as follows:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava.mockito; import org.mockito.Mock; public class ApplicationTest { @Mock private Calculation calculation; } |
Next, we need to declare MockitoExtension.class with the annotation @ExtendWith of JUnit 5 above the ApplicationTest class:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.mockito; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class ApplicationTest { @Mock private Calculation calculation; } |
With this extension, Mockito will scan our ApplicationTest class to retrieve all classes that we declare with the @Mock annotation to perform the mock object creation.
The code to test the check() method of the Application class is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.huongdanjava.mockito; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class ApplicationTest { @Mock private Calculation calculation; @Test public void testCheck() { Application application = new Application(); application.setCalculation(calculation); Mockito.when(calculation.sub(2, 12)).thenReturn(-10); int a = 2; int b = 12; boolean c = application.check(a, b); assertEquals(false, c); } } |
Now we can run the Unit Test to check the results.
Result:
1 2 3 4 5 6 7 8 9 10 |
[INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.huongdanjava.mockito.ApplicationTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.484 s - in com.huongdanjava.mockito.ApplicationTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] |