img
. .
implement unit tests. For the BindingResult interface, an instance of the BeanPropertyBindingResult
class was constructed that will perform the registration and evaluation of binding errors on JavaBean
objects, by using standard JavaBean property access.
Then, the ContactController.create() method is called, and assert operations are invoked to verify
the result. Run the result again, and note the result in the JUnit view.
For the create() method, we should create more test cases to test various scenarios. For example,
we need to test when data access errors are encountered during the save operation.
Implementing an Integration Unit Test
In this section, we will implement the integration unit test for the service layer. In the contact
application, the core service is the class com.apress.prospring3.ch19.service.jpa.ContactServiceImpl,
which is the JPA implementation of the com.apress.prospring3.ch19.service.ContactService interface.
When unit testing the service layer, we will use the H2 in-memory database to host the data model
and testing data, with the JPA providers (Hibernate and Spring Data JPA's repository abstraction) in
place. The objective is to ensure that the ContactServiceImpl class is performing the business
functions correctly.
In the following sections, we will show how to test some of the finder methods and the save
operation of the ContactServiceImpl class.
Adding Required Dependencies
For implementing test cases with the database in place, we need a library that can help populate the
desired testing data in the database before executing the test case and that can perform the necessary
database operations easily. Moreover, in order to make it easier to prepare the test data, we will support
the preparation of test data in Microsoft Excel format.
To fulfill these purposes, additional libraries are required. On the database side, DBUnit
(http://dbunit.sourceforge.net) is a common library that can help implement database-related
testing. In addition, the Apache POI (http://poi.apache.org) project's library will be used to help parse
the test data that was prepared in Microsoft Excel. Table 19-3 shows the required dependencies.
Table 19-3. Maven Dependencies for Integration Unit Test
Group ID
Artifact ID
Version
Description
2.4.8
The DBUnit library
org.dbunit
dbunit
3.2-FINAL
Apache POI library that supports reading and writing of
org.apache.poi
poi
files in Microsoft Office format
Add the dependencies in Table 19-3 into the project in STS.
Configuring the Profile for Service Layer Testing
The bean definition profiles feature introduced in Spring 3.1 is very useful for implementing a test case
with the appropriate configuration of the testing components. To facilitate the testing of the service
layer, we will also use the profile feature for the ApplicationContext configuration.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home