Writing a test case using Mockito and JUnit 5 extensions involves several steps. Here's a detailed guide on how to do it:
Step 1: Setup Your Project
First, ensure you have JUnit 5 and Mockito dependencies in your project. If you're using Maven, add these dependencies to your `pom.xml`:
xml
org.junit.jupiter
junit-jupiter-api
5.9.0
test
org.junit.jupiter
junit-jupiter-engine
5.9.0
test
org.mockito
mockito-core
4.1.0
test
org.mockito
mockito-junit-jupiter
4.1.0
test
For Gradle, add these to your `build.gradle`:
groovy
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
testImplementation 'org.mockito:mockito-core:4.1.0'
testImplementation 'org.mockito:mockito-junit-jupiter:4.1.0'
Step 2: Create a Class to Test
Let's say you have a simple `Service` class that depends on a `Database` interface:
java
public interface Database {
boolean isAvailable();
int getUniqueId();
}
public class Service {
private Database database;
public Service(Database database) {
this.database = database;
}
public boolean query(String query) {
return database.isAvailable();
}
@Override
public String toString() {
return "Using database with id: " + String.valueOf(database.getUniqueId());
}
}
Step 3: Write the Test Case
Now, you can write a test case using Mockito and JUnit 5 extensions. Use the `@ExtendWith(MockitoExtension.class)` annotation to enable Mockito support in JUnit 5.
java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class ServiceTest {
@Mock
Database databaseMock;
@Test
public void testQuery() {
assertNotNull(databaseMock);
// Configure the mock to return true when isAvailable() is called
when(databaseMock.isAvailable()).thenReturn(true);
// Create an instance of Service with the mock database
Service service = new Service(databaseMock);
// Call the method under test
boolean result = service.query("* from t");
// Assert the result
assertTrue(result);
}
}
Explanation:
- `@ExtendWith(MockitoExtension.class)`: This annotation tells JUnit 5 to use the Mockito extension, which automatically initializes mock objects annotated with `@Mock`.
- `@Mock Database databaseMock;`: This creates a mock object for the `Database` interface.
- `when(databaseMock.isAvailable()).thenReturn(true);`: This configures the mock to return `true` when `isAvailable()` is called.
- `Service service = new Service(databaseMock);`: Creates an instance of `Service` using the mock database.
- `boolean result = service.query("* from t");`: Calls the method under test.
- `assertTrue(result);`: Asserts that the result is as expected.
This setup allows you to easily write unit tests for classes with dependencies by mocking those dependencies using Mockito.
Citations:[1] https://www.lambdatest.com/blog/junit5-mockito-tutorial/
[2] https://www.tutorialspoint.com/mockito/mockito_junit_integration.htm
[3] https://www.lambdatest.com/blog/junit5-extensions/
[4] https://www.vogella.com/tutorials/Mockito/article.html
[5] https://www.youtube.com/watch?v=p7_cTAF39A8
[6] https://junit.org/junit5/docs/current/user-guide/
[7] https://stackoverflow.com/questions/40961057/how-to-use-mockito-with-junit-5
[8] https://www.youtube.com/watch?v=KYkEMuA50yE