Interface SmartContextLoader

All Superinterfaces:
ContextLoader
All Known Subinterfaces:
AotContextLoader
All Known Implementing Classes:
AbstractContextLoader, AbstractDelegatingSmartContextLoader, AbstractGenericContextLoader, AbstractGenericWebContextLoader, AnnotationConfigContextLoader, AnnotationConfigWebContextLoader, DelegatingSmartContextLoader, GenericGroovyXmlContextLoader, GenericGroovyXmlWebContextLoader, GenericXmlContextLoader, GenericXmlWebContextLoader, WebDelegatingSmartContextLoader

public interface SmartContextLoader extends ContextLoader
Strategy interface for loading an ApplicationContext for an integration test managed by the Spring TestContext Framework.

The SmartContextLoader SPI supersedes the ContextLoader SPI introduced in Spring 2.5: a SmartContextLoader can choose to process resource locations, component classes, or a combination of both. Furthermore, a SmartContextLoader can configure the context that it loads based on any properties available in the provided MergedContextConfiguration. For example, active bean definition profiles can be configured for the context based on MergedContextConfiguration.getActiveProfiles().

See the Javadoc for @ContextConfiguration for a definition of component classes.

Clients of a SmartContextLoader should call processContextConfiguration() prior to calling loadContext(). This gives a SmartContextLoader the opportunity to provide custom support for modifying resource locations or detecting default resource locations or default configuration classes. The results of processContextConfiguration() should be merged for all classes in the hierarchy of the root test class and then supplied to loadContext().

NOTE: As of Spring Framework 6.0, SmartContextLoader no longer supports methods defined in the ContextLoader SPI.

Concrete implementations must provide a public no-args constructor.

Spring provides the following SmartContextLoader implementations.

Since:
3.1
Author:
Sam Brannen
See Also:
  • Method Details

    • processContextConfiguration

      void processContextConfiguration(ContextConfigurationAttributes configAttributes)
      Process the ContextConfigurationAttributes for a given test class.

      Concrete implementations may choose to modify the locations or classes in the supplied ContextConfigurationAttributes, generate default configuration locations, or detect default configuration classes if the supplied values are null or empty.

      Note: a SmartContextLoader must preemptively verify that a generated or detected default actually exists before setting the corresponding locations or classes property in the supplied ContextConfigurationAttributes. Consequently, leaving the locations or classes property empty signals that this SmartContextLoader was not able to generate or detect defaults.

      Parameters:
      configAttributes - the context configuration attributes to process
    • loadContext

      org.springframework.context.ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
      Load a new ApplicationContext based on the supplied MergedContextConfiguration, configure the context, and return the context in a fully refreshed state.

      Concrete implementations should register annotation configuration processors with bean factories of application contexts loaded by this SmartContextLoader. Beans will therefore automatically be candidates for annotation-based dependency injection using @Autowired, @Resource, and @Inject. In addition, concrete implementations should perform the following actions.

      As of Spring Framework 6.0, any exception thrown while attempting to load an ApplicationContext should be wrapped in a ContextLoadException. Concrete implementations should therefore contain a try-catch block similar to the following.

       ApplicationContext context = // create context
       try {
           // configure and refresh context
       }
       catch (Exception ex) {
           throw new ContextLoadException(context, ex);
       }
       
      Parameters:
      mergedConfig - the merged context configuration to use to load the application context
      Returns:
      a new application context
      Throws:
      ContextLoadException - if context loading failed
      Exception
      See Also:
    • processLocations

      default String[] processLocations(Class<?> clazz, @Nullable String... locations)
      SmartContextLoader does not support deprecated ContextLoader methods. Call processContextConfiguration(ContextConfigurationAttributes) instead.
      Specified by:
      processLocations in interface ContextLoader
      Parameters:
      clazz - the class with which the locations are associated: used to determine how to process the supplied locations
      locations - the unmodified locations to use for loading the application context (can be null or empty)
      Returns:
      an array of application context resource locations
      Throws:
      UnsupportedOperationException - in this implementation
      Since:
      6.0
    • loadContext

      default org.springframework.context.ApplicationContext loadContext(String... locations) throws Exception
      SmartContextLoader does not support deprecated ContextLoader methods.

      Call loadContext(MergedContextConfiguration) instead.

      Specified by:
      loadContext in interface ContextLoader
      Parameters:
      locations - the resource locations to use to load the application context
      Returns:
      a new application context
      Throws:
      UnsupportedOperationException - in this implementation
      Exception - if context loading failed
      Since:
      6.0