Java Mockito JUnit 5. User Teach me Java Mockito with Junit 5 and JDK 1.8 ChatGPT Sure! I'll walk you through the basics of using Mockito with JUnit 5 and JDK 1.8. Mockito is a popular framework for mocking objects in Java tests, which allows you to create and configure mock objects. JUnit 5 is the latest version of the JUnit testing framework.
-
Ensure JDK 1.8 is installed: Make sure that you have JDK 1.8 installed and configured on your machine.
-
Add dependencies: Add JUnit 5 and Mockito dependencies to your
pom.xml
if you are using Maven, orbuild.gradle
if you are using Gradle.
For Maven, your pom.xml
should include:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.7.7</version>
<scope>test</scope>
</dependency>
For Gradle, your build.gradle
should include:
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testImplementation 'org.mockito:mockito-core:3.7.7'
testImplementation 'org.mockito:mockito-junit-jupiter:3.7.7'
Let's write a simple example to demonstrate how to use Mockito with JUnit 5.
Suppose we have a Calculator
class with a dependency on a MathService
class.
public class MathService {
public int add(int a, int b) {
return a + b;
}
}
public class Calculator {
private MathService mathService;
public Calculator(MathService mathService) {
this.mathService = mathService;
}
public int add(int a, int b) {
return mathService.add(a, b);
}
}
We want to test the Calculator
class by mocking the MathService
class.
Create a test class CalculatorTest
with the following content:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class CalculatorTest {
@Mock
private MathService mathService;
@InjectMocks
private Calculator calculator;
@Test
public void testAdd() {
// Arrange
when(mathService.add(10, 20)).thenReturn(30);
// Act
int result = calculator.add(10, 20);
// Assert
assertEquals(30, result);
}
}
-
Annotations:
@ExtendWith(MockitoExtension.class)
: This is used to integrate Mockito with JUnit 5.@Mock
: This annotation is used to create and inject mocked instances.@InjectMocks
: This annotation is used to inject the mock dependencies into theCalculator
instance.
-
Test Method:
when(mathService.add(10, 20)).thenReturn(30);
: This line sets up the mock behavior. It tells Mockito to return30
when theadd
method ofmathService
is called with the arguments10
and20
.calculator.add(10, 20);
: This calls the method under test.assertEquals(30, result);
: This asserts that the result is as expected.
To run the test, you can use your IDE's built-in test runner or use a build tool like Maven or Gradle:
- Maven:
mvn test
- Gradle:
gradle test
This will execute the CalculatorTest
and you should see that the test passes if everything is set up correctly.
This basic example demonstrates how to use Mockito with JUnit 5 to mock dependencies and write unit tests for a Java application. You can expand on this by exploring more features of Mockito such as argument matchers, verifying interactions, and more complex setups. User what is wrong with the following pom.xml file
4.0.0
org.example Play_with_Mockito 1.0-SNAPSHOT
8 8 UTF-8 org.junit.jupiter junit-jupiter-engine 5.7.0 test org.mockito mockito-core 3.7.7 test org.mockito mockito-junit-jupiter 3.7.7 test org.apache.maven.plugins maven-compiler-plugin 3.8.1