Wednesday, 26 July 2017

Whitebox: Invoke the constructor of a class

Whitebox class provides 'invokeConstructor' method, it is used to invoke the constructor of a class and get an instance.

Following are the overloaded forms of invokeConstructor method.

public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes, Object[] arguments) throws Exception
public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Object... arguments) throws Exception

Above methods are useful for testing classes with a private constructor.

Following statements are used to invoke the constructor of a class StringUtil.
StringUtil stringUtil = Whitebox.invokeConstructor(StringUtil.class, "madam");

Find the following working application.

StringUtil.java
package com.sample.util;

public class StringUtil {
 private String str;
 
 private StringUtil(String str){
  this.str = str;
 }

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

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

}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class StringUtilTest {

 @Test
 public void isStringPalindrome_palindrome_true() throws Exception{
  StringUtil stringUtil = Whitebox.invokeConstructor(StringUtil.class, "madam");
  assertTrue(stringUtil.isStringPalindrome());
 }
 
 @Test
 public void isStringPalindrome_palindrome_false() throws Exception{
  StringUtil stringUtil = Whitebox.invokeConstructor(StringUtil.class, "madamaa");
  assertFalse(stringUtil.isStringPalindrome());
 }
}


Previous                                                 Next                                                 Home

Whitebox: invoke static method

In my previous post, I explained how to invoke instance methods of an object. In this post, I am going to explain how to invoke static methods of a class.

Whitebox class provides the method 'invokeMethod', it is used to invoke the methods of class. Following are the overloaded form of 'invokeMethod'.

public static synchronized <T> T invokeMethod(Object instance, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Class<?> klass, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Object... arguments)throws Exception
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Class<?> definedIn, Class<?>[] argumentTypes, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object instance, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute, Class<?>[] parameterTypes, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments) throws Exception

Following snippet is used to call the static method ‘isStringPalindrome’ and pass the argument ‘madam’ to the method.

boolean isPalindrome = Whitebox.invokeMethod(StringUtil.class, "isStringPalindrome", "madam");

Following is the 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());
 }

}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.*;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class StringUtilTest {

 @Test
 public void isStringPalindrome_palindrome_true() throws Exception{
  boolean isPalindrome = Whitebox.invokeMethod(StringUtil.class, "isStringPalindrome", "madam");
  assertTrue(isPalindrome);
 }
 
 @Test
 public void isStringPalindrome_palindrome_false() throws Exception{
  boolean isPalindrome = Whitebox.invokeMethod(StringUtil.class, "isStringPalindrome", "Hello");
  assertFalse(isPalindrome);
 }
}




Previous                                                 Next                                                 Home

Whitebox: invoke instance method

Whitebox class provides the method 'invokeMethod', it is used to invoke the methods of an object (or) class. Following are the overloaded form of 'invokeMethod'.

public static synchronized <T> T invokeMethod(Object instance, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Class<?> klass, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Object... arguments)throws Exception
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Class<?> definedIn, Class<?>[] argumentTypes, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object instance, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute, Class<?>[] parameterTypes, Object... arguments) throws Exception
public static synchronized <T> T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments) throws Exception

Following snippet is used to call the method ‘isStringPalindrome’ and pass the argument ‘madam’ to the method.

boolean isPalindrome = Whitebox.invokeMethod(util, "isStringPalindrome", "madam");

Find the following 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());
 }

}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.*;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class StringUtilTest {
 private StringUtil util = new StringUtil();

 @Test
 public void isStringPalindrome_palindrome_true() throws Exception{
  boolean isPalindrome = Whitebox.invokeMethod(util, "isStringPalindrome", "madam");
  assertTrue(isPalindrome);
 }
 
 @Test
 public void isStringPalindrome_palindrome_false() throws Exception{
  boolean isPalindrome = Whitebox.invokeMethod(util, "isStringPalindrome", "Hello");
  assertFalse(isPalindrome);
 }
}




Previous                                                 Next                                                 Home

Whitebox: get the value of static property

