2. DAO Test

The DAO tests and their test base are for testing the JPA entities & Spring Data JPA repositories against an in memory database.

Spring Configuration

The DAO test config is very simple and just loads all of the DB Spring configuration files. All Spring XML configuration files end in -context.xml and test ones end in -test-context.xml

dao-test-context.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath*:/META-INF/spring/db/**/*-context.xml"/>
    
</beans>
                    
                

Abstract Code

Most of the test configuration was already configured in the parent test classes. All that is left is to configure the test Spring configuration to use by setting the @ContextConfiguration. As each test extending this class runs, it will automatically use this shared Spring test context.

Example 3. AbstractRepositoryTest

                    
@ContextConfiguration({ "classpath:/dao-test-context.xml" })
public abstract class AbstractRepositoryTest extends AbstractTransactionalProfileTest {
 
}
                    
                

Code Example

The person repository test is for the Spring Data JPA PersonRepository. There is in progress work for having Person subclasses work through the entire stack.

Example 4. PersonRepositoryTest

                    
public class PersonRepositoryTest extends AbstractRepositoryTest {

    final Logger logger = LoggerFactory.getLogger(PersonRepositoryTest.class);

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private ProfessionalRepository professionalRepository;
    
    @Test
    public void testFindOne() {
        Person person = personRepository.findOne(FIRST_ID);

        testPersonOne(person);
    }
    
    @Test
    public void testFindAll() {
        Collection<Person> persons = personRepository.findAll();

        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + EXPECTED_COUNT + ".", EXPECTED_COUNT, persons.size());
        
