<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
<!-- Other code omitted -->
</web-app>
The new parameter to apply is highlighted in bold. The parameter spring.profiles.active is given
the value dev to indicate that Spring should bootstrap WebApplicationContext with the beans defined for
the dev profile.
Implementing the Infrastructure Classes
Before implementing the individual test case, we need to implement the infrastructure classes, including
the Java configuration class and the base class for service layer testing. In addition, we also need to
implement some classes to support the population of test data in the Excel file. Moreover, to ease the
development of the test case, we want to introduce a custom annotation called @DataSets, which accepts
the Excel file name as the argument. We will develop a custom test execution listener (a feature
supported by the Spring testing framework) to check for the existence of the annotation and load the
data accordingly.
In the following sections, we will discuss how to implement the various infrastructure classes and
the custom listener that loads data from the Excel file.
Implementing Custom TestExecutionListener
In the spring-test module, the org.springframework.test.context.TestExecutionListener interface
defines a listener API that can intercept the events in the various phases of the test case execution (e.g.,
before and after the class under test, before and after the method under test, and so on). In testing the
service layer, we will implement a custom listener for the newly introduced @DataSets annotation. The
objective is to support the population of test data with a simple annotation on the test case. For
example, to test the ContactService.findAll() method, we would like to have the code look like the
code snippet in Listing 19-7.
Listing 19-7. Usage of the @DataSets Annotation
@DataSets(setUpDataSet="/com/apress/prospring3/ch19/service/jpa/ContactServiceImplTest.xls")
@Test
public void testFindAll() throws Exception {
List<Contact> result = customerService.findAll();
...
}
In Listing 19-7, the application of the @DataSets annotation to the test case indicates that before
running the test, testing data needs to be loaded into the database from the specified Excel file.
First we need to define the custom annotation, which is shown in Listing 19-8.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home