CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-powermock--powermock-api-easymock

PowerMock API extension for EasyMock providing advanced mocking capabilities for static methods, constructors, final classes, and private methods through bytecode manipulation.

Pending
Overview
Eval results
Files

reflection.mddocs/

Reflection and Internal State Access

PowerMock's Whitebox utility provides comprehensive reflection capabilities for testing internal object state and behavior. These utilities enable access to private fields, methods, and constructors without modifying production code.

Capabilities

Field Access and Manipulation

Get and set field values regardless of visibility modifiers.

/**
 * Get the value of a field (including private fields)
 * @param object the object containing the field
 * @param fieldName the name of the field
 * @return the field value
 */
public static <T> T getInternalState(Object object, String fieldName);

/**
 * Get the value of a field by type
 * @param object the object containing the field
 * @param fieldType the type of the field
 * @return the field value
 */
public static <T> T getInternalState(Object object, Class<T> fieldType);

/**
 * Get the value of a field from a specific class in the hierarchy
 * @param object the object containing the field
 * @param fieldName the name of the field
 * @param where the class that declares the field
 * @return the field value
 */
public static <T> T getInternalState(Object object, String fieldName, Class<?> where);

/**
 * Get the value of a field by type from a specific class in the hierarchy
 * @param object the object containing the field
 * @param fieldType the type of the field
 * @param where the class that declares the field
 * @return the field value
 */
public static <T> T getInternalState(Object object, Class<T> fieldType, Class<?> where);

/**
 * Set the value of a field (including private fields)
 * @param object the object containing the field
 * @param fieldName the name of the field
 * @param value the value to set
 */
public static void setInternalState(Object object, String fieldName, Object value);

/**
 * Set the value of multiple fields at once
 * @param object the object containing the fields
 * @param value the first value to set
 * @param additionalValues additional values to set
 */
public static void setInternalState(Object object, Object value, Object... additionalValues);

/**
 * Set the value of a field by type
 * @param object the object containing the field
 * @param fieldType the type of the field
 * @param value the value to set
 */
public static void setInternalState(Object object, Class<?> fieldType, Object value);

/**
 * Set the value of a field in a specific class in the hierarchy
 * @param object the object containing the field
 * @param fieldName the name of the field
 * @param value the value to set
 * @param where the class that declares the field
 */
public static void setInternalState(Object object, String fieldName, Object value, Class<?> where);

/**
 * Set the value of a field by type in a specific class in the hierarchy
 * @param object the object containing the field
 * @param fieldType the type of the field
 * @param value the value to set
 * @param where the class that declares the field
 */
public static void setInternalState(Object object, Class<?> fieldType, Object value, Class<?> where);

Method Invocation

Invoke methods including private methods with support for parameter type resolution.

/**
 * Invoke a method with automatic method resolution
 * @param instance the object instance
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Object instance, Object... arguments) throws Exception;

/**
 * Invoke a method by name
 * @param instance the object instance
 * @param methodToExecute the method name
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Object... arguments) throws Exception;

/**
 * Invoke a method with explicit parameter types
 * @param instance the object instance
 * @param methodToExecute the method name
 * @param argumentTypes the parameter types
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Object instance, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception;

/**
 * Invoke a method in a specific declaring class
 * @param instance the object instance
 * @param declaringClass the class that declares the method
 * @param methodToExecute the method name
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Object instance, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception;

/**
 * Invoke a method with full parameter specification
 * @param object the object instance
 * @param declaringClass the class that declares the method
 * @param methodToExecute the method name
 * @param parameterTypes the parameter types
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute, Class<?>[] parameterTypes, Object... arguments) throws Exception;

/**
 * Invoke a static method with automatic method resolution
 * @param klass the class containing the static method
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Class<?> klass, Object... arguments) throws Exception;

/**
 * Invoke a static method by name
 * @param clazz the class containing the static method
 * @param methodToExecute the method name
 * @param arguments the method arguments
 * @return the method return value
 */
public static synchronized <T> T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments) throws Exception;

Constructor Invocation

Create objects using private or complex constructors.

/**
 * Create an instance using constructor arguments
 * @param classThatContainsTheConstructorToTest the class to instantiate
 * @param arguments the constructor arguments
 * @return the new instance
 */
public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Object... arguments) throws Exception;

/**
 * Create an instance with explicit parameter types
 * @param classThatContainsTheConstructorToTest the class to instantiate
 * @param parameterTypes the constructor parameter types
 * @param arguments the constructor arguments
 * @return the new instance
 */
public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes, Object[] arguments) throws Exception;

/**
 * Create an instance using the default constructor
 * @param classToInstantiate the class to instantiate
 * @return the new instance
 */
public static <T> T newInstance(Class<T> classToInstantiate);

Field and Method Discovery

Access reflection metadata for fields, methods, and constructors.

/**
 * Get a field by name
 * @param type the class containing the field
 * @param fieldName the field name
 * @return the Field object
 */
public static Field getField(Class<?> type, String fieldName);

/**
 * Get multiple fields by name
 * @param clazz the class containing the fields
 * @param fieldNames the field names
 * @return array of Field objects
 */
public static Field[] getFields(Class<?> clazz, String... fieldNames);

/**
 * Get all instance fields
 * @param object the object
 * @return set of all instance fields
 */
public static Set<Field> getAllInstanceFields(Object object);

