Class TestUtils


  • public final class TestUtils
    extends java.lang.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​(java.lang.reflect.Constructor<T> constructor)
      Triggers an allocation of an object of type T and forces a call to the private constructor.
      static <T,​U>
      U
      callMethod​(T subject, java.lang.String methodName, java.lang.Class<?>[] paramTypes, java.lang.Object... args)
      Calls the method, bypassing scope restriction.
      static <T,​U>
      U
      callMethod​(T subject, java.lang.String methodName, java.lang.Class<?> paramType, java.lang.Object arg)
      Calls the method, bypassing scope restriction.
      static <T,​U>
      U
      getField​(T subject, java.lang.String fieldName)
      Gets the field, bypassing scope restriction.
      static <T,​U>
      void
      setField​(T subject, java.lang.String fieldName, U value)
      Sets the field, bypassing scope restriction.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • setField

        public static <T,​U> void setField​(T subject,
                                                java.lang.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,
                                             java.lang.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,
                                               java.lang.String methodName,
                                               java.lang.Class<?>[] paramTypes,
                                               java.lang.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,
                                               java.lang.String methodName,
                                               java.lang.Class<?> paramType,
                                               java.lang.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​(java.lang.reflect.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