public abstract class

AbstractDependencyInjectionSpringContextTests

extends AbstractSingleSpringContextTests
java.lang.Object
   ↳ junit.framework.Assert
     ↳ junit.framework.TestCase
       ↳ org.springframework.test.ConditionalTestCase
         ↳ org.springframework.test.AbstractSpringContextTests
           ↳ org.springframework.test.AbstractSingleSpringContextTests
             ↳ org.springframework.test.AbstractDependencyInjectionSpringContextTests
Known Direct Subclasses
Known Indirect Subclasses

This class is deprecated.
as of Spring 3.0, in favor of using the listener-based test context framework (AbstractJUnit38SpringContextTests)

Class Overview

Convenient superclass for JUnit 3.8 based tests depending on a Spring context. The test instance itself is populated by Dependency Injection.

Really for integration testing, not unit testing. You should not normally use the Spring container for unit tests: simply populate your POJOs in plain JUnit tests!

This supports two modes of populating the test:

  • Via Setter Dependency Injection. Simply express dependencies on objects in the test fixture, and they will be satisfied by autowiring by type.
  • Via Field Injection. Declare protected variables of the required type which match named beans in the context. This is autowire by name, rather than type. This approach is based on an approach originated by Ara Abrahmian. Setter Dependency Injection is the default: set the populateProtectedVariables property to true in the constructor to switch on Field Injection.

Summary

Constants
int AUTOWIRE_BY_NAME Constant that indicates autowiring bean properties by name.
int AUTOWIRE_BY_TYPE Constant that indicates autowiring bean properties by type.
int AUTOWIRE_NO Constant that indicates no autowiring at all.
[Expand]
Inherited Fields
From class org.springframework.test.AbstractSingleSpringContextTests
From class org.springframework.test.ConditionalTestCase
Public Constructors
AbstractDependencyInjectionSpringContextTests()
Default constructor for AbstractDependencyInjectionSpringContextTests.
AbstractDependencyInjectionSpringContextTests(String name)
Constructor for AbstractDependencyInjectionSpringContextTests with a JUnit name.
Public Methods
final int getAutowireMode()
Return the autowire mode for test properties set by Dependency Injection.
final boolean isDependencyCheck()
Return whether or not dependency checking should be performed for test properties set by Dependency Injection.
final boolean isPopulateProtectedVariables()
Return whether to populate protected variables of this test case.
final void setAutowireMode(int autowireMode)
Set the autowire mode for test properties set by Dependency Injection.
final void setDependencyCheck(boolean dependencyCheck)
Set whether or not dependency checking should be performed for test properties set by Dependency Injection.
final void setPopulateProtectedVariables(boolean populateFields)
Set whether to populate protected variables of this test case.
Protected Methods
void injectDependencies()
Inject dependencies into 'this' instance (that is, this test instance).
void prepareTestInstance()
Prepare this test instance, injecting dependencies into its protected fields and its bean properties.
[Expand]
Inherited Methods
From class org.springframework.test.AbstractSingleSpringContextTests
From class org.springframework.test.AbstractSpringContextTests
From class org.springframework.test.ConditionalTestCase
From class junit.framework.TestCase
From class junit.framework.Assert
From class java.lang.Object
From interface junit.framework.Test

Constants

public static final int AUTOWIRE_BY_NAME

Constant that indicates autowiring bean properties by name.

Constant Value: 1 (0x00000001)

public static final int AUTOWIRE_BY_TYPE

Constant that indicates autowiring bean properties by type.

Constant Value: 2 (0x00000002)

public static final int AUTOWIRE_NO

Constant that indicates no autowiring at all.

Constant Value: 0 (0x00000000)

Public Constructors

public AbstractDependencyInjectionSpringContextTests ()

Default constructor for AbstractDependencyInjectionSpringContextTests.

public AbstractDependencyInjectionSpringContextTests (String name)

Constructor for AbstractDependencyInjectionSpringContextTests with a JUnit name.

Parameters
name the name of this text fixture

Public Methods

public final int getAutowireMode ()

Return the autowire mode for test properties set by Dependency Injection.

public final boolean isDependencyCheck ()

Return whether or not dependency checking should be performed for test properties set by Dependency Injection.

public final boolean isPopulateProtectedVariables ()

Return whether to populate protected variables of this test case.

public final void setAutowireMode (int autowireMode)

Set the autowire mode for test properties set by Dependency Injection.

The default is AUTOWIRE_BY_TYPE. Can be set to AUTOWIRE_BY_NAME or AUTOWIRE_NO instead.

public final void setDependencyCheck (boolean dependencyCheck)

Set whether or not dependency checking should be performed for test properties set by Dependency Injection.

The default is true, meaning that tests cannot be run unless all properties are populated.

public final void setPopulateProtectedVariables (boolean populateFields)

Set whether to populate protected variables of this test case. Default is false.

Protected Methods

protected void injectDependencies ()

Inject dependencies into 'this' instance (that is, this test instance).

The default implementation populates protected variables if the #populateProtectedVariables() appropriate flag is set, else uses autowiring if autowiring is switched on (which it is by default).

Override this method if you need full control over how dependencies are injected into the test instance.

Throws
Exception in case of dependency injection failure
IllegalStateException if the ApplicationContext for this test instance has not been configured
See Also
  • #populateProtectedVariables()

protected void prepareTestInstance ()

Prepare this test instance, injecting dependencies into its protected fields and its bean properties.

Note: if the ApplicationContext for this test instance has not been configured (e.g., is null), dependency injection will naturally not be performed, but an informational message will be written to the log.

Throws
Exception