In this short tutorial, we’re going to explore how to use JUnit 5 and Mockito, a popular mocking framework for unit tests written in Java.
1. JUnit 5 and Mockito Dependencies
1.1. Maven Dependencies
At first, let’s take a look at the Maven dependencies for JUnit 5:
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 |
<properties> <junit.jupiter.version>5.3.0</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> |
Next, we’re going to include Mockito’s mockito–junit–jupiter, an official support from the Mockito for JUnit 5:
1 2 3 4 5 6 |
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>2.22.0</version> <scope>test</scope> </dependency> |
1.2. Gradle Dependencies
Here is how our Gradle build file looks like:
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 |
plugins { id 'java' id 'eclipse' // optional (to generate Eclipse project files) id 'idea' // optional (to generate IntelliJ IDEA project files) } repositories { mavenCentral() } dependencies { testCompile('org.junit.jupiter:junit-jupiter-api:5.3.0') testCompile('org.junit.jupiter:junit-jupiter-params:5.3.0') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.0') } test { useJUnitPlatform() testLogging { events "passed", "skipped", "failed" } } wrapper { gradleVersion = '4.8' } |
2. JUnit 5 and Mockito Example
Let’s see an example test class that we use Mockito with JUnit 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class UserServiceTest { @Mock UserManager userManager; @Test public void testSaveArgMatch() throws Exception { final String name = "Carbery"; UserService userService = new UserService(userManager); userService.save(name); Mockito.verify(userManager, Mockito.times(1)).save(Mockito.anyString()); Mockito.verify(userManager, Mockito.times(1)).save(Mockito.isA(String.class)); Mockito.verify(userManager, Mockito.times(1)).save(Mockito.startsWith("Ca")); Mockito.verify(userManager, Mockito.times(1)).save(Mockito.endsWith("ry")); } } |
We need to get the Mockito extension MockitoExtension for JUnit 5 enabled:
1 2 3 4 |
@ExtendWith(MockitoExtension.class) public class UserServiceTest { // } |
It initializes mocks and handles strict stubbings. And now we have all the Mockito annotations ready to work, for example:
1 2 |
@Mock UserManager userManager; |
We have used @Mock annotation to create a mock implementation for the UserManager interface we need.
Notice that the JUnit 5 extension Mockito by default works in the “strict mode”. To change the default behavior, we can use @MockitoSettings annotation as follows:
1 2 3 4 5 |
@ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.WARN) public class UserServiceTest { // } |
3. Conclusion
The tutorial has illustrated how to use Mockito with JUnit5 using official extension supported from Mockito. It’s really nice to use mock objects with JUnit 5.
The sample source code presented in the tutorial is available on my Github project. It’s a Maven based project, so it’s easy to be imported in IDE such as Eclipse, IntelliJ.
The tutorial is a part of JUnit 5 series. If you want to dig deeper and learn other cool things you can do with the JUnit 5 – head on over to the main JUnit 5 tutorial.