/**
 * Get all static fields
 * @param type the class
 * @return set of all static fields
 */
public static Set<Field> getAllStaticFields(Class<?> type);

/**
 * Get fields of a specific type
 * @param object the object
 * @param type the field type
 * @return set of fields of the specified type
 */
public static Set<Field> getFieldsOfType(Object object, Class<?> type);

/**
 * Get fields annotated with specific annotations
 * @param object the object
 * @param annotation the annotation type
 * @param additionalAnnotations additional annotation types
 * @return set of annotated fields
 */
public static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation> annotation, Class<? extends Annotation>... additionalAnnotations);

/**
 * Get a method by name and parameter types
 * @param type the class containing the method
 * @param methodName the method name
 * @param parameterTypes the parameter types
 * @return the Method object
 */
public static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes);

/**
 * Get a method by parameter types only
 * @param type the class containing the method
 * @param parameterTypes the parameter types
 * @return the Method object
 */
public static Method getMethod(Class<?> type, Class<?>... parameterTypes);

/**
 * Get multiple methods by name
 * @param clazz the class containing the methods  
 * @param methodNames the method names
 * @return array of Method objects
 */
public static Method[] getMethods(Class<?> clazz, String... methodNames);

/**
 * Get a constructor by parameter types
 * @param type the class containing the constructor
 * @param parameterTypes the parameter types
 * @return the Constructor object
 */
public static <T> Constructor<T> getConstructor(Class<?> type, Class<?>... parameterTypes);

/**
 * Get the first parent constructor
 * @param klass the class
 * @return the first parent constructor
 */
public static Constructor<?> getFirstParentConstructor(Class<?> klass);

Context-Based Field Setting

Set multiple fields using context objects for dependency injection scenarios.

/**
 * Set internal state from context objects
 * @param instance the target object
 * @param context the context object providing values
 * @param additionalContexts additional context objects
 */
public static void setInternalStateFromContext(Object instance, Object context, Object... additionalContexts);

/**
 * Set internal state from context classes
 * @param classOrInstance the target object or class
 * @param context the context class providing values
 * @param additionalContexts additional context classes
 */
public static void setInternalStateFromContext(Object classOrInstance, Class<?> context, Class<?>... additionalContexts);

/**
 * Set internal state from context with matching strategy
 * @param instance the target object
 * @param context the context object providing values
 * @param strategy the field matching strategy
 */
public static void setInternalStateFromContext(Object instance, Object context, FieldMatchingStrategy strategy);

/**
 * Set internal state from context class with matching strategy
 * @param instance the target object
 * @param context the context class providing values
 * @param strategy the field matching strategy
 */
public static void setInternalStateFromContext(Object instance, Class<?> context, FieldMatchingStrategy strategy);

Type Utilities

Access type information and work with inner classes.

/**
 * Get the type of an object
 * @param object the object
 * @return the object's type
 */
public static Class<?> getType(Object object);

/**
 * Get the unproxied type of an object
 * @param object the object (potentially proxied)
 * @return the original type
 */
public static Class<?> getUnproxyType(Object object);

/**
 * Get an inner class type by name
 * @param declaringClass the declaring class
 * @param name the inner class name
 * @return the inner class type
 */
public static Class<Object> getInnerClassType(Class<?> declaringClass, String name) throws ClassNotFoundException;

/**
 * Get a local class type by occurrence and name
 * @param declaringClass the declaring class
 * @param occurrence the occurrence number
 * @param name the local class name
 * @return the local class type
 */
public static Class<Object> getLocalClassType(Class<?> declaringClass, int occurrence, String name) throws ClassNotFoundException;

/**
 * Get an anonymous inner class type by occurrence
 * @param declaringClass the declaring class
 * @param occurrence the occurrence number
 * @return the anonymous class type
 */
public static Class<Object> getAnonymousInnerClassType(Class<?> declaringClass, int occurrence) throws ClassNotFoundException;

Usage Examples

Basic Field Access

// Get private field value
String value = Whitebox.getInternalState(myObject, "privateField");

// Set private field value
Whitebox.setInternalState(myObject, "privateField", "newValue");

// Set field by type
Whitebox.setInternalState(myObject, DatabaseConnection.class, mockConnection);

Private Method Invocation

// Invoke private method
String result = Whitebox.invokeMethod(myObject, "privateMethod", "param1", 123);

// Invoke private static method
int result = Whitebox.invokeMethod(MyClass.class, "privateStaticMethod", "param");

Constructor Testing

// Create instance with private constructor
MyClass instance = Whitebox.invokeConstructor(MyClass.class, "param1", 123);

// Create instance using default constructor
MyClass instance = Whitebox.newInstance(MyClass.class);

Context-Based Field Setting

// Set fields from context object (dependency injection testing)
TestContext context = new TestContext();
context.database = mockDatabase;
context.logger = mockLogger;

Whitebox.setInternalStateFromContext(serviceUnderTest, context);

Types

/**
 * Strategy for matching fields when setting internal state from context
 */
interface FieldMatchingStrategy {
    // Strategy implementations for field matching
}

Install with Tessl CLI

npx tessl i tessl/maven-org-powermock--powermock-api-easymock

docs

annotations.md

constructor-mocking.md

core-mocking.md

index.md

mock-control.md

partial-mocking.md

private-methods.md

reflection.md

static-mocking.md

tile.json