Expert Spring MVC and Web Flow

(Dana P.) #1

Create and Use a Special ApplicationContext for Tests Only?


A common question regarding Spring’s integration test support classes is, “Should I create a
custom ApplicationContextconfiguration for my tests?” Our initial answer to this question is
no, for a good reason. We feel that much of the purpose for integration tests is to test the config-
uration of the application as it will run in production. There’s rarely another good opportunity
to test all that XML, including external configurations such as Hibernate mapping files, before a
deployment.
It is possible that your configuration will require extra bean definitions to replace
objects found only inside your J2EE container, such as connection pools or transaction
managers. If your application requires these container-provided resources, you can create
a testOnlyApplicationContext.xml configuration with bean definitions for those external
resources. Spring can easily host connection pools and transaction managers, which you can
use when running the code outside of the container.
Through intelligent modularization of the configuration files, you will find it easy to swap
bean definitions at test runtime.


Role of the Application Server


It’s important to mention that integration tests are still intended to be run outside of the appli-
cation server. Even though the full Spring ApplicationContextis constructed, the test should
still run without being deployed. Both unit tests and integration tests must still run as quickly
as possible, and testing outside the application server is the only way to run the tests and get
immediate feedback.
Testing inside the container is another way to perform integration tests, but should be
avoided due to the time it takes to run the tests. Any test that becomes a hassle to run will
quickly fall out of fashion and thus usefulness. We discourage requiring an application server
for tests.
If your application requires the application server to run, it’s possible that it is not taking
advantage of the configuration options provided by Spring. As mentioned, even if you are using
the server’s connection pool during deployments, you can use Spring to create a connection
pool for test runs.


Summary


Integration tests ensure that the application is wired and configured correctly by creating a full
Spring ApplicationContextfor the test class. Spring provides base classes for easy implemen-
tation of dependency injected and transactional tests, making it easy and safe to create tests
that exercise multiple layers of the system, including the database.
These types of tests follow the basic guidelines of unit tests, except they explicitly are test-
ing how multiple components of the system interact. This is an important testing step, as it
checks that all the XML is configured correctly, and usually that the database mappings are
correct as well.
Spring’s AbstractDependencyInjectionSpringContextTestsclass performs autowiring by
type on the test class itself to populate it with all of its dependencies. These dependencies are
normally the classes under test, and thus enter the test fully wired and configured.


CHAPTER 10 ■TESTING SPRING MVC APPLICATIONS 307
Free download pdf