Class TestInstanceTesterResolver

java.lang.Object
org.dbunit.annotation.runtime.TestInstanceTesterResolver

public class TestInstanceTesterResolver extends Object
The binding-neutral part of resolving, for one test, the 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:

  1. A field annotated DbUnitTestCase, whose type implements PrepAndExpectedTestCase - that instance is driven directly.
  2. A field annotated DbUnitTester, whose type implements IDatabaseTester.
  3. Whatever the binding's unmarkedTester supplies, normally built from fromFactory(AnnotatedTestConfiguration) and fromUnmarkedField(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
  • 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 of instances, falling back to unmarkedTester when no instance declares a marked field - and for the tester a @DbUnitTestCase field's test case does not carry itself.
      Parameters:
      instances - The test instances in scope, innermost first.
      unmarkedTester - Supplies the tester when no @DbUnitTester field does; must throw rather than return null when it finds none.
      Returns:
      The resolved tester, and the test case when a @DbUnitTestCase field supplied one.
      Throws:
      IllegalStateException - If a marked field is ambiguous, null, or of the wrong type.
      Exception - If unmarkedTester fails.
    • fromFactory

      public static IDatabaseTester fromFactory(AnnotatedTestConfiguration configuration) throws Exception
      Creates the tester DbUnitConfig.databaseTesterFactory() names.
      Parameters:
      configuration - The resolved configuration.
      Returns:
      The factory-created tester, or null when no factory is configured.
      Throws:
      IllegalStateException - If the factory cannot be instantiated or returns null.
      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 to IDatabaseTester; 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 null when no instance declares such a field.
      Throws:
      IllegalStateException - If a class declares more than one such field, or the one found holds null.
      IllegalAccessException - If the field cannot be read.