PowerMock : How to test a private method

2015-04-23 540 words 3 mins read

“I HAVE THE POWER!!” – I had this feeling a few days ago. I will be honest that at work I do not get time to write unit test cases for each and every piece of code that I write. Often when I do have time, I make an effort to write test cases even for the trivial piece of code blocks such as — Check if properties file is present.

I was working on new code where I had the luxury to write the code in peace (a rarity at my work place where every project is like a fire drill). While writing test cases I came across a situation where I had a class with two methods:

[java]

public void my\_public\_method()

private void my\_private\_method()
  
[/java]

 

I wanted to write test cases for both the method. However Junit would not allow me to write a test case for a private method. I searched over internet forums and every one suggested that I use [**Java Reflection API**][1]  to write my test cases or make my private method public, which I did not want to do.

That’s when [**POWERMOCK**][2] steps in and in a tiny little section of its documentation I noticed a piece of “**WhiteboxImpl” ** class which can help me test private methods.

So that’s what I am going to demonstrate in this tutorial.

**STEP 1: Add Maven jar files**

[xml]
  

      
relative/svn/path
        
1.6.2
    

   

  
……..
        

            
junit
            
junit
            
4.11
            
test
        

        

            
org.powermock
            
powermock-module-junit4
            
${powermock.version}
            
test
        

        

            
org.powermock
            
powermock-api-easymock
            
${powermock.version}
            
test
        

   

  
[/xml]

**STEP 2: Create a class MyClass.java**

 

[java]
public class MyClass {
      
//PUBLIC METHOD
      
public String my\_public\_method(){
          
String msg="This is my PUBLIC method";
          
System.out.println(msg);
          
return msg;
      
}

//PRIVATE METHOD
      
private String my\_private\_method(){
          
String msg="This is my PRIVATE method";
          
System.out.println(msg);
          
return msg;
      
}
  
}
  
[/java]



STEP 3: Write a test case for public method : my_public_method

[java]

import org.junit.Assert;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.powermock.core.classloader.annotations.PrepareForTest;

import org.powermock.modules.junit4.PowerMockRunner;

import org.powermock.reflect.internal.WhiteboxImpl;

@RunWith(PowerMockRunner.class)

@PrepareForTest(MyClass.class)

public class MyClassTest {

final String publicMsg = “This is my PUBLIC method”;

final String privateMsg = “This is my PRIVATE method”;

@Test

public void testMy_public_method() throws Exception {

MyClass myClass = new MyClass();

String msg=myClass.my_public_method();

Assert.assertEquals(publicMsg,msg);

}

}

[/java]

As you can see above that there is no issue with calling a public method and it will run successfully but when you try and call the private method, the code will show error that private method is not visible.

STEP 4: Use PowerMock’s WhiteboxImpl class to test a private method.

Before you do anything you need to make sure that you added Powermock annotations correctly.

  • Add these two annotations to your class.

[java]

@RunWith(PowerMockRunner.class)

@PrepareForTest(MyClass.class)

[/java]

  • Write the code to test private method.

[java]

@Test

public void testMy_private_method() throws Exception {

MyClass myClass = new MyClass();

String msg= WhiteboxImpl.invokeMethod(myClass, “my_private_method”);

Assert.assertEquals(privateMsg,msg);

}

[/java]

The syntax is pretty simple WhiteboxImpl.invokeMethod(, “,input param1, input param2,…);

The WhiteBoxImpl class actually uses “Java Reflection API” in the background to make a call, but for the lazy coders like me, who do not want to write Reflection API(Read hate Reflection API), the WhiteBoxImpl class is a small piece of coding heaven.

Now run the test class and you will see that test cases have passed.

~Ciao –Repeat the mantra – “I HAVE THE POWER{MOCK}!!!”

 



                 
                
         
      
    

author

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now