Package h07.util

Class TestClass

    • Field Detail

      • SEED

        private static final long SEED
        The seed to instantiate RANDOM with.
        If no seed has been set in Config.SEED, a random one will be generated. In any case, the seed will be printed to standard output.
      • RANDOM

        public static final Random RANDOM
        The Random instance used by all randomized tests / providers.
      • className

        public final String className
        The fully qualified type name of the class that is tested
      • classDescriptor

        public final Class<?> classDescriptor
        The Class object representing the actual class that is tested
      • constructor

        public final Constructor<?> constructor
        The Constructor object representing the constructor of the tested class.
        This constant may be null if a class has no constructors (e.g., because classDescriptor actually represents an interface) or if the tested class shouldn't be instantiated. If this object is not null, it will have been made accessible by invoking Constructor.setAccessible(boolean) with first parameter = true.
      • fields

        public final Map<String,​Field> fields
        A map with all declared fields of the tested class.
        The keys of this map are the field's identifiers; the values are their respective Field objects. All fields have been set to be accessible by invoking Field.setAccessible(boolean) with first parameter = true on each entry.
    • Constructor Detail

      • TestClass

        protected TestClass​(String className,
                            Predicate<Constructor<?>> constructorPredicate)
        Initializes a test class.
        Sets the fields className, classDescriptor, constructor, fields and methods.
        Parameters:
        className - The name of the class / interface to be tested.
        constructorPredicate - A predicate to find a specific constructor of the tested class. May be null if none is needed.
        Throws:
        org.opentest4j.TestAbortedException - if the specified class was not found
        TestClass.MissingMemberException - if no constructor matching constructorPredicate has been found
    • Method Detail

      • testDefinition

        public abstract void testDefinition()
        Tests the definition of classDescriptor. Implementations must be annotated with Test.
        Throws:
        org.opentest4j.AssertionFailedError - if any assertion failed
      • testInstance

        public void testInstance()
                          throws Exception
        Tests an instance of classDescriptor. Implementations must be annotated with Test.
        Throws:
        org.opentest4j.AssertionFailedError - if any assertion failed
        Exception
      • getClassForName

        public static Class<?> getClassForName​(String className)
        Returns the class object for a given name.
        Parameters:
        className - The fully qualified type name of the class.
        Returns:
        the class object for the corresponding name
        Throws:
        org.opentest4j.TestAbortedException - if the class was not found
      • getMethodSignature

        public static String getMethodSignature​(Method method)
        Returns the signature of the given method. Parameter types are mapped to their respective fully qualified type names. The returned string for this method would be:
        "getMethodSignature(java.lang.reflect.Method)".
        Parameters:
        method - The method to generate the signature of.
        Returns:
        the method signature
      • getFieldByName

        public Field getFieldByName​(String fieldName)
        Gets the field for a given name from fields.
        Parameters:
        fieldName - The name of the field.
        Returns:
        the field
        Throws:
        TestClass.MissingMemberException - if the field does not exist under that name
      • getFieldByCriteria

        public Field getFieldByCriteria​(String identifier,
                                        Predicate<Field> predicate)
        Gets the field matching a given predicate from fields.
        Parameters:
        identifier - A string identifying a field in the test class.
        predicate - The predicate to test with.
        Returns:
        the field
        Throws:
        TestClass.MissingMemberException - if no fields match the given predicate
        RuntimeException - if the number of fields matching the predicate is greater than 1
      • getMethodByName

        public Method getMethodByName​(String methodSignature)
        Gets the method for a given signature from methods.
        Parameters:
        methodSignature - The name of the method.
        Returns:
        the method
        Throws:
        TestClass.MissingMemberException - if the method does not exist under that name
      • newInstance

        public Object newInstance​(Object... params)
        Creates a new instance of the tested class.
        Parameters:
        params - The parameters to invoke the constructor with.
        Returns:
        the instance
        Throws:
        NullPointerException - if constructor is null
        RuntimeException - if any exception occurred when invoking the constructor
      • newInstanceExpectingException

        public Object newInstanceExpectingException​(Object... params)
                                             throws Throwable
        Creates a new instance of the tested class and passes along any exceptions.
        Parameters:
        params - The parameters to supply to the constructor.
        Returns:
        the instance
        Throws:
        NullPointerException - if constructor is null
        Throwable - any exceptions thrown by the constructor
      • invokeExpectingException

        public void invokeExpectingException​(Object instance,
                                             Method method,
                                             Object... params)
                                      throws Throwable
        Invokes the given method with params in the context of instance and returns the result.
        Parameters:
        instance - The instance the method should be invoked on.
        method - The method to invoke.
        params - The parameters to invoke the method with.
        Throws:
        Throwable - any exceptions thrown by the method