Earlier a single bundle was integration tested by providing a test implementation of its
DataSource
dependency.
When integration testing it is often a good idea to
test the entire application outside of the container.
In this step you will create a test case for the
entire GreenPages application starting with the GreenPagesController
class
and descending all the way to a database.
It would be sensible to create this in a separate test bundle
but as one of the bundles involved here is a web bundle the tests will have to go in there.
Since this project will be testing the GreenPages application as a whole, it needs to depend on the bundles
that make up the application.
Open the pom.xml
file for the greenpages.web
project and add a dependency declaration for the greenpages.jpa
bundle:
<dependency> <groupId>com.springsource.dmserver</groupId> <artifactId>greenpages.jpa</artifactId> <version>${project.version}</version> <scope>test</scope> </dependency>
noting that the scope is test
.
Open the GreenPagesSpringContextTests
class
and add the Spring Test Framework declarations.
These declarations should run the test with the
SpringJunit4ClassRunner
and configure the test with the
classpath*:/META-INF/spring/module-context.xml
,
file:src/main/webapp/WEB-INF/greenpages-servlet.xml
and
classpath:/META-INF/spring/test-context.xml
files. Note the use of
classpath*:
with respect to the module-context.xml
path.
This will
cause Spring to look for files that match that path in all of the bundles on the classpath meaning that all
the application beans will be instantiated.
Also, as we do not want the WEB-INF
folder
on the classpath we must reference the servlet context for GreenPages with a full file path:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:/META-INF/spring/module-context.xml", "file:src/main/webapp/WEB-INF/greenpages-servlet.xml", "classpath:/META-INF/spring/test-context.xml" }) @TestExecutionListeners(value = DependencyInjectionTestExecutionListener.class) public class GreenPagesSpringContextTests { …
It may be necessary to click on Update the MANIFEST.MF
on the template overview pane
and Update Dependencies from the Maven menu,
before Eclipse will suggest appropriate imports here.
When this configuration is complete, click on the Run drop-down and select Run Configurations…. In the the dialog that opens select → and press Run;
When this test is run, Spring creates an ApplicationContext
that is built
from the module-context.xml
configuration files from all of the bundles.
Because of
this all of the internal dependencies are satisfied by the beans created directly by the bundles.
There are warnings output by this test concerning log4j
:
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner). log4j:WARN Please initialize the log4j system properly.
These warnings are benign, and do not influence the tests in any way.
The next chapter constructs an automated build system that might be used to build GreenPages (and run its tests) outside of an interactive development environment.