Class SpringFactory

java.lang.Object
io.cucumber.spring.SpringFactory
All Implemented Interfaces:
Container, Lookup, ObjectFactory

@API(status=STABLE) public final class SpringFactory extends Object implements ObjectFactory
Spring based implementation of ObjectFactory.

Application beans are accessible from the step definitions using autowiring (with annotations).

The spring context can be configured by annotating one glue class with a @CucumberContextConfiguration and any one of the following @ContextConfiguration, @ContextHierarchy or @BootstrapWith. This glue class can also be annotated with @WebAppConfiguration or @DirtiesContext annotation.

Notes:

  • SpringFactory uses Springs TestContextManager framework to manage the spring context. The class annotated with CucumberContextConfiguration will be use to instantiate the TestContextManager.
  • If not exactly one glue class is annotated with CucumberContextConfiguration an exception will be thrown.
  • Step definitions should not be annotated with @Component or other annotations that mark it as eligible for detection by classpath scanning. When a step definition class is annotated by @Component or an annotation that has the @Component stereotype an exception will be thrown
  • Field Details

    • stepClasses

      private final Collection<Class<?>> stepClasses
    • withCucumberContextConfiguration

      private @Nullable Class<?> withCucumberContextConfiguration
    • testContextAdaptor

      private @Nullable TestContextAdaptor testContextAdaptor
  • Constructor Details

    • SpringFactory

      public SpringFactory()
  • Method Details

    • addClass

      public boolean addClass(Class<?> stepClass)
      Description copied from interface: Container
      Add a glue class to the test context.

      Invoked after creation but before ObjectFactory.start().

      Specified by:
      addClass in interface Container
      Parameters:
      stepClass - glue class to add to the text context.
      Returns:
      should always return true, should be ignored.
    • checkNoComponentAnnotations

      private static void checkNoComponentAnnotations(Class<?> type)
    • hasCucumberContextConfiguration

      static boolean hasCucumberContextConfiguration(Class<?> stepClass)
    • checkOnlyOneClassHasCucumberContextConfiguration

      private void checkOnlyOneClassHasCucumberContextConfiguration(Class<?> stepClass)
    • start

      public void start()
      Description copied from interface: ObjectFactory
      Start the object factory. Invoked once per scenario.

      While started Lookup.getInstance(Class) may be invoked.

      Specified by:
      start in interface ObjectFactory
    • stop

      public void stop()
      Description copied from interface: ObjectFactory
      Stops the object factory. Called once per scenario.

      When stopped the object factory should dispose of all glue instances.

      Specified by:
      stop in interface ObjectFactory
    • getInstance

      public <T> T getInstance(Class<T> type)
      Description copied from interface: Lookup
      Provides an instance of a glue class.
      Specified by:
      getInstance in interface Lookup
      Type Parameters:
      T - type of Glue class
      Parameters:
      type - type of instance to be created.
      Returns:
      new instance of type T