Wednesday, 26 July 2017

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

No comments:

Post a Comment