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.
Following the import instructions in the section called “Importing the greenpages.web project”, import the
$GREENPAGES_HOME/start/greenpages.tests
project. Initially this project will have
compile failures in it; this is to be expected.
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 its pom.xml
file and add dependency declarations for
the greenpages.app
, greenpages.jpa
, and
greenpages.web
bundles:
<dependency> <groupId>com.springsource.dmserver</groupId> <artifactId>greenpages.app</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.springsource.dmserver</groupId> <artifactId>greenpages.jpa</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.springsource.dmserver</groupId> <artifactId>greenpages.web</artifactId> <version>${project.version}</version> </dependency>
Open the GreenPagesSpringContextTests
class (in greenpages.tests
)
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
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:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath*:/META-INF/spring/module-context.xml", "classpath:/META-INF/spring/test-context.xml" }) @TestExecutionListeners(value = DependencyInjectionTestExecutionListener.class) public class GreenPagesSpringContextTests { …
It may be necessary to update the MANIFEST.MF
from the template overview pane, before Eclipse will suggest
all the right 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.