In my previous post, I explained how to get the value of instance property. In this post, I am going to explain how to get the value of static property.

Whitebox class provides method 'getInternalState' to get the state of instance property.

public static <T> T getInternalState(Object object, String fieldName)
public static <T> T getInternalState(Object object, String fieldName, Class<?> where)
public static <T> T getInternalState(Object object, Class<T> fieldType)
public static <T> T getInternalState(Object object, Class<T> fieldType, Class<?> where)

Following snippet is used to get the value associated static property ‘count’.

int count = Whitebox.getInternalState(Employee.class, "count");
assertEquals(count, 1);

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private static int count = 0;

 public Employee(int id, String firstName, String lastName) {
  count++;
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

}


EmployeeTest.java
package com.sample.model;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class EmployeeTest {

 @Test
 public void Employee_DefaultConstructor() {

  new Employee(1, "Hari Krishna", "Gurram");

  int count = Whitebox.getInternalState(Employee.class, "count");
  assertEquals(count, 1);

  new Employee(1, "Hari Krishna", "Gurram");
  count = Whitebox.getInternalState(Employee.class, "count");
  assertEquals(count, 2);

  new Employee(1, "Hari Krishna", "Gurram");
  count = Whitebox.getInternalState(Employee.class, "count");
  assertEquals(count, 3);

 }

}




Previous                                                 Next                                                 Home

Whitebox: get the value of instance property

Whitebox class provides method 'getInternalState' to get the state of instance property.

public static <T> T getInternalState(Object object, String fieldName)
public static <T> T getInternalState(Object object, String fieldName, Class<?> where)
public static <T> T getInternalState(Object object, Class<T> fieldType)
public static <T> T getInternalState(Object object, Class<T> fieldType, Class<?> where)

Following snippet is used to get the values associated with properties id, firstName and lastName respectively.

int id = Whitebox.getInternalState(emp, "id");
String firstName = Whitebox.getInternalState(emp, "firstName");
String lastName = Whitebox.getInternalState(emp, "lastName");
                
Find the following working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee(int id, String firstName, String lastName) {
  super();
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

}


EmployeeTest.java
package com.sample.model;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class EmployeeTest {

 @Test
 public void Employee_DefaultConstructor() {

  Employee emp = new Employee(1, "Hari Krishna", "Gurram");
  
  int id = Whitebox.getInternalState(emp, "id");
  String firstName = Whitebox.getInternalState(emp, "firstName");
  String lastName = Whitebox.getInternalState(emp, "lastName");
  
  assertEquals(id, 1);
  assertEquals(firstName, "Hari Krishna");
  assertEquals(lastName, "Gurram");
  
 }

}



Previous                                                 Next                                                 Home

Whitebox: initialize static properties

In my previous post, I explained how to set the instance properties of an object. In this post, I am going to explain how to set the static properties of a class using Whitebox.

Following snippet is used to set the static property 'count' of class Employee.
Whitebox.setInternalState(Employee.class, "count", 1123);

Find the following working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private static int count = 0;

 public Employee() {
  
 }

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public static int getCount() {
  return count;
 }

}


EmployeeTest.java
package com.sample.model;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class EmployeeTest {

 @Test
 public void Employee_DefaultConstructor() {

  Whitebox.setInternalState(Employee.class, "count", 1);
  assertEquals(Employee.getCount(), 1);
  
  Whitebox.setInternalState(Employee.class, "count", 1123);
  assertEquals(Employee.getCount(), 1123);
 }

}




Previous                                                 Next                                                 Home

Whitebox: initialize properties of an object

Whitebox provides following methods to set the value to a field.

public static void setInternalState(Object object, String fieldName, Object value)
public static void setInternalState(Object object, String fieldName, Object[] value)
public static void setInternalState(Object object, Object value, Object... additionalValues)
public static void setInternalState(Object object, Object value, Class<?> where)
public static void setInternalState(Object object, String fieldName, Object value, Class<?> where)
public static void setInternalState(Object object, Class<?> fieldType, Object value)
public static void setInternalState(Object object, Class<?> fieldType, Object value, Class<?> where)

