Introduction
In this chapter, we will guide you through the steps to set up Mockito in your Java project. Mockito is a powerful framework that simplifies the creation of mock objects for testing. By adding Mockito to your project, you can write effective and maintainable tests.
Using Maven
To use Mockito with Maven, you need to add the necessary dependencies to your pom.xml
file. Here’s how to do it using the latest version of Mockito (5.12.0).
Adding Dependencies
Open your pom.xml
file and add the following dependencies within the <dependencies>
section:
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Updating the Project
After adding the dependencies, update your Maven project to download the new dependencies. You can do this by right-clicking on your project in the IDE and selecting "Maven" > "Update Project".
Using Gradle
To use Mockito with Gradle, you need to add the necessary dependencies to your build.gradle
file. Here’s how to do it using the latest version of Mockito (5.12.0).
Adding Dependencies
Open your build.gradle
file and add the following dependencies within the dependencies
section:
dependencies {
testImplementation 'org.mockito:mockito-core:5.12.0'
testImplementation 'org.mockito:mockito-junit-jupiter:5.12.0'
}
Updating the Project
After adding the dependencies, refresh your Gradle project to download the new dependencies. You can do this by clicking the "Refresh" button in the Gradle tool window in your IDE.
Example Project Setup
Here’s an example of how your project structure might look after setting up Mockito.
Project Structure
my-project
|-- src
| |-- main
| | |-- java
| | | |-- com
| | | | |-- example
| | | | | |-- Calculator.java
| |-- test
| | |-- java
| | | |-- com
| | | | |-- example
| | | | | |-- CalculatorTest.java
|-- pom.xml
|-- build.gradle
Calculator.java
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public double multiply(double a, double b) {
return a * b;
}
public double divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
CalculatorTest.java
package com.example;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
public class CalculatorTest {
@Test
void testAdd() {
// Arrange
Calculator calculator = mock(Calculator.class);
when(calculator.add(2, 3)).thenReturn(5);
// Act
int result = calculator.add(2, 3);
// Assert
assertEquals(5, result);
}
@Test
void testSubtract() {
// Arrange
Calculator calculator = mock(Calculator.class);
when(calculator.subtract(3, 2)).thenReturn(1);
// Act
int result = calculator.subtract(3, 2);
// Assert
assertEquals(1, result);
}
@Test
void testMultiply() {
// Arrange
Calculator calculator = mock(Calculator.class);
when(calculator.multiply(2.0, 3.0)).thenReturn(6.0);
// Act
double result = calculator.multiply(2.0, 3.0);
// Assert
assertEquals(6.0, result, 0.001);
}
@Test
void testDivide() {
// Arrange
Calculator calculator = mock(Calculator.class);
when(calculator.divide(6.0, 3.0)).thenReturn(2.0);
// Act
double result = calculator.divide(6.0, 3.0);
// Assert
assertEquals(2.0, result, 0.001);
}
@Test
void testDivideByZero() {
// Arrange
Calculator calculator = mock(Calculator.class);
when(calculator.divide(1.0, 0)).thenThrow(new IllegalArgumentException("Division by zero"));
// Act and Assert
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
calculator.divide(1.0, 0);
});
assertEquals("Division by zero", exception.getMessage());
}
}
Conclusion
Setting up Mockito in your project is straightforward with Maven or Gradle. By following the steps outlined in this chapter, you can quickly add Mockito to your Java project and start writing effective tests with mock objects. This will help you create more reliable and maintainable code.