Package h07.util
Class TestClass
- java.lang.Object
-
- h07.util.TestClass
-
- Direct Known Subclasses:
AdjacencyMatrixFactoryTest
,ChickenNuggetGraphFactoryTest
,ChickenNuggetsTest
,DijkstraTest
,DirectedGraphImplTest
,IntegerAdditionTest
,PathImplTest
,TemplateTests.AbstractPathTemplateTest
,TemplateTests.DirectedGraphFactoryTemplateTest
,TemplateTests.DirectedGraphTemplateTest
,TemplateTests.MonoidTemplateTest
,TemplateTests.PathTemplateTest
,TemplateTests.ShortestPathsAlgorithmTemplateTest
,TemplateTests.TraverserTemplateTest
public abstract class TestClass extends Object
Common class for all test classes.
The centerpiece of this testsuite, which must be extended by all test classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TestClass.MissingMemberException
An exception class to indicate that a member was not found when searching by name or criteria.
-
Field Summary
Fields Modifier and Type Field Description Class<?>
classDescriptor
TheClass
object representing the actual class that is testedString
className
The fully qualified type name of the class that is testedConstructor<?>
constructor
TheConstructor
object representing the constructor of the tested class.Map<String,Field>
fields
A map with all declared fields of the tested class.Map<String,Method>
methods
A map with all declared methods of the tested class.static Random
RANDOM
TheRandom
instance used by all randomized tests / providers.private static long
SEED
The seed to instantiateRANDOM
with.
-
Constructor Summary
Constructors Modifier Constructor Description protected
TestClass(String className, Predicate<Constructor<?>> constructorPredicate)
Initializes a test class.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Class<?>
getClassForName(String className)
Returns the class object for a given name.Field
getFieldByCriteria(String identifier, Predicate<Field> predicate)
Gets the field matching a given predicate fromfields
.Field
getFieldByName(String fieldName)
Gets the field for a given name fromfields
.Method
getMethodByCriteria(String identifier, Predicate<Method> predicate)
Gets the method matching a given predicate frommethods
.Method
getMethodByName(String methodSignature)
Gets the method for a given signature frommethods
.static String
getMethodSignature(Method method)
Returns the signature of the given method.void
invokeExpectingException(Object instance, Method method, Object... params)
Invokes the given method withparams
in the context ofinstance
and returns the result.Object
newInstance(Object... params)
Creates a new instance of the tested class.Object
newInstanceExpectingException(Object... params)
Creates a new instance of the tested class and passes along any exceptions.abstract void
testDefinition()
Tests the definition ofclassDescriptor
.void
testInstance()
Tests an instance ofclassDescriptor
.
-
-
-
Field Detail
-
SEED
private static final long SEED
The seed to instantiateRANDOM
with.
If no seed has been set inConfig.SEED
, a random one will be generated. In any case, the seed will be printed to standard output.
-
RANDOM
public static final Random RANDOM
TheRandom
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
TheClass
object representing the actual class that is tested
-
constructor
public final Constructor<?> constructor
TheConstructor
object representing the constructor of the tested class.
This constant may benull
if a class has no constructors (e.g., becauseclassDescriptor
actually represents an interface) or if the tested class shouldn't be instantiated. If this object is notnull
, it will have been made accessible by invokingConstructor.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 respectiveField
objects. All fields have been set to be accessible by invokingField.setAccessible(boolean)
with first parameter =true
on each entry.
-
methods
public final Map<String,Method> methods
A map with all declared methods of the tested class.
The keys of this map are the method's signatures; the values are their respectiveMethod
objects. All methods have been set to be accessible by invokingMethod.setAccessible(boolean)
with first parameter =true
on each entry.- See Also:
getMethodSignature(Method)
-
-
Constructor Detail
-
TestClass
protected TestClass(String className, Predicate<Constructor<?>> constructorPredicate)
Initializes a test class.
Sets the fieldsclassName
,classDescriptor
,constructor
,fields
andmethods
.- Parameters:
className
- The name of the class / interface to be tested.constructorPredicate
- A predicate to find a specific constructor of the tested class. May benull
if none is needed.- Throws:
org.opentest4j.TestAbortedException
- if the specified class was not foundTestClass.MissingMemberException
- if no constructor matchingconstructorPredicate
has been found
-
-
Method Detail
-
testDefinition
public abstract void testDefinition()
Tests the definition ofclassDescriptor
. Implementations must be annotated withTest
.- Throws:
org.opentest4j.AssertionFailedError
- if any assertion failed
-
testInstance
public void testInstance() throws Exception
Tests an instance ofclassDescriptor
. Implementations must be annotated withTest
.- Throws:
org.opentest4j.AssertionFailedError
- if any assertion failedException
-
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 fromfields
.- 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 fromfields
.- 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 predicateRuntimeException
- 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 frommethods
.- Parameters:
methodSignature
- The name of the method.- Returns:
- the method
- Throws:
TestClass.MissingMemberException
- if the method does not exist under that name
-
getMethodByCriteria
public Method getMethodByCriteria(String identifier, Predicate<Method> predicate)
Gets the method matching a given predicate frommethods
.- Parameters:
predicate
- The predicate to test with.- Returns:
- the method
- Throws:
TestClass.MissingMemberException
- if no methods match the given predicate
-
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
- ifconstructor
isnull
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
- ifconstructor
isnull
Throwable
- any exceptions thrown by the constructor
-
invokeExpectingException
public void invokeExpectingException(Object instance, Method method, Object... params) throws Throwable
Invokes the given method withparams
in the context ofinstance
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
-
-