Following snippet define the properties of the object 'Employee'.

Employee emp = new Employee();
                
Whitebox.setInternalState(emp, "id", 1234);
Whitebox.setInternalState(emp, "firstName", "Hari Krishna");
Whitebox.setInternalState(emp, "lastName", "Gurram");

Find the following working application.


Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee() {

 }

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

}


EmployeeTest.java
package com.sample.model;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.InvocationTargetException;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class EmployeeTest {

 @Test
 public void Employee_DefaultConstructor()
   throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  Employee emp = new Employee();
  
  Whitebox.setInternalState(emp, "id", 1234);
  Whitebox.setInternalState(emp, "firstName", "Hari Krishna");
  Whitebox.setInternalState(emp, "lastName", "Gurram");
  
  assertproperties(emp, 1234, "Hari Krishna", "Gurram");
 }
 
 
 private void assertproperties(Employee emp, int id, String firstName, String lastName) {

  assertEquals(emp.getId(), id);
  assertEquals(emp.getFirstName(), firstName);
  assertEquals(emp.getLastName(), lastName);

 }

}





Previous                                                 Next                                                 Home

Whitebox: access constructors

Whitebox class provides 'getConstructor' method, it is used to get the declared constructor of given class.

public static <T> Constructor<T> getConstructor(Class<?> type, Class<?>... parameterTypes)
type : The type of the class where the constructor is located.
parameterTypes : All parameter types of the constructor, value is null incase of default constructor.

For example, following snippet is used to create an object using default constructor.
Constructor<Employee> constructor = Whitebox.getConstructor(Employee.class);
Employee emp = constructor.newInstance();

Following snippet is used to create an object using constructor which takes an integer argument.
Constructor<Employee> constructor = Whitebox.getConstructor(Employee.class, int.class);
Employee emp = constructor.newInstance(1234);

Find the following working application.

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee() {
  this(-1);
 }

 public Employee(int id) {
  this(id, "no_name");
 }

 public Employee(int id, String firstName) {
  this(id, firstName, "no_name");
 }

 public Employee(String firstName, String lastName) {
  this(-1, firstName, lastName);
 }

 public Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public int getId() {
  return id;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return lastName;
 }

}


EmployeeTest.java
package com.sample.model;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.junit.Test;
import org.powermock.reflect.Whitebox;

public class EmployeeTest {
 private static String NO_NAME = "no_name";

 @Test
 public void Employee_DefaultConstructor()
   throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  Constructor<Employee> constructor = Whitebox.getConstructor(Employee.class);

  Employee emp = constructor.newInstance();
  assertproperties(emp, -1, NO_NAME, NO_NAME);
 }
 
 @Test
 public void Employee_ParameterizedConstructor_OnlyId() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
  Constructor<Employee> constructor = Whitebox.getConstructor(Employee.class, int.class);
  Employee emp = constructor.newInstance(1234);
  assertproperties(emp, 1234, NO_NAME, NO_NAME);
  
 }
 
 @Test
 public void Employee_ParameterizedConstructor_OnlyIdAndFirstName() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
  Constructor<Employee> constructor = Whitebox.getConstructor(Employee.class, int.class, String.class);
  Employee emp = constructor.newInstance(1234, "Hari krishna");
  assertproperties(emp, 1234, "Hari krishna", NO_NAME);
  
 }
 
 @Test
 public void Employee_ParameterizedConstructor_IdAndFirstNameAndLastName() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
  Constructor<Employee> constructor = Whitebox.getConstructor(Employee.class, int.class, String.class, String.class);
  Employee emp = constructor.newInstance(1234, "Hari krishna", "Gurram");
  assertproperties(emp, 1234, "Hari krishna", "Gurram");
  
 }

 private void assertproperties(Employee emp, int id, String firstName, String lastName) {

  assertEquals(emp.getId(), id);
  assertEquals(emp.getFirstName(), firstName);
  assertEquals(emp.getLastName(), lastName);

 }

}




Previous                                                 Next                                                 Home