Spring 3.1 M2: Testing with @Configuration Classes and Profiles
As Jürgen Höller mentioned in his post announcing the release of Spring 3.1 M2, the Spring TestContext Framework(*) has been overhauled to provide first-class testing support for @Configuration
classes and environment profiles.
In this post I'll first walk you through some examples that demonstrate these new testing features. I'll then cover some of the new extension points in the TestContext framework that make these new features possible.
Please note: this is a cross post from my company blog www.swiftmind.com.
Background
In Spring 2.5 we introduced the Spring TestContext Framework which provides annotation-driven integration testing support that can be used with JUnit or TestNG. The examples in this blog will focus on JUnit-based tests, but all features used here apply to TestNG as well.At its core, the TestContext framework allows you to annotate test classes with @ContextConfiguration
to specify which configuration files to use to load the ApplicationContext
for your test. By default the ApplicationContext
is loaded using the GenericXmlContextLoader
which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext
by annotating fields in your test class with @Autowired
, @Resource
, or @Inject
…