EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials JUnit Tutorial JUnit Private Methods
Secondary Sidebar
JUnit Tutorial
  • Junit
    • JUnit 5 Assert
    • JUnit RunWith
    • JUnit AssertThat
    • JUnit Report
    • JUnit BeforeClass
    • JUnit Framework
    • JUnit Data Provider
    • JUnit Runner
    • JUnit 5 Parameterized Tests
    • JUnit BeforeAll
    • JUnit Private Methods
    • JUnit Integration Test
    • JUnit Maven Dependency
    • JUnit Annotations?
    • JUnit Testing
    • JUnit Test Order
    • JUnit 5 RunWith
    • JUnit 4 Maven
    • JUnit Jar
    • JUnit assertEquals
    • JUnit 5 Parameterized Test
    • JUnit Dependency
    • JUnit Fail
    • JUnit Disable Test
    • JUnit Assert
    • JUnit in Maven
    • JUnit 5 Gradle
    • JUnit XML
    • JUnit XML Format
    • JUnit Eclipse
    • JUnit Test Suite
    • JUnit Parameterized Test
    • JUnit assert exception
    • JUnit Code Coverage
    • JUnit Jupiter
    • JUnit Rule
    • JUnit version
    • Junit Interview Questions

JUnit Private Methods

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

JUnit 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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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

Spring Initializr

  • In this step, we extract the downloaded project and open the same by using the spring tool suite.

JUnit Private Methods - Import Projects from file system or Archive

  • 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.

JUnit Private Methods - pom.xml file is created or not

  • 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:

JUnit Private Methods - add the junit dependency

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:

JUnit Private Methods - class name as junitprivatemethod

  • 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:

we are calling the non-static method

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:

we are calling the static method

  • 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:

returning the value without arguments

  • 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:

JUnit Private Methods - call private constructor without arguments

JUnit Private Methods 10

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 –

  1. JUnit Code Coverage
  2. JUnit assertEquals
  3. JUnit Jupiter
  4. JUnit 5 Maven Dependency
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more