Class TestInstanceTesterResolver
IDatabaseTester and (when
present) the PrepAndExpectedTestCase an AnnotatedTestExecutor drives: what the
test instances' fields and DbUnitConfig.databaseTesterFactory() say. Written against
plain java.lang.reflect, so DbUnitExtension (JUnit 5/6) and
DbUnitTestExecutionListener (Spring) share one set of rules and one set of messages
for the same mistakes, whichever test framework or annotation-scanning utility each binding
itself uses.
A binding passes the test instances in scope, innermost first - for a @Nested test
class, its own instance, then its enclosing class's instance, and so on outward - and supplies
only what differs between frameworks: where to look when no field names a tester. Resolution
is first match wins:
- A field annotated
DbUnitTestCase, whose type implementsPrepAndExpectedTestCase- that instance is driven directly. - A field annotated
DbUnitTester, whose type implementsIDatabaseTester. - Whatever the binding's
unmarkedTestersupplies, normally built fromfromFactory(AnnotatedTestConfiguration)andfromUnmarkedField(List)plus the binding's own tiers.
The first instance, innermost first, that declares either marker supplies the marked field
and shadows every outer instance entirely, whichever marker the outer one uses. Within one
instance, the whole class hierarchy is searched, and a marked field is ambiguous - rejected
with an IllegalStateException - when it is declared more than once there, on one class
or across a class and its superclass, or when both markers are declared.
Public so a binding outside org.dbunit.annotation.runtime - such as
DbUnitExtension or a Spring TestExecutionListener - can reach it too.
- Since:
- 3.6.0
- Author:
- Jeff Jensen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe resolved tester, and the test case when a@DbUnitTestCasefield supplied one. -
Method Summary
Modifier and TypeMethodDescriptionstatic IDatabaseTesterfromFactory(AnnotatedTestConfiguration configuration) Creates the testerDbUnitConfig.databaseTesterFactory()names.static IDatabaseTesterfromUnmarkedField(List<Object> instances) The original (3.5.0) field auto-scan: within each instance, innermost first, and within its class hierarchy (most-derived class first), the first class declaring exactly one non-static field assignable toIDatabaseTester; two or more at the same declaring class are ambiguous.resolve(List<Object> instances, Callable<IDatabaseTester> unmarkedTester) Resolves the tester and test case from the marked fields ofinstances, falling back tounmarkedTesterwhen no instance declares a marked field - and for the tester a@DbUnitTestCasefield's test case does not carry itself.
-
Method Details
-
resolve
public static TestInstanceTesterResolver.Resolution resolve(List<Object> instances, Callable<IDatabaseTester> unmarkedTester) throws Exception Resolves the tester and test case from the marked fields ofinstances, falling back tounmarkedTesterwhen no instance declares a marked field - and for the tester a@DbUnitTestCasefield's test case does not carry itself.- Parameters:
instances- The test instances in scope, innermost first.unmarkedTester- Supplies the tester when no@DbUnitTesterfield does; must throw rather than returnnullwhen it finds none.- Returns:
- The resolved tester, and the test case when a
@DbUnitTestCasefield supplied one. - Throws:
IllegalStateException- If a marked field is ambiguous, null, or of the wrong type.Exception- IfunmarkedTesterfails.
-
fromFactory
public static IDatabaseTester fromFactory(AnnotatedTestConfiguration configuration) throws Exception Creates the testerDbUnitConfig.databaseTesterFactory()names.- Parameters:
configuration- The resolved configuration.- Returns:
- The factory-created tester, or
nullwhen no factory is configured. - Throws:
IllegalStateException- If the factory cannot be instantiated or returnsnull.Exception- If the factory fails creating the tester.
-
fromUnmarkedField
public static IDatabaseTester fromUnmarkedField(List<Object> instances) throws IllegalAccessException The original (3.5.0) field auto-scan: within each instance, innermost first, and within its class hierarchy (most-derived class first), the first class declaring exactly one non-static field assignable toIDatabaseTester; two or more at the same declaring class are ambiguous.- Parameters:
instances- The test instances in scope, innermost first.- Returns:
- The tester that field holds, or
nullwhen no instance declares such a field. - Throws:
IllegalStateException- If a class declares more than one such field, or the one found holdsnull.IllegalAccessException- If the field cannot be read.
-