Class TestUtils


  • public final class TestUtils
    extends Object
    Utilities for testing.
    • 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 type
        U - value type
        Parameters:
        subject - Object where the field belongs
        fieldName - name of the field to set
        value - 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 type
        U - fieldO value type
        Parameters:
        subject - Object where the field belongs
        fieldName - 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 type
        U - return value type
        Parameters:
        subject - Object where the method belongs
        methodName - name of the method to call
        paramTypes - formal parameter type array
        args - 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 type
        U - return value type
        Parameters:
        subject - Object where the method belongs
        methodName - name of the method to call
        paramType - formal parameter type
        arg - 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