Class SpringInjectRulesAndExtensions.SpringInjectRulesAndExtensionsTestExecutionListener

java.lang.Object
org.springframework.test.context.support.AbstractTestExecutionListener
io.mats3.spring.test.SpringInjectRulesAndExtensions.SpringInjectRulesAndExtensionsTestExecutionListener
All Implemented Interfaces:
org.springframework.core.Ordered, org.springframework.test.context.TestExecutionListener
Enclosing class:
SpringInjectRulesAndExtensions

public static class SpringInjectRulesAndExtensions.SpringInjectRulesAndExtensionsTestExecutionListener extends org.springframework.test.context.support.AbstractTestExecutionListener
This TestExecutionListener finds all fields in the test class instance which is annotated with JUnit Rule or JUnit 5 Jupiter RegisterExtension, and runs beanFactory.autowireBean(fieldValue) on them.
  • Field Summary

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static Set<Field>
    findFields(Class<?> clazz, String annotationClassName)
    Find all fields in class with given annotation.
    void
    prepareTestInstance(org.springframework.test.context.TestContext testContext)
    Performs dependency injection on Rule and @RegisterExtension fields in test-class as supplied by testContext.

    Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener

    afterTestClass, afterTestExecution, afterTestMethod, beforeTestClass, beforeTestExecution, beforeTestMethod, getOrder

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SpringInjectRulesAndExtensionsTestExecutionListener

      public SpringInjectRulesAndExtensionsTestExecutionListener()
  • Method Details

    • prepareTestInstance

      public void prepareTestInstance(org.springframework.test.context.TestContext testContext)
      Performs dependency injection on Rule and @RegisterExtension fields in test-class as supplied by testContext.
      Specified by:
      prepareTestInstance in interface org.springframework.test.context.TestExecutionListener
      Overrides:
      prepareTestInstance in class org.springframework.test.context.support.AbstractTestExecutionListener
    • findFields

      protected static Set<Field> findFields(Class<?> clazz, String annotationClassName)
      Find all fields in class with given annotation. Amended from StackOverflow, inspired by Apache Commons Lang.

      https://stackoverflow.com/a/29766135