--Show sample code for basic mocking and testing with Mockito 3 + JUnit 5 --Operation check environment this time: Java 14 (AdoptOpenJDK 14.0.2 + 12) + Gradle 6.5.1 + Mockito 3.4.6 + JUnit 5.6.2 + macOS Catalina
├── build.gradle
└── src
├── main
│ └── java
│ ├── Client.java
│ └── Worker.java
└── test
└── java
└── ClientTest.java
build.gradle
Gradle configuration file for build test execution.
plugins {
id 'java'
}
repositories {
jcenter()
}
dependencies {
//Introduced Junit 5
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
//Introduced Mockito 3
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testImplementation 'org.mockito:mockito-core:3.4.6'
//Introducing the JUnit 5 Extension library with Mockito
// https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
testImplementation 'org.mockito:mockito-junit-jupiter:3.4.6'
}
test {
//Settings to use JUnit platform
useJUnitPlatform()
//Output settings during testing
testLogging {
//Display standard output and standard error output during testing
showStandardStreams true
//Output event(TestLogEvent)
events 'started', 'skipped', 'passed', 'failed'
//Output settings when an exception occurs(TestExceptionFormat)
exceptionFormat 'full'
}
}
src/main/java/Worker.java
The class to be mocked.
public class Worker {
//Method with argument / return value
public int ariari(int x) {
throw new IllegalStateException("Exceptions that seem to occur depending on the environment");
}
//Method with no arguments and no return value
public void nasinasi() {
throw new IllegalStateException("Exceptions that seem to occur depending on the environment");
}
}
src/main/java/Client.java
The class to be tested.
public class Client {
private Worker worker;
//Call a method with arguments and return values
public int callAriari(int x) {
return worker.ariari(x * 2); //Doubled value for Worker#Pass to ariari
}
//Call a method with no arguments and no return value
public int callNasinasi(int x) {
worker.nasinasi(); // Worker#Call nasinasi
return x * 2; //Returns a doubled value
}
}
src/test/java/ClientTest.java
Test class. Mock the Worker class with Mockito 3 and test the Client class with JUnit 5.
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.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
@ExtendWith(MockitoExtension.class) //JUnit 5 extension with Mockito
public class ClientTest {
//Object to inject mock
@InjectMocks
private Client client = new Client();
//Object to mock
@Mock
private Worker mockedWorker;
@Test
public void with arguments_There is a return value() {
//Mock behavior: Worker#Returns 6 when 2 is passed to ariari
doReturn(6).when(mockedWorker).ariari(2);
//test: Client#Passing 1 to callAriari is a mock worker#Pass 2 to ariari and 6 is returned
assertEquals(6, client.callAriari(1));
}
@Test
public void no arguments_No return value() {
//Mock behavior: Worker#Do nothing when calling nasinasi
doNothing().when(mockedWorker).nasinasi();
//test: Client#Passing 1 to callNasinasi is a mock worker#Run nasinasi and get 2 back
assertEquals(2, client.callNasinasi(1));
}
@Test
public void Exception occurred() {
//Mock behavior: Worker#Throw an exception when 4 is passed to ariari
doThrow(new IllegalArgumentException("Mock exception")).when(mockedWorker).ariari(4);
//test: Client#Passing 2 to callAriari is a mock worker#Passing 4 to ariari throws an exception
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> client.callAriari(2));
//test:The exception contains the expected message
assertEquals("Mock exception", e.getMessage());
}
@Test
public void sequential call() {
//Mock behavior: Worker#Throws an exception twice when 6 is passed to ariari and then returns 18.
doThrow(new IllegalArgumentException("1st mock exception"))
.doThrow(new IllegalArgumentException("Second mock exception"))
.doReturn(18)
.when(mockedWorker).ariari(6);
//test: Client#Passing 3 to callAriari is a mock worker#Pass 6 to ariari and throw an exception
IllegalArgumentException e1 = assertThrows(IllegalArgumentException.class, () -> client.callAriari(3));
assertEquals("1st mock exception", e1.getMessage());
//test: Client#Passing 3 to callAriari is a mock worker#Pass 6 to ariari and throw an exception
IllegalArgumentException e2 = assertThrows(IllegalArgumentException.class, () -> client.callAriari(3));
assertEquals("Second mock exception", e2.getMessage());
//test: Client#Passing 3 to callAriari is a mock worker#Pass 6 to ariari and 18 is returned
assertEquals(18, client.callAriari(3));
}
}
An example of running the test task in Gradle.
$ gradle test
> Task :test
ClientTest >No arguments_No return value() STARTED
ClientTest >No arguments_No return value() PASSED
ClientTest >With arguments_There is a return value() STARTED
ClientTest >With arguments_There is a return value() PASSED
ClientTest >Exception occurred() STARTED
ClientTest >Exception occurred() PASSED
ClientTest >Sequential call() STARTED
ClientTest >Sequential call() PASSED
BUILD SUCCESSFUL in 2s
3 actionable tasks: 3 executed
Recommended Posts