3. Services Test

The Services tests and their test base are for testing the services use of the Spring Data JPA repositories, conversion between the entity and ws bean models, and business logic (if any). Also security and transactional configuration are loaded and used during the tests. All database operations are performed against an in memory database.

Spring Configuration

The Services Spring test configuration loads the main security, marshaller, DB, and services config. It doesn't use any special test configuration or mocks, but anything different from the standard production Spring configuration could be added here.

services-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/security/**/*-context.xml"/>
    <import resource="classpath*:/META-INF/spring/marshaller/**/*-context.xml"/>
    <import resource="classpath*:/META-INF/spring/db/**/*-context.xml"/>
    <import resource="classpath*:/META-INF/spring/services/**/*-context.xml"/>
    
</beans>
                    
                

Abstract Code

The AbstractServiceTest extends the shared transactional profile test abstract class. It sets the shared test configuration to services-test-context.xml.

Example 5. AbstractServiceTest

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

Code Example

The ContactServiceTest tests the basic usage of the ContactService. This primarily covers usage of it's JPA repository and conversion to and from entity beans & ws beans.

Example 6. ContactServiceTest

                    
public class ContactServiceTest extends AbstractServiceTest {

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

    @Autowired
    private ContactService service;
        
    @Test
    public void testFindOne() {
        PersonResponse response = service.findById(FIRST_ID);
        Person person = response.getResults();

        testPersonOne(person);
    }
    
    @Test
    public void testFindAll() {
        PersonFindResponse response = service.find();
        assertNotNull("Person response is null.", response);
        
        Collection<Person> persons = response.getResults();

        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 testCreate() {
        String firstName = "Jack";
        String lastName = "Johnson";
        
        PersonResponse response = createPerson(firstName, lastName);
        assertNotNull("Person response is null.", response);
        
        Person person = response.getResults();
        
        // test saved person
        testPerson(person, 
                   firstName, lastName);

        PersonFindResponse findResponse = service.find();
        assertNotNull("Person response is null.", findResponse);
        
        Collection<Person> persons = findResponse.getResults();

        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() {
        PersonResponse response = service.findById(FIRST_ID);
        assertNotNull("Person response is null.", response);
        
        Person person = response.getResults();
        
        testPersonOne(person);
        
        String lastName = "Jones"; 
        person.setLastName(lastName);

        service.update(person);

        response = service.findById(FIRST_ID);
        assertNotNull("Person response is null.", response);
        
        person = response.getResults();
        
        testPersonOne(person, lastName);
    }

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

        // person should be null after delete
        PersonResponse response = service.findById(FIRST_ID);
        assertNotNull("Person response is null.", response);
        
        Person person = response.getResults();

        assertNull("Person is not null.", person);
    }

    /**
     * Create person.
     */
    private PersonResponse createPerson(String firstName, String lastName) {
        Person person = new Person();

        person.setFirstName(firstName);
        person.setLastName(lastName);
        
        PersonResponse response = service.create(person);
        
        return response;
    }
    
    /**
     * 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) {
        testPerson(person, 
                   FIRST_NAME, lastName);
    }

    /**
     * Tests person with a PK of two.
     */
    private void testPersonTwo(Person person) {
        String firstName = "John";
        String lastName = "Wilson";

        testPerson(person, 
                   firstName, lastName);
    }

    /**
     * Tests person.
     */
    private void testPerson(Person person, 
                            String firstName, String lastName) {
        assertNotNull("Person is null.", person);
        
        assertEquals("firstName", firstName, person.getFirstName());
        assertEquals("lastName", lastName, person.getLastName());
                
        testAuditable(person);
    }

    /**
     * Tests auditable entity.
     */
    private void testAuditable(PkEntityBase auditRecord) {
        assertNotNull("lastUpdated", auditRecord.getLastUpdated());
        assertNotNull("lastUpdatedBy", auditRecord.getLastUpdateUser());
        assertNotNull("created", auditRecord.getCreated());
        assertNotNull("createdBy", auditRecord.getCreateUser());
    }
 
}