        for (Person person : persons) {
            logger.debug(person.toString());
            
            if (FIRST_ID.equals(person.getId())) {                
                testPersonOne(person);
            } else if (SECOND_ID.equals(person.getId())) {
                testPersonTwo(person);
            }
        }
    }

    @Test
    public void testFindByFirstNameLike() {
        List<Person> persons = personRepository.findByFirstNameLike("J%");
        
        int expectedCount = 2;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());
        
        Person person = persons.get(0);
        
        testPersonOne(person);
    }

    @Test
    public void testFindByLastName() {
        List<Person> persons = personRepository.findByLastName(LAST_NAME);

        int expectedCount = 1;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());

        Person person = persons.get(0);
        
        testPersonOne(person);
    }

    @Test
    public void testFindByAddress() {
        List<Person> persons = personRepository.findByAddress(ADDR);
        
        int expectedCount = 1;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());
        
        Person person = persons.get(0);
        
        testPersonOne(person);
    }

    @Test
    public void testFindByAddressPage() {
        String firstName = "Jack";
        String lastName = "Johnson";
        String companyName = "Spring Pizza";

        int page = 0;
        int size = 10;

        for (int i = 0; i < 35; i++) {
            createProfessional(firstName, lastName, companyName, ADDR);
        }
        
        Page<Person> pageResult = personRepository.findByAddress(ADDR, new PageRequest(page, size));
        List<Person> persons = pageResult.getContent();
        
        int expectedCount = size;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());

        // query last page
        page = pageResult.getTotalPages() - 1;
        pageResult = personRepository.findByAddress(ADDR, new PageRequest(page, size));
        persons = pageResult.getContent();
        
        // created 35 records with the same address, one existing
        expectedCount = 6;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());
        
    }

    @Test
    public void testFindByName() {
        List<Person> persons = personRepository.findByName(FIRST_NAME, LAST_NAME);
        
        int expectedCount = 1;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());
        
        Person person = persons.get(0);
        
        testPersonOne(person);
    }

    @Test
    public void testSave() {
        String firstName = "Jack";
        String lastName = "Johnson";
        String companyName = "Spring Pizza";
        
        Person person = createProfessional(firstName, lastName, companyName, ADDR);
        
        // get PK of first address
        Integer addressId = person.getAddresses().iterator().next().getId();

        // test saved person
        testPerson(person, 
                   firstName, lastName,  
                   EXPECTED_ADDRESS_COUNT, addressId, ADDR, CITY, STATE, ZIP_POSTAL,
                   true, companyName);

        person = professionalRepository.findOne(person.getId());

        // test retrieved person just saved
        testPerson(person, 
                   firstName, lastName,  
                   EXPECTED_ADDRESS_COUNT, addressId, ADDR, CITY, STATE, ZIP_POSTAL,
                   true, companyName);

        Collection<Person> persons = personRepository.findAll();

        int expectedCount = EXPECTED_COUNT + 1;
        
        assertNotNull("Person list is null.", persons);
        assertEquals("Number of persons should be " + expectedCount + ".", expectedCount, persons.size());
    }

    @Test
    public void testUpdate() {
        Person person = personRepository.findOne(FIRST_ID);
        testPersonOne(person);
        
        String lastName = "Jones"; 
        person.setLastName(lastName);
        
        personRepository.saveAndFlush(person);

        person = personRepository.findOne(FIRST_ID);
        testPersonOne(person, lastName);
    }

    @Test
    public void testDelete() {
        personRepository.delete(FIRST_ID);

        // person should be null after delete
        Person person = personRepository.findOne(FIRST_ID);
        assertNull("Person is not null.", person);
    }

    /**
     * Create professional.
     */
    private Person createProfessional(String firstName, String lastName, String companyName,
                                            String addr) {
        Professional person = new Professional();
        Set<Address> addresses = new HashSet<Address>();
        Address address = new Address();
        addresses.add(address);
        
        address.setAddress(addr);
        address.setCity(CITY);
        address.setState(STATE);
        address.setZipPostal(ZIP_POSTAL);
        address.setCountry(COUNTRY);
        
        person.setFirstName(firstName);
        person.setLastName(lastName);
        person.setCompanyName(companyName);
        person.setAddresses(addresses);
        
        Person result = personRepository.saveAndFlush(person);
        
        return result;
    }
    
    /**
     * Tests person with a PK of one.
     */
    private void testPersonOne(Person person) {
        testPersonOne(person, LAST_NAME);
    }
    
    /**
     * Tests person with a PK of one.
     */
    private void testPersonOne(Person person, String lastName) {
        String schoolName = "NYU";
        
        Integer addressId = new Integer(1);
        
        testPerson(person, 
                   FIRST_NAME, lastName,  
                   EXPECTED_ADDRESS_COUNT, addressId, ADDR, CITY, STATE, ZIP_POSTAL,
                   false, schoolName);
    }

    /**
     * Tests person with a PK of two.
     */
    private void testPersonTwo(Person person) {
        String firstName = "John";
        String lastName = "Wilson";
        String companyName = "Spring Pizza";
        
        int expectedAddresses = 2;
                
        Integer addressId = new Integer(3);
        String addr = "47 Howard St.";
        String city = "San Francisco";
        String state = "CA";
        String zipPostal = "94103";

        testPerson(person, 
                   firstName, lastName,  
                   expectedAddresses, addressId, addr, city, state, zipPostal,
                   true, companyName);
    }

    /**
     * Tests person.
     */
    private void testPerson(Person person, 
                            String firstName, String lastName, 
                            int expectedAddresses, Integer addressId,
                            String addr, String city, String state, String zipPostal,
                            boolean professional, String professionName) {
        assertNotNull("Person is null.", person);
        
        assertEquals("firstName", firstName, person.getFirstName());
        assertEquals("lastName", lastName, person.getLastName());
        
        assertNotNull("Person's address list is null.", person.getAddresses());
        assertEquals("addresses", expectedAddresses, person.getAddresses().size());
        
...
        
        for (Address address : person.getAddresses()) {
            assertNotNull("Address is null.", address);
            
            if (addressId.equals(address.getId())) {
                assertEquals("address.id", addressId, address.getId());
                assertEquals("address.addr", addr, address.getAddress());
                
                assertEquals("address.city", city, address.getCity());
                assertEquals("address.state", state, address.getState());
                assertEquals("address.zipPostal" + zipPostal + "'.", zipPostal, address.getZipPostal());
                assertEquals("address.country", COUNTRY, address.getCountry());
                
                testAuditable(address);
            }
        }
        
        testAuditable(person);
    }

    /**
     * Tests auditable entity.
     */
    private void testAuditable(AbstractAuditableEntity auditRecord) {
        assertNotNull("lastUpdated", auditRecord.getLastModifiedDate());
        assertNotNull("lastUpdatedBy", auditRecord.getLastModifiedBy());
        assertNotNull("created", auditRecord.getCreatedDate());
        assertNotNull("createdBy", auditRecord.getCreatedBy());
    }
 
}