Updated February 17, 2023
Introduction to JUnit Private Methods
JUnit private methods are tested using a version of junit as 5; by using the Mockito framework, we cannot test the private methods, but by using power mock API, we will be able to test the private methods. We can also use the reflection API of java to test the private methods in junit. We can also use the ReflectionTestUtils in the spring framework for testing private methods.
Overview of JUnit Private Methods
- We can test the junit private methods using powermock leverages, an open-source tool. The powermock is the capabilities framework of Mockito, which was used to create mock objects for unit testing.
- Unit testing methods and classes are possible using the white box of powermocks. Whitebox is used to create the inner class instances and also used to invoke the private methods.
- In general, we don’t require private methods in unit tests directly because it’s private; we would need to consider the same by using the public method. Suppose the method which calls private method as per working; then we can assume by extension that our private methods will work as expected.
- Given the method, access is also essential to test the private methods in junit. This approach works correctly, but it will contain a cost.
- At the time, private methods were made in java to prevent them, which is called from the owning class outside.
- For testing junit private methods, we need to work on java access controls. Furthermore, to test junit private methods, we need to add junit dependency in our project.
JUnit Private Methods Class
Below are steps shown to test the class of the junit private method as follows:
- We are creating the project name as JunitPrivateMethod. In this step, we create the project template of the junit private method in spring boot. We provide the project group name as com.example, artifact name as JunitPrivateMethod, project name as JunitPrivateMethod, and selected java version as 11. We are defining the version of spring boot as 2.7.0
Group – com.example Artifact name – JunitPrivateMethod
Name – JunitPrivateMethod Spring boot – 2.7.0
Project – Maven Java – 11
Package name – com.example. JunitPrivateMethod
Project Description – Project for JunitPrivateMethod
- In this step, we extract the downloaded project and open the same by using the spring tool suite.
- In this step, we check all the project structures and their files. Also, we are checking whether that pom.xml file is created or not. If this file is not created, we need to create the same manually. However, this file is created in the below example, so we do not need to create it manually.
- In this step, we add the junit dependency in the junit private method project. We are adding junit dependency.
Code:
<dependencies>
<dependency>
<groupId> org.junit.jupiter </groupId>
<artifactId> junit-jupiter-engine </artifactId>
<version> 5.8.0 </version>
<scope> junit- private-method </scope>
</dependency>
<dependency>
<groupId> org.mockito </groupId>
<artifactId> mockito.junit.jupiter </artifactId>
<version> 3.9.0 </version>
</dependency>
<dependency>
<groupId> org.powermock </groupId>
<artifactId> powermock-core </artifactId>
<version> 2.0.9 </version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-compiler-plugin </artifactId>
</plugin>
</plugins>
</reporting>
Output:
For the junit private method class, we are testing the following class. We are creating the class name as junitprivatemethod as follows. In the below example, we are creating multiple private methods.
Code:
public class junitprivatemethod {
private String strValue = null;
public junitprivatemethod (String value) {
this.strValue = (value == null) ? "" : value;
}
private junitprivatemethod () {
}
private boolean eq (String value) {
return this.strValue.equals (value);
}
private static boolean isemp (String value) {
return (Boolean) null;
}
private static void dmsg () {
System.out.println ("Junit private method");
}
}
Output:
- After creating the sample class, we call the non-static method under the test. The argument types of the private method are stored in order from the first argument and passed the same with the arguments. The type of arguments specified in the test failed on a side. The below example shows calling the non-static method.
Code:
public class Junit {
@Test
{
Object junit = null;
assertTrue((boolean) this.junit1 (junit, null, null, null));
}
private Object junit1(
Object obj, String name, Class[] types, Object[] args) throws Exception {
java.lang.reflect.Method method = obj.getClass ().getDeclaredMethod (name, types);
((AccessibleObject) method).canAccess (true);
return method.invoke (obj, args);
}
}
Output:
In the below example, we are calling the static method. We are using a private method for calling the static method as follows. By using the static method, we are not passing any object.
Code:
public class Junit {
@Test
{
assertTrue((boolean) this.junit1(
"test", new Class[]{String.class}, new Object[]{null}));
}
private Object junit1 (
String addr, Class[] types, Object[] args) throws Exception
{
java.lang.reflect.Method method = junitprivatemethod.class.getDeclaredMethod (addr, types);
method.setAccessible (true);
return method.invoke(null, args);
}
}
Output:
- In the below example, we return the value without arguments as follows. If the method under the test is void, it will not return any value. Therefore, it will pass null when calling the method without any arguments.
Code:
public class Junit {
@Test
{
this.junit1 ("Junit1", null, null);
}
}
Output:
- In the below example, we call the private constructor without arguments to prevent calling from the static class. This is used in projects where we require a condition that is 100%. In the below example, we describe a class with a zero origin; we have defined a private constructor with origin as one. Because we have defined the constructor in the second into the sample class. A zero value is fine if we have already created a constructor.
Code:
public class Junit {
@Test
public void con () {
try {
Class<?> clazz = Class.forName("junit private method");
Constructor<?>[] constructors = clazz.getDeclaredConstructors ();
constructors[1].setAccessible(true);
Object obj = constructors[1].newInstance ();
assertNotNull(obj);
} catch (Exception e) {
fail(e.getMessage ());
}
}
Output:
Conclusion
Unit testing methods and classes are possible using the white box of powermocks. For example, we can test the junit private methods using powermock leverages, an open-source tool. The powermock is the capabilities framework of Mockito, which was used to create mock objects for unit testing.
Recommended Articles
This is a guide to JUnit Private Methods. Here we discuss the introduction and JUnit private methods class for better understanding. You may also have a look at the following articles to learn more –