org.unitils.spring
Class SpringModule

java.lang.Object
  extended by org.unitils.spring.SpringModule
All Implemented Interfaces:
Module

public class SpringModule
extends Object
implements Module

A module for Spring enabling a test class by offering an easy way to load application contexts and an easy way of retrieving beans from the context and injecting them in the test.

The application context loading can be achieved by using the SpringApplicationContext annotation. These contexts are cached, so a context will be reused when possible. For example suppose a superclass loads a context and a test-subclass wants to use this context, it will not create a new one. invalidateApplicationContext(java.lang.Class...) } can be used to force a reloading of a context if needed.

Spring bean retrieval can be done by annotating the corresponding fields in the test with following annotations: SpringBean, SpringBeanByName and SpringBeanByType.

See the javadoc of these annotations for more info on how you can use them.

Author:
Tim Ducheyne, Filip Neven

Nested Class Summary
protected  class SpringModule.SpringTestListener
          The TestListener for this module
 
Field Summary
static String PROPKEY_APPLICATION_CONTEXT_FACTORY_CLASS_NAME
           
 
Constructor Summary
SpringModule()
           
 
Method Summary
 void afterInit()
          No after initialization needed for this module
 org.springframework.context.ApplicationContext getApplicationContext(Object testObject)
          Gets the application context for this test.
protected  DatabaseModule getDatabaseModule()
           
 Object getSpringBean(Object testObject, String name)
          Gets the spring bean with the given name.
<T> T
getSpringBeanByType(Object testObject, Class<T> type)
          Gets the spring bean with the given type.
 TestListener getTestListener()
          Creates the test listener for this module.
 void init(Properties configuration)
          Initializes this module using the given configuration
 void injectApplicationContext(Object testObject)
          Gets the application context for this class and sets it on the fields and setter methods that are annotated with SpringApplicationContext.
 void injectSpringBeans(Object testObject)
          Injects spring beans into all fields that are annotated with SpringBean.
 void injectSpringBeansByName(Object testObject)
          Injects spring beans into all fields that are annotated with SpringBeanByName.
 void injectSpringBeansByType(Object testObject)
          Injects spring beans into all fields methods that are annotated with SpringBeanByType.
 void invalidateApplicationContext(Class<?>... classes)
          Forces the reloading of the application context the next time that it is requested.
 boolean isApplicationContextConfiguredFor(Object testObject)
           
protected  boolean isDatabaseModuleEnabled()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPKEY_APPLICATION_CONTEXT_FACTORY_CLASS_NAME

public static final String PROPKEY_APPLICATION_CONTEXT_FACTORY_CLASS_NAME
See Also:
Constant Field Values
Constructor Detail

SpringModule

public SpringModule()
Method Detail

init

public void init(Properties configuration)
Initializes this module using the given configuration

Specified by:
init in interface Module
Parameters:
configuration - The configuration, not null

afterInit

public void afterInit()
No after initialization needed for this module

Specified by:
afterInit in interface Module

getSpringBean

public Object getSpringBean(Object testObject,
                            String name)
Gets the spring bean with the given name. The given test instance, by using SpringApplicationContext, determines the application context in which to look for the bean.

A UnitilsException is thrown when the no bean could be found for the given name.

Parameters:
testObject - The test instance, not null
name - The name, not null
Returns:
The bean, not null

getSpringBeanByType

public <T> T getSpringBeanByType(Object testObject,
                                 Class<T> type)
Gets the spring bean with the given type. The given test instance, by using SpringApplicationContext, determines the application context in which to look for the bean. If more there is not exactly 1 possible bean assignment, an UnitilsException will be thrown.

Parameters:
testObject - The test instance, not null
type - The type, not null
Returns:
The bean, not null

isApplicationContextConfiguredFor

public boolean isApplicationContextConfiguredFor(Object testObject)
Parameters:
testObject - The test object
Returns:
Whether an ApplicationContext has been configured for the given testObject

getApplicationContext

public org.springframework.context.ApplicationContext getApplicationContext(Object testObject)
Gets the application context for this test. A new one will be created if it does not exist yet. If a superclass has also declared the creation of an application context, this one will be retrieved (or created if it was not created yet) and used as parent context for this classes context.

If needed, an application context will be created using the settings of the SpringApplicationContext annotation.

If a class level SpringApplicationContext annotation is found, the passed locations will be loaded using a ClassPathXmlApplicationContext. Custom creation methods can be created by annotating them with SpringApplicationContext. They should have an ApplicationContext as return type and either no or exactly 1 argument of type ApplicationContext. In the latter case, the current configured application context is passed as the argument.

A UnitilsException will be thrown if no context could be retrieved or created.

Parameters:
testObject - The test instance, not null
Returns:
The application context, not null

invalidateApplicationContext

public void invalidateApplicationContext(Class<?>... classes)
Forces the reloading of the application context the next time that it is requested. If classes are given only contexts that are linked to those classes will be reset. If no classes are given, all cached contexts will be reset.

Parameters:
classes - The classes for which to reset the contexts

injectApplicationContext

public void injectApplicationContext(Object testObject)
Gets the application context for this class and sets it on the fields and setter methods that are annotated with SpringApplicationContext. If no application context could be created, an UnitilsException will be raised.

Parameters:
testObject - The test instance, not null

injectSpringBeans

public void injectSpringBeans(Object testObject)
Injects spring beans into all fields that are annotated with SpringBean.

Parameters:
testObject - The test instance, not null

injectSpringBeansByType

public void injectSpringBeansByType(Object testObject)
Injects spring beans into all fields methods that are annotated with SpringBeanByType.

Parameters:
testObject - The test instance, not null

injectSpringBeansByName

public void injectSpringBeansByName(Object testObject)
Injects spring beans into all fields that are annotated with SpringBeanByName.

Parameters:
testObject - The test instance, not null

isDatabaseModuleEnabled

protected boolean isDatabaseModuleEnabled()

getDatabaseModule

protected DatabaseModule getDatabaseModule()

getTestListener

public TestListener getTestListener()
Description copied from interface: Module
Creates the test listener for this module.

Specified by:
getTestListener in interface Module
Returns:
The SpringModule.SpringTestListener for this module


Copyright © 2011. All Rights Reserved.