Verifying private method invocation

This post explains how to write an unit test case, to verify a Class’s private method is invoked.

We invoke a Class’s private method indirectly through the help of public method exposed by the class.

When writing unit test cases for public methods we want to make sure that private methods with expected arguments is called.

From the return value of the public method we cannot verify 100% that a private method with expected arguments is called.

With the help of PowerMock framework, we can verify 100% that a private method with expected arguments is called.

Class to be tested

1  package package4;
3  public class Class1 {
4   public void publicMethod(int value) {
5       this.privateMethod(value);
6   }
8   private void privateMethod(int value) {
9       int result = value * value;
10  }
11 }

Test Class

1  package package4;
3  import org.junit.Before;
4  import org.junit.Test;
5  import org.junit.runner.RunWith;
6  import org.powermock.api.mockito.PowerMockito;
7  import org.powermock.api.mockito.verification.PrivateMethodVerification;
8  import org.powermock.core.classloader.annotations.PrepareForTest;
9  import org.powermock.modules.junit4.PowerMockRunner;
11 @RunWith(PowerMockRunner.class)
12 @PrepareForTest(Class1.class)
13 public class Class1Test {
14  private Class1 class1;
16  @Before
17  public void setUp() {
18      class1 = new Class1();
19      class1 = PowerMockito.spy(class1);
20  }
22  @Test
23  public void testPrivateMethod() throws Exception {
24      class1.publicMethod(5);
25      PrivateMethodVerification privateMethodInvocation = PowerMockito.verifyPrivate(class1);
26      privateMethodInvocation.invoke("privateMethod", 5);
27  }
28 }


In the above code, we are verifying that “privateMethod” is called by “publicMethod” with argument 5.

In the setUp method, we create an instance of Class1 class and create a spy object of it using PowerMockito’s spy method. Refer to line 18 and 19.

In the “testPrivateMethod”, we first call the “publicMethod” passing 5 as the argument.

At line 25, we create an instance of PrivateMethodVerification class using the method verifyPrivate of PowerMockito.

verifyPrivate takes the instance of the class being tested as an argument.

Next we do verification by calling invoke method on privateMethodInvocation instance. This method takes 2 arguments
1) method name, which we have to verify was invoked by publicMethod
2) argument which has to be passed when the private method is invoked

If the private method is not invoked or private method is invoked with different argument the test case fails.

In this case, we are calling publicMethod with 5 as argument. So privateMethod is called with 5 as the argument. Where as we are testing that privateMethod is called with 6 as a argument. This mismatch between actual and expected argument fails the test case.

Below is screenshot of the output



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s