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); } }
No comments:
Post a Comment