Class TestUtils
A primary motivating factor for creating this class is to gain access to some of the myriad functionality in TestCase without loading its static data.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object[]
A convenience method that can be statically imported to use with the class, allowing you to avoid your own ugly manual array creation.static Class<?>[]
A convenience method that can be statically imported to use with the class, allowing you to avoid your own ugly manual array creation.static String
Returns a string which is a printout of a stack trace for each thread running in the current JVMgetAllInstanceFields
(Object ownerInstance) Gets all fields of the given object.static Object
getInstanceField
(String fieldName, Object ownerInstance) Gets the instance field by the given name on the given object instance.static <T> T
getInstanceFieldByClassType
(Class<T> classType, Object ownerInstance) Get the first field object contained within object ownerInstance which has the type classType.static Object
invokeConstructor
(Class<?> containingClass, Class<?>[] parameterTypes, Object[] args) Uses reflection to execute the constructor for the given class with the given parameters.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance) This method is just a "pass through" method forinvokeInstanceMethod(String, Object, Class[], Object[])
so that callers do not need to pass null to that method when the underlying instance method does not have any parameters.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance, Class<?>[] parameterTypes, Object[] args) Uses reflection to execute the method denoted by the given method name.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance, Class<?> parameterType, Object arg) Uses reflection to execute the method denoted by the given method name.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance, Object... args) Uses reflection to execute the method denoted by the given method name.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance, List<Class<?>> parameterTypes, List<Object> args) Uses reflection to execute the method denoted by the given method name.static Field
locateFieldByTypeOnClass
(Class<?> classType, Class<?> containingClass) Get the first field specification contained within containingClass which has the type classType.static void
setInstanceField
(String fieldName, Object ownerInstance, Object value) Sets the instance field by the given name on the given object instance.
-
Method Details
-
createStackTraceForAllThreads
Returns a string which is a printout of a stack trace for each thread running in the current JVM- Returns:
- the stack trace string
-
setInstanceField
public static void setInstanceField(String fieldName, Object ownerInstance, Object value) throws RuntimeException Sets the instance field by the given name on the given object instance.Note: if the field is static, then the
ownerInstance
field can be the class of the object that contains the variable.- Parameters:
fieldName
- The name of the field to retrieve.ownerInstance
- The object instance from which to get the variable instance.value
- The value to use when setting the given field- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- See Also:
-
getInstanceField
public static Object getInstanceField(String fieldName, Object ownerInstance) throws RuntimeException Gets the instance field by the given name on the given object instance. The value is a primitive wrapper if it is a primitive type.Note: if the field is static, then the
ownerInstance
field can be the class of the object that contains the variable.- Parameters:
fieldName
- The name of the field to retrieve.ownerInstance
- The object instance from which to get the variable instance.- Returns:
- The field instance.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- Since:
- Tracker Id 267
- See Also:
-
getAllInstanceFields
Gets all fields of the given object. Only objects on the immediate instance are returned.- Parameters:
ownerInstance
- the object from which to get fields- Returns:
- the fields
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Class<?>[] parameterTypes, Object[] args) throws RuntimeException Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise,null
is returned.Note: if the method is static, then the
ownerInstance
field can be the class of the object that contains the method.- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.parameterTypes
- The parameter types that the method takes.args
- The parameter values that should be passed to the method. This value can be null or zero length if there are no parameters to pass- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- Since:
- Tracker Id 267
- See Also:
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance, List<Class<?>> parameterTypes, List<Object> args) throws RuntimeException Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise,null
is returned.Note: if the method is static, then the
ownerInstance
field can be the class of the object that contains the method.This method is just a convenience for calling
invokeInstanceMethod(String, Object, Class[], Object[])
. As the following example shows, this method's uses is a bit cleaner:// The call below is equivalent to calling:
System.out.println("Hi") invokeInstanceMethod("println", System.out, Arrays.asList(String.class), Arrays.asList("Hi"));
- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.parameterTypes
- The parameter types that the method takes.args
- The parameter values that should be passed to the method. This value can be null or zero length if there are no parameters to pass- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Class<?> parameterType, Object arg) throws RuntimeException Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise,null
is returned.Note: if the method is static, then the
ownerInstance
field can be the class of the object that contains the method.If the method you are calling takes no parameters, then call
invokeInstanceMethod(String, Object)
instead.This method is just a convenience for calling
invokeInstanceMethod(String, Object, Class[], Object[])
when the method only takes a single parameter, so that you don't have the ugliness of creating arrays as the parameters for this method.As an example:
// The call below is equivalent to calling:
System.out.println("Hi") invokeInstanceMethod("println", System.out, String.class, "Hi");
- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.parameterType
- The parameter types that the method takes.arg
- The parameter value that should be passed to the method.- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Object... args) throws RuntimeException Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise,null
is returned.Note: if the method is static, then the
ownerInstance
field can be the class of the object that contains the method.Warning: The exact class of each
arg
will be used as the class type of the parameter for the method being called. If the method you are calling takes parameters that do not match exactly the class of the args you wish to use, then callinvokeInstanceMethod(String, Object, List, List)
instead so that you can specify the parameter types explicitly.If the method you are calling takes no parameters, then call
invokeInstanceMethod(String, Object)
instead.This method is just a convenience for calling
invokeInstanceMethod(String, Object, Class[], Object[])
when the method only takes a single parameter, so that you don't have the ugliness of creating arrays as the parameters for this method.As an example:
// The call below is equivalent to calling:
System.out.println("Hi") invokeInstanceMethod("println", System.out, "Hi"); // This call is equivalent to the one above invokeInstanceMethod("println", System.out, Arrays.asList(String.class), Arrays.asList("Hi"));
- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.args
- The parameter value that should be passed to the method.- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance) throws RuntimeException This method is just a "pass through" method forinvokeInstanceMethod(String, Object, Class[], Object[])
so that callers do not need to pass null to that method when the underlying instance method does not have any parameters.- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- See Also:
-
invokeConstructor
public static Object invokeConstructor(Class<?> containingClass, Class<?>[] parameterTypes, Object[] args) throws RuntimeException Uses reflection to execute the constructor for the given class with the given parameters. The new instance of the given class will be returned.- Parameters:
containingClass
- The class that contains the desired constructor.parameterTypes
- The parameter types that the constructor takes. This value can be null or zero length if there are no parameters to passargs
- The parameter values that should be passed to the constructor. This value can be null or zero length if there are no parameters to pass- Returns:
- The new class instance
- Throws:
RuntimeException
- if there is a problem accessing the constructor using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
-
argTypes
A convenience method that can be statically imported to use with the class, allowing you to avoid your own ugly manual array creation.- Parameters:
classes
- the classes- Returns:
- the classes array
-
args
A convenience method that can be statically imported to use with the class, allowing you to avoid your own ugly manual array creation.- Parameters:
objects
- the objects- Returns:
- the objects array
-
getInstanceFieldByClassType
Get the first field object contained within object ownerInstance which has the type classType. This method is only really useful if it is known that only a single field of classType exists within the ownerInstance.- Type Parameters:
T
- the type- Parameters:
classType
- the class type of the desired fieldownerInstance
- the object instance that owns the field- Returns:
- field object of type classType or null
-
locateFieldByTypeOnClass
Get the first field specification contained within containingClass which has the type classType. This method is only really useful if it is known that only a single field of classType exists within the containingClass hierarchy.- Parameters:
classType
- the classcontainingClass
- the class that contains a field of the given type- Returns:
- field which corresponds to type classType or null
-