In this post, I am going to explain how to mock a static method using junit5, and mockito-inline library.
Example
try (MockedStatic mocked = mockStatic(NameChecker.class)) {
String nameToTest = "Krishna";
mocked.when(() -> NameChecker.isValidName(nameToTest)).thenReturn(true);
}
Above snippet mock the method ‘NameChecker.isValidName’.
Follow below step-by-step procedure to build complete working application.
Step 1: Create new maven project ‘junit5-mockito-inline’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
modelVersion>4.0.0modelVersion>
groupId>com.sample.appgroupId>
artifactId>junit5-mockito-inlineartifactId>
version>1.0.0version>
properties>
junit.jupiter.version>5.9.1junit.jupiter.version>
mockito-inline.version>3.9.0mockito-inline.version>
mockito-core.version>4.6.1mockito-core.version>
java.version>1.8java.version>
maven.compiler.source>1.8maven.compiler.source>
maven.compiler.target>1.8maven.compiler.target>
properties>
build>
plugins>
plugin>
groupId>org.apache.maven.pluginsgroupId>
artifactId>maven-surefire-pluginartifactId>
version>3.0.0-M6version>
dependencies>
dependency>
groupId>org.junit.jupitergroupId>
artifactId>junit-jupiter-engineartifactId>
version>${junit.jupiter.version}version>
dependency>
dependencies>
plugin>
plugin>
groupId>org.apache.maven.pluginsgroupId>
artifactId>maven-release-pluginartifactId>
version>2.5.3version>
configuration>
localCheckout>truelocalCheckout>
autoVersionSubmodules>trueautoVersionSubmodules>
configuration>
plugin>
plugins>
build>
dependencies>
dependency>
groupId>org.junit.jupitergroupId>
artifactId>junit-jupiter-engineartifactId>
version>${junit.jupiter.version}version>
scope>testscope>
dependency>
dependency>
groupId>org.mockitogroupId>
artifactId>mockito-inlineartifactId>
version>${mockito-inline.version}version>
scope>testscope>
dependency>
dependencies>
project>
Step 3: Define utility classes.
NameChecker.java
package com.sample.app.util;
public class NameChecker {
public static boolean isValidName(String name) {
throw new RuntimeException("Functionality is not yet implements");
}
}
WelcomeUtil.java
package com.sample.app.util;
public class WelcomeUtil {
public static String sayHi(String name) {
if (NameChecker.isValidName(name)) {
return "Hi " + name;
}
throw new RuntimeException("Invalid name");
}
}
Step 4: Define WelcomeUtilUnitTest class.
WelcomeUtilUnitTest.java
package com.sample.app.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mockStatic;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
public class WelcomeUtilUnitTest {
@Test
public void sayHiTest() {
try (MockedStatic mocked = mockStatic(NameChecker.class)) {
String nameToTest = "Krishna";
mocked.when(() -> NameChecker.isValidName(nameToTest)).thenReturn(true);
String actualMsg = WelcomeUtil.sayHi(nameToTest);
String expected = "Hi " + nameToTest;
assertEquals(expected, actualMsg);
}
}
}
Run the test cases
Navigate to the package, where pom.xml is located and execute the command ‘mvn test’.bash-3.2$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------
[INFO] Building junit5-mockito-inline 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit5-mockito-inline ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit5-mockito-inline ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit5-mockito-inline ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit5-mockito-inline ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M6:test (default-test) @ junit5-mockito-inline ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.sample.app.util.WelcomeUtilUnitTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.649 s - in com.sample.app.util.WelcomeUtilUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.440 s
[INFO] Finished at: 2023-03-14T16:48:38+05:30
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "artifactory" could not be activated because it does not exist.
This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here