img
Unit Testing Service Layer
Having the configuration and infrastructure classes in place, we can implement the unit test for the
service layer.
Let's begin with unit testing the finder methods, including the ContactService.findAll() and
ContactService.findByFirstNameAndLastName() methods. First we need to prepare the testing data in
Excel format. A common practice is to put the file into the same folder as the test case class, with the
same name. So, in this case, the file name is
/src/test/java/com/apress/prospring3/ch19/service/jpa/ContactServiceImplTest.xls. Figure 19-6
shows the file content.
As shown in Figure 19-6, the testing data was prepared in a worksheet. The worksheet's name is the
table's name, while the first row is the column name within the table. You can see that we specified the
ID column, but no value was provided. This is because the ID will be populated by the database. We had
only one contact record in the file, so the findAll() method should return one contact.
Figure 19-6. Test data in Excel format
Listing 19-12 shows the test class with test cases for the two finder methods.
Listing 19-12. Testing the Finder Methods
package com.apress.prospring3.ch19.service.jpa;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.apress.prospring3.ch19.domain.Contact;
import com.apress.prospring3.ch19.service.ContactService;
import com.apress.prospring3.ch19.test.annotation.DataSets;
public class ContactServiceImplTest extends AbstractServiceImplTest {
@Autowired
ContactService customerService;
@DataSets(
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home