Mockito Installation Guide
Prerequisites
Before installing Mockito, ensure your development environment meets the following requirements.
Mockito 5.x requires JDK 11 or higher. If your project needs to support JDK 8, you can use Mockito 4.x series. Using the latest version of Mockito is recommended to get the newest features and security fixes.
Maven Installation
Adding Dependencies
Add the mockito-core dependency to your Maven project's pom.xml file. Using a property to define the Mockito version is recommended for version management.
<properties>
<mockito.version>5.14.0</mockito.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
If you need to use Mockito's inline functionality to mock final classes and methods, you also need to add the mockito-inline dependency.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
Snapshot Versions
Mockito publishes snapshot versions to the Sonatype repository after every code change. If you need to use the latest development features, you can add the snapshot repository to your pom.xml.
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
Gradle Installation
Adding Dependencies
Add the dependency declaration to your Gradle project's build.gradle file.
dependencies {
testImplementation 'org.mockito:mockito-core:5.14.0'
}
If using Gradle's Kotlin DSL file (build.gradle.kts), the syntax differs slightly.
dependencies {
testImplementation("org.mockito:mockito-core:5.14.0")
}
Using BOM for Version Management
Using the Spring Dependency Management plugin or Gradle's BOM feature can simplify version management.
dependencyManagement {
imports {
mavenBom 'org.mockito:mockito-bom:5.14.0'
}
}
dependencies {
testImplementation 'org.mockito:mockito-core'
}
Manual Download
If your project does not use Maven or Gradle, you can download JAR files directly from the Maven Central repository. Visit Maven Central and search for org.mockito to find all versions of mockito-core and mockito-all (deprecated) distributions.
After downloading, add the JAR files to your project's classpath. Note that you also need to download dependent libraries like Byte Buddy and Objenesis.
Spring Boot Integration
Spring Boot's spring-boot-starter-test already includes Mockito dependency. If your project uses Spring Boot, you generally do not need to add Mockito dependency separately.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
If you need to use Mocks more conveniently in a Spring environment, you can use the @MockBean annotation, which automatically injects mock objects into the Spring container.
JUnit 5 Integration
Using MockitoExtension
JUnit 5 introduced an extension model, and Mockito provides MockitoExtension to support JUnit 5. No additional dependency configuration is needed as mockito-core already contains the necessary support.
@ExtendWith(MockitoExtension.class)
class MyTest {
@Mock
private List<String> mockedList;
@Test
void testSomething() {
when(mockedList.get(0)).thenReturn("test");
assertEquals("test", mockedList.get(0));
}
}
Using MockitoJUnitRunner
For JUnit 4, you can use MockitoJUnitRunner to automatically initialize Mock objects.
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
@Mock
private MyService myService;
@Test
public void testSomething() {
when(myService.getData()).thenReturn("mocked data");
assertEquals("mocked data", myService.getData());
}
}
Verifying Installation
After installation, you can verify that Mockito is properly configured by creating a simple test class. If the test passes after running, Mockito is installed correctly.
import static org.mockito.Mockito.*;
public class VerifyInstallation {
public static void main(String[] args) {
List<String> mock = mock(List.class);
when(mock.get(0)).thenReturn("Hello");
System.out.println(mock.get(0));
verify(mock).get(0);
System.out.println("Mockito installed successfully!");
}
}
Version Selection Recommendations
For new projects, using the latest Mockito 5.x version is recommended. If your project needs JDK 8 compatibility, Mockito 4.x series is available. Always use the latest stable version for better performance and security.
