Junit Private Methods

๐๐ป๐๐ป๐๐ป ALL INFORMATION CLICK HERE ๐๐ป๐๐ป๐๐ป
Home Java Code Quality Junit How to test Private Methods using Junit 5
Author: Soumitra Hi, I am, Soumitra, the owner of roytuts.com and it is my passion for sharing my knowledge and I have been writing blogs on various technologies since 2014. If you like my tutorials, you may also want to like my Facebook Page , follow me on Twitter , Github .
Here in this tutorial I will show you an example on how to test private methods using Junit framework. I am using Junit 5 framework to test the private method. Using Mockito framework you wonโt be able to test private methods, but using PowerMock core API you will be able to test the private methods. You can also use Javaโs Reflection API to test private methods. You can use Spring frameworkโs ReflectionTestUtils to test your private methods.
You generally donโt want to unit test private methods directly. Since they are private, you would consider them to call from a public method. If the methods that call your private methods are working as you expect, you then assume by extension that your private methods are working correctly. But still I will show you here how to unit test your private methods in Java and Spring applications.
Java at least 8, Junit 5, PowerMock Core, Mockito, Spring Boot Starter Test
For Java based applications you need to configure the following build file for maven based project:
First I am going to test private method in Java based application. Consider the following class where I have created a private method called pvMethod() .
Note important thing is that if you are using Javaโs reflection API to test your private method then arguments to the private methods need to be of object class types instead of primitive types. For example, I have used Integer instead of int.
Next I am going to create the following Junit test class to test the above private method.
To create the mock instance of the PrivateMethodApp, you can use either @Mock or @InjectMocks annotation.
Finally I have created two methods and annotated with @Test annotation to test the private method. testPrivateMethodUsingReflection() โ tests using Javaโs reflection API and make sure you need to have the class types for the method arguments. testPrivateMethodUsingPowerMock() โ tests using PowerMock API and you can have class or primitive types for method arguments.
Executing the above test class will give you the expected output:
Now I will show you how to test private method using Springโs ReflectionTestUtils API.
Now you can add the Spring Boot starter and starter test dependencies into the pom.xml file.
Next I am going to add new test method in the PrivateMethodTest class to test private method using ReflectionTestUtils.
Executing the above test class will give you the following output:
Thatโs an idea how to test your private methods in Java and Spring applications.
Your email address will not be published. Required fields are marked *
Copyright ยฉ 2014 - 2021 Roy Tutorials
Junit private methods: How to unit test private methods and classes
package com . learnbestcoding . junit ;
import java . util . ArrayList ;
import java . util . List ;
public class Util
{
public void runMethods ( String [ ] args )
{
//Public method uses all private methods
}
private static int getNumber ( )
{
return 1 ;
}
private List < String > getLanguages ( )
{
List languages = new ArrayList < String > ( ) ;
languages . add ( "Java" ) ;
languages . add ( "Sql" ) ;
languages . add ( "Javascript" ) ;
return languages ;
}
private void addLanguage ( List languages , String language )
{
languages . add ( language ) ;
}
private ProgrammingLanguages getJavaLanguageClass ( )
{
ProgrammingLanguages languages = new ProgrammingLanguages ( ) ;
languages . setLanguageId ( 1 ) ;
languages . setLanguageName ( "Java" ) ;
languages . setNoOfEnrolments ( 100 ) ;
return languages ;
}
private ProgrammingLanguages createLanguageClass ( int languageId , String languageName , int noOfEnrolments )
{
ProgrammingLanguages languages = new ProgrammingLanguages ( ) ;
languages . setLanguageId ( languageId ) ;
languages . setLanguageName ( languageName ) ;
languages . setNoOfEnrolments ( noOfEnrolments ) ;
return languages ;
}
private List < ProgrammingLanguages > addLanguageClass ( ProgrammingLanguages language )
{
List < ProgrammingLanguages > languageList = new ArrayList < ProgrammingLanguages > ( ) ;
languageList . add ( language ) ;
return languageList ;
}
class ProgrammingLanguages {
int languageId ;
String languageName ;
int noOfEnrolments ;
public int getLanguageId ( ) {
return languageId ;
}
public void setLanguageId ( int languageId ) {
this . languageId = languageId ;
}
public String getLanguageName ( ) {
return languageName ;
}
public void setLanguageName ( String languageName ) {
this . languageName = languageName ;
}
public int getNoOfEnrolments ( ) {
return noOfEnrolments ;
}
public void setNoOfEnrolments ( int noOfEnrolments ) {
this . noOfEnrolments = noOfEnrolments ;
}
}
}
< 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 http://maven.apache.org/xsd/maven-4.0.0.xsd " >
< modelVersion > 4.0.0
< groupId > com.learnbestcoding
< artifactId > junit
< version > 0.0.1-SNAPSHOT
< packaging > jar
< name > junit
< url > http://maven.apache.org
< properties >
< project.build.sourceEncoding > UTF-8
< dependencies >
< dependency >
< groupId > junit
< artifactId > junit
< version > 4.13.2
< dependency >
< groupId > org.powermock
< artifactId > powermock-api-mockito
< version > 1.7.4
< dependency >
< groupId > org.powermock
< artifactId > powermock-module-junit4
< version > 1.7.4
package com . learnbestcoding . junit ;
import static org . junit . Assert . assertEquals ;
import java . lang . reflect . Constructor ;
import java . lang . reflect . Field ;
import java . util . ArrayList ;
import java . util . List ;
import org . junit . Test ;
import org . junit . runner . RunWith ;
import org . powermock . modules . junit4 . PowerMockRunner ;
import org . powermock . reflect . Whitebox ;
@RunWith ( PowerMockRunner . class )
public class UtilTest {
@Test
public void test_getNumber ( ) throws Exception
{
int obj = Whitebox . invokeMethod ( new Util ( ) , "getNumber" ) ;
assertEquals ( obj , 1 ) ;
}
}
@Test
public void test_getLanguages ( ) throws Exception
{
List obj = Whitebox . invokeMethod ( new Util ( ) , "getLanguages" ) ;
assertEquals ( obj . get ( 0 ) , "Java" ) ;
}
@Test
public void test_addLanguage ( ) throws Exception
{
List languages = new ArrayList < String > ( ) ;
languages . add ( "Java" ) ;
languages . add ( "Sql" ) ;
languages . add ( "Javascript" ) ;
Whitebox . invokeMethod ( new Util ( ) , "addLanguage" , languages , "Python" ) ;
assertEquals ( languages . get ( 3 ) , "Python" ) ;
}
@Test
public void test_getLanguageClass ( ) throws Exception
{
Object obj = Whitebox . invokeMethod ( new Util ( ) , "getJavaLanguageClass" ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageId" ) . get ( obj ) , 1 ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageName" ) . get ( obj ) , "Java" ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) . get ( obj ) , 100 ) ;
}
@Test
public void test_createLanguageClass ( ) throws Exception
{
Object obj = Whitebox . invokeMethod ( new Util ( ) , "createLanguageClass" , 2 , "Sql" , 240 ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageId" ) . get ( obj ) , 2 ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageName" ) . get ( obj ) , "Sql" ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) . get ( obj ) , 240 ) ;
}
@Test
public void test_addLanguageClass ( ) throws Exception
{
Object programmingLanguages = new Object ( ) ;
Class clazz = Whitebox . getInnerClassType ( Util . class , "ProgrammingLanguages" ) ;
Constructor constructor = Whitebox . getConstructor ( clazz , Util . class ) ;
programmingLanguages = constructor . newInstance ( new Util ( ) ) ;
Field field = null ;
field = programmingLanguages . getClass ( ) . getDeclaredField ( "languageId" ) ;
field . setAccessible ( true ) ;
field . set ( programmingLanguages , 1 ) ;
field = programmingLanguages . getClass ( ) . getDeclaredField ( "languageName" ) ;
field . setAccessible ( true ) ;
field . set ( programmingLanguages , "Java" ) ;
field = programmingLanguages . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) ;
field . setAccessible ( true ) ;
field . set ( programmingLanguages , 100 ) ;
List obj = Whitebox . invokeMethod ( new Util ( ) , "addLanguageClass" , programmingLanguages ) ;
assertEquals ( obj . get ( 0 ) . getClass ( ) . getDeclaredField ( "languageId" ) . get ( obj . get ( 0 ) ) , 1 ) ;
assertEquals ( obj . get ( 0 ) . getClass ( ) . getDeclaredField ( "languageName" ) . get ( obj . get ( 0 ) ) , "Java" ) ;
assertEquals ( obj . get ( 0 ) . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) . get ( obj . get ( 0 ) ) , 100 ) ;
}
package com . learnbestcoding . junit ;
import static org . junit . Assert . assertEquals ;
import java . lang . reflect . Constructor ;
import java . lang . reflect . Field ;
import java . util . ArrayList ;
import java . util . List ;
import org . junit . Test ;
import org . junit . runner . RunWith ;
import org . powermock . modules . junit4 . PowerMockRunner ;
import org . powermock . reflect . Whitebox ;
@RunWith ( PowerMockRunner . class )
public class UtilTest {
@Test
public void test_getNumber ( ) throws Exception
{
int obj = Whitebox . invokeMethod ( new Util ( ) , "getNumber" ) ;
assertEquals ( obj , 1 ) ;
}
@Test
public void test_getLanguages ( ) throws Exception
{
List obj = Whitebox . invokeMethod ( new Util ( ) , "getLanguages" ) ;
assertEquals ( obj . get ( 0 ) , "Java" ) ;
}
@Test
public void test_addLanguage ( ) throws Exception
{
List languages = new ArrayList < String > ( ) ;
languages . add ( "Java" ) ;
languages . add ( "Sql" ) ;
languages . add ( "Javascript" ) ;
Whitebox . invokeMethod ( new Util ( ) , "addLanguage" , languages , "Python" ) ;
assertEquals ( languages . get ( 3 ) , "Python" ) ;
}
@Test
public void test_getLanguageClass ( ) throws Exception
{
Object obj = Whitebox . invokeMethod ( new Util ( ) , "getJavaLanguageClass" ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageId" ) . get ( obj ) , 1 ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageName" ) . get ( obj ) , "Java" ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) . get ( obj ) , 100 ) ;
}
@Test
public void test_createLanguageClass ( ) throws Exception
{
Object obj = Whitebox . invokeMethod ( new Util ( ) , "createLanguageClass" , 2 , "Sql" , 240 ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageId" ) . get ( obj ) , 2 ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "languageName" ) . get ( obj ) , "Sql" ) ;
assertEquals ( obj . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) . get ( obj ) , 240 ) ;
}
@Test
public void test_addLanguageClass ( ) throws Exception
{
Object programmingLanguages = new Object ( ) ;
Class clazz = Whitebox . getInnerClassType ( Util . class , "ProgrammingLanguages" ) ;
Constructor constructor = Whitebox . getConstructor ( clazz , Util . class ) ;
programmingLanguages = constructor . newInstance ( new Util ( ) ) ;
Field field = null ;
field = programmingLanguages . getClass ( ) . getDeclaredField ( "languageId" ) ;
field . setAccessible ( true ) ;
field . set ( programmingLanguages , 1 ) ;
field = programmingLanguages . getClass ( ) . getDeclaredField ( "languageName" ) ;
field . setAccessible ( true ) ;
field . set ( programmingLanguages , "Java" ) ;
field = programmingLanguages . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) ;
field . setAccessible ( true ) ;
field . set ( programmingLanguages , 100 ) ;
List obj = Whitebox . invokeMethod ( new Util ( ) , "addLanguageClass" , programmingLanguages ) ;
assertEquals ( obj . get ( 0 ) . getClass ( ) . getDeclaredField ( "languageId" ) . get ( obj . get ( 0 ) ) , 1 ) ;
assertEquals ( obj . get ( 0 ) . getClass ( ) . getDeclaredField ( "languageName" ) . get ( obj . get ( 0 ) ) , "Java" ) ;
assertEquals ( obj . get ( 0 ) . getClass ( ) . getDeclaredField ( "noOfEnrolments" ) . get ( obj . get ( 0 ) ) , 100 ) ;
}
}
Lance is a software engineer with over 15 years of experience in full-stack software development.
โย Free: Subscribe to download this project
Powermock is an open-source Java framework. Powermock leverages the capabilities of frameworks like Mockito to create mock objects in unit testing.
Unit testing private methods and classes are possible by using Powermock's Whitebox. The Whitebox uses reflection to create inner class instances and
invoke private methods outside the class file they are declared.
If you have been researching how to unit test private methods and classes, you may have come across various debates about best practices.
I am confident that "Don't unit test private methods" is not the answer you want to find. If unit testing private methods and classes are inevitable for your situation,
this article explains how to test private methods and classes.
org.powermock.reflect.Whitebox provides various utilities for accessing the internals of a class. These utilities include instantiating private classes and
invoking private methods by using reflection. Whitebox is a utility intended for tests.
The below is the test candidate class with private methods and an inner class. Note that those methods and inner class are not accessible outside
the class they are defined.
In this tutorial, we will discuss six different scenarios to test private classes and methods. These scenarios include simple private methods with no arguments
to private methods that accept private class arguments and return private classes.
The private method getNumber() is a simple method without any arguments and returns 1 for every execution. The important thing is not the method logic
but how you invoke this method outside the method scope. The below code illustrates how to invoke a private method with no arguments and a simple return value.
The getNumber is the private method name in Util.java class and Whitebox.invokeMethod(new Util(),"getNumber") returns the method return value.
The private method getLanguages() accepts no arguments and returns an ArrayList. The getLanguages is the private method name in Util.java class and
Whitebox.invokeMethod(new Util(),"getLanguages") returns the method return value, which is an ArrayList.
The private method addLanguage() accepts two arguments and does not return a value. The addLanguage is the private method name in Util.java class,
and method arguments are passed along with the method call
Whitebox.invokeMethod(new Util(),"addLanguage",languages,"Python")
The changes are mane to the variable reference languages.
The private method getJavaLanguageClass() accepts no arguments and returns an inner class. Note that the test class has no access to the private method
and inner class. The getJavaLanguageClass is the private method name in Util.java class, and ProgrammingLanguages is the inner class the method returns.
Whitebox.invokeMethod(new Util(),"getJavaLanguageClass") returns the innerclass and we can access its methods
with obj.getClass().getDeclaredField("languageId").get(obj) . The languageId is the property name.
The private method createLanguageClass() accepts arguments and returns a private class. The createLanguageClass is the private method name in Util.java class,
and ProgrammingLanguages is the inner class the method returns. Whitebox.invokeMethod(new Util(),"createLanguageClass",2,"Sql",240) passes the arguments
to the private class and returns the return value of the private method. The return value is also a type of private class and the properties are accessible
with obj.getClass().getDeclaredField("languageId").get(obj) . The languageId is the class property name.
The private method addLanguageClass() accepts private class as arguments and returns a private class. The addLanguageClass is the private method name in
Util.java class, and ProgrammingLanguages is the inner class the method returns. Here is how to implement it.
Powermock offers Whitebox utility offers utilities to unit test private methods and private classes using reflection. Whitebox makes private methods and classes
accessible outside their defined scope and visibility.
https://roytuts.com/how-to-test-private-methods-using-junit-5/
https://www.learnbestcoding.com/post/21/unit-test-private-methods-and-classes
Ptaf Boss Ass
Beast Tube Porn
Apocalyptica Hole In My Soul
How to test Private Methods using Junit 5 - Roy Tutorials
Junit private methods: How to unit test private methods ...
How do you write a JUnit test for private methods?
How to JUnit Test a Private Method - cjwebb.com
Test private methods using Junit5 | by Chanaka ...
JUnit test and mock private methods with PowerMock ...
Junit Test of private methods - codesd.com
How to Unit test private methods in Java and Kotlin | by ...
Junit Private Methods




















































































.png)

