Package org.onlab.junit
Class TestUtils
- java.lang.Object
-
- org.onlab.junit.TestUtils
-
public final class TestUtils extends Object
Utilities for testing.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TestUtils.TestUtilsException
Exception that can be thrown if problems are encountered while executing the utility method.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
callConstructor(Constructor<T> constructor)
Triggers an allocation of an object of type T and forces a call to the private constructor.static <T,U>
UcallMethod(T subject, String methodName, Class<?>[] paramTypes, Object... args)
Calls the method, bypassing scope restriction.static <T,U>
UcallMethod(T subject, String methodName, Class<?> paramType, Object arg)
Calls the method, bypassing scope restriction.static <T,U>
UgetField(T subject, String fieldName)
Gets the field, bypassing scope restriction.static <T,U>
voidsetField(T subject, String fieldName, U value)
Sets the field, bypassing scope restriction.
-
-
-
Method Detail
-
setField
public static <T,U> void setField(T subject, String fieldName, U value) throws TestUtils.TestUtilsException
Sets the field, bypassing scope restriction.- Type Parameters:
T
- subject typeU
- value type- Parameters:
subject
- Object where the field belongsfieldName
- name of the field to setvalue
- value to set to the field.- Throws:
TestUtils.TestUtilsException
- if there are reflection errors while setting the field
-
getField
public static <T,U> U getField(T subject, String fieldName) throws TestUtils.TestUtilsException
Gets the field, bypassing scope restriction.- Type Parameters:
T
- subject typeU
- fieldO value type- Parameters:
subject
- Object where the field belongsfieldName
- name of the field to get- Returns:
- value of the field.
- Throws:
TestUtils.TestUtilsException
- if there are reflection errors while getting the field
-
callMethod
public static <T,U> U callMethod(T subject, String methodName, Class<?>[] paramTypes, Object... args) throws TestUtils.TestUtilsException
Calls the method, bypassing scope restriction.- Type Parameters:
T
- subject typeU
- return value type- Parameters:
subject
- Object where the method belongsmethodName
- name of the method to callparamTypes
- formal parameter type arrayargs
- arguments- Returns:
- return value or null if void
- Throws:
TestUtils.TestUtilsException
- if there are reflection errors while calling the method
-
callMethod
public static <T,U> U callMethod(T subject, String methodName, Class<?> paramType, Object arg) throws TestUtils.TestUtilsException
Calls the method, bypassing scope restriction.- Type Parameters:
T
- subject typeU
- return value type- Parameters:
subject
- Object where the method belongsmethodName
- name of the method to callparamType
- formal parameter typearg
- argument- Returns:
- return value or null if void
- Throws:
TestUtils.TestUtilsException
- if there are reflection errors while calling the method
-
callConstructor
public static <T> T callConstructor(Constructor<T> constructor) throws TestUtils.TestUtilsException
Triggers an allocation of an object of type T and forces a call to the private constructor.- Type Parameters:
T
- type of the object to create- Parameters:
constructor
- Constructor to call- Returns:
- created object of type T
- Throws:
TestUtils.TestUtilsException
- if there are reflection errors while calling the constructor
-
-