Tuesday 14 March 2023

Junit5, mockito-inline demo

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.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>junit5-mockito-inline</artifactId>
	<version>1.0.0</version>

	<properties>
		<junit.jupiter.version>5.9.1</junit.jupiter.version>
		<mockito-inline.version>3.9.0</mockito-inline.version>
		<mockito-core.version>4.6.1</mockito-core.version>

		<java.version>1.8</java.version>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M6</version>
				<dependencies>
					<dependency>
						<groupId>org.junit.jupiter</groupId>
						<artifactId>junit-jupiter-engine</artifactId>
						<version>${junit.jupiter.version}</version>
					</dependency>
				</dependencies>
			</plugin>


			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-release-plugin</artifactId>
				<version>2.5.3</version>
				<configuration>
					<localCheckout>true</localCheckout> <!-- must be true -->
					<autoVersionSubmodules>true</autoVersionSubmodules>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-inline</artifactId>
			<version>${mockito-inline.version}</version>
			<scope>test</scope>
		</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] ----------------< com.sample.app:junit5-mockito-inline >----------------
[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.


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment