In
my previous post, I explained how to test instance private methods of a class
using Whitebox class. In this post, I am going to explain how to test the
static private methods using Whitebox class.
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. If you pass any object as
an argument, it will be ignored in case of static methods.
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 test the static method
'isStringPalindrome'.
Method
method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome",
String.class);
boolean
isPalindrome = (Boolean) method.invoke(null, "");
assertTrue(isPalindrome);
Following
is the complete working application.
StringUtil.java
package com.sample.util; public class StringUtil { /** * * @param str * @return true if the string is palindrome, else false. */ private static 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.assertFalse; import static org.junit.Assert.assertTrue; 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 { Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class); boolean isPalindrome = (Boolean) method.invoke(null, ""); assertTrue(isPalindrome); } @Test public void isStringPalindrome_Palindrome_true() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class); boolean isPalindrome = (Boolean) method.invoke(null, "madam"); assertTrue(isPalindrome); } @Test public void isStringPalindrome_NotAPalindrome_true() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method method = Whitebox.getMethod(StringUtil.class, "isStringPalindrome", String.class); boolean isPalindrome = (Boolean) method.invoke(null, "madama"); assertFalse(isPalindrome); } }
No comments:
Post a Comment