Wednesday 26 July 2017

Whitebox: test private instance methods

Whitebox class provides 'getMethod', by using this we can get access to a method reference of the class. Following is the signatures of overloaded forms of 'getMethod'.

public static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes)
public static Method getMethod(Class<?> type, Class<?>... parameterTypes)
type : The type of the class where the method is located.
methodName : The method name to get the reference
parameterTypes : All parameter types of the method, can be null if no parameters exist.

For example, following snippet is used to call the private method 'isStringPalindrome' of the class Whitebox.

StringUtil stringUtil = new StringUtil();
Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class);
boolean isPalindrome = (Boolean) method.invoke(stringUtil, "madam");

Find the following complete working application

StringUtil.java
package com.sample.util;

public class StringUtil {

 /**
  * 
  * @param str
  * @return true if the string is palindrome, else false.
  */
 private boolean isStringPalindrome(String str) {
  if (str == null || str.isEmpty()) {
   return true;
  }

  return str.equals(new StringBuilder(str).reverse().toString());
 }

 public boolean isValidPassword(String password) {
  if (password == null || password.isEmpty()) {
   return false;
  }

  if (password.length() < 8) {
   return false;
  }

  if (isStringPalindrome(password)) {
   return false;
  }

  return true;
 }
}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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.Whitebox;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ StringUtil.class })
public class StringUtilTest {

 @Test
 public void isStringPalindrome_empty_true()
   throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  StringUtil stringUtil = new StringUtil();

  Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class);

  boolean isPalindrome = (Boolean) method.invoke(stringUtil, "");

  assertTrue(isPalindrome);

 }
 
 @Test
 public void isStringPalindrome_Palindrome_true()
   throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  StringUtil stringUtil = new StringUtil();

  Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class);

  boolean isPalindrome = (Boolean) method.invoke(stringUtil, "madam");

  assertTrue(isPalindrome);

 }
 
 @Test
 public void isStringPalindrome_NotAPalindrome_true()
   throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  StringUtil stringUtil = new StringUtil();

  Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class);

  boolean isPalindrome = (Boolean) method.invoke(stringUtil, "madama");

  assertFalse(isPalindrome);

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment