Print
Testing
WORK IN PROGRESS

Testing Guide

Outline

  • Testing the persistence service.
  • Functional Testing with HtmlUnit
  • Functional Testing with Selenium

Testing the persistence service.

http://www.theserverside.com/tt/articles/article.tss?l=PersistentDomain

http://www.springframework.org/docs/reference/testing.html

package org.amneris;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import org.trails.persistence.PersistenceService;

/**
 * Class for MyDomainObject tests.
 * 
 * <p>
 * This class extends AbstractTransactionalDataSourceSpringContextTests, one of
 * the valuable test superclasses provided in the org.springframework.test
 * package. This represents best practice for integration tests with Spring. The
 * AbstractTransactionalDataSourceSpringContextTests superclass provides the
 * following services:
 * </p>
 * 
 * <li>Injects test dependencies, meaning that we don't need to perform
 * application context lookups. Injection uses autowiring by type.</li>
 * 
 * <li>Executes each test method in its own transaction, which is automatically
 * rolled back by default. This means that even if tests insert or otherwise
 * change database state, there is no need for a teardown or cleanup script.</li>
 * 
 * <ul>
 * <li>If you want a transaction to commit--unusual, but useful if you want a
 * particular test to populate the database, for example--you can call the
 * setComplete() method inherited from AbstractTransactionalSpringContextTests.
 * This will cause the transaction to commit instead of roll back.</li>
 * 
 * <li>There is also convenient ability to end a transaction before the test
 * case ends, through calling the endTransaction() method. This will roll back
 * the transaction by default, and commit it only if setComplete() had
 * previously been called.</li>
 * </ul>
 * 
 * <li>Provides useful inherited protected fields, such as a JdbcTemplate that
 * can be used to verify database state after test operations, or verify the
 * results of queries performed by application code. An ApplicationContext is
 * also inherited, and can be used for explicit lookup if necessary.</li>
 * <p>
 * The AbstractTransactionalDataSourceSpringContextTests and related classes are
 * shipped in the spring-mock.jar.
 * </p>
 * 
 * @see http://www.theserverside.com/tt/articles/article.tss?l=PersistentDomain
 * @see http://www.springframework.org/docs/reference/testing.html
 * @see org.springframework.samples.petclinic.AbstractClinicTests
 * @see org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
 * 
 * @author Alejandro Scandroli
 */
public class MyDomainObjectTest extends
		AbstractTransactionalDataSourceSpringContextTests {

	@Override
	protected String[] getConfigLocations() {
		return new String[] { "applicationContext.xml" };
	}

	PersistenceService persistenceService;

	public void setPersistenceService(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	public void testCreate() {
		MyDomainObject myDomainObject = new MyDomainObject();
		myDomainObject.setName("some name");
		myDomainObject = persistenceService.save(myDomainObject);
		assertEquals(myDomainObject.getName(), "some name");
		assertNotNull(myDomainObject.getId());
	}

	public void testRetrieve() {
		MyDomainObject myDomainObject = new MyDomainObject();
		myDomainObject.setName("some name");
		myDomainObject = persistenceService.save(myDomainObject);
		assertEquals(myDomainObject.getName(), "some name");
		assertNotNull(myDomainObject.getId());

		MyDomainObject myReturnedDomainObject;
		myReturnedDomainObject = persistenceService.getInstance(
				MyDomainObject.class, myDomainObject.getId());

		assertEquals(myDomainObject, myReturnedDomainObject);
		assertEquals(myDomainObject.getName(), myReturnedDomainObject.getName());

	}

	public void testUpdate() {
		MyDomainObject myDomainObject = new MyDomainObject();
		myDomainObject.setName("some name");
		myDomainObject = persistenceService.save(myDomainObject);
		assertEquals(myDomainObject.getName(), "some name");
		assertNotNull(myDomainObject.getId());

		MyDomainObject myReturnedDomainObject;

		myReturnedDomainObject = persistenceService.getInstance(
				MyDomainObject.class, myDomainObject.getId());

		assertEquals(myDomainObject, myReturnedDomainObject);
		assertEquals(myDomainObject.getName(), myReturnedDomainObject.getName());

		myReturnedDomainObject.setName("some other name");
		Integer id = myReturnedDomainObject.getId();

		myReturnedDomainObject = persistenceService
				.save(myReturnedDomainObject);

		assertEquals(id, myReturnedDomainObject.getId());
		assertEquals("some other name", myReturnedDomainObject.getName());
	}

	public void testDelete() {
		MyDomainObject myDomainObject = new MyDomainObject();
		myDomainObject.setName("some name");
		myDomainObject = persistenceService.save(myDomainObject);
		assertEquals(myDomainObject.getName(), "some name");
		assertNotNull(myDomainObject.getId());

		Integer id = myDomainObject.getId();

		persistenceService.remove(myDomainObject);
		MyDomainObject myReturnedDomainObject = null;
		try {
			myReturnedDomainObject = persistenceService.getInstance(
					MyDomainObject.class, id);
		} catch (HibernateObjectRetrievalFailureException e) {
		}

		assertNull(myReturnedDomainObject);
	}

	public void testGetAllInstances() {
		MyDomainObject firstDomainObject = new MyDomainObject();
		firstDomainObject.setName("this is the first one");
		firstDomainObject = persistenceService.save(firstDomainObject);

		MyDomainObject secondDomainObject = new MyDomainObject();
		secondDomainObject.setName("this is the second object");
		secondDomainObject = persistenceService.save(secondDomainObject);

		List<MyDomainObject> objectList = persistenceService
				.getAllInstances(MyDomainObject.class);

		assertFalse(objectList.isEmpty());
		int i = objectList.indexOf(firstDomainObject);
		assertTrue(i >= 0);

		assertFalse(objectList.isEmpty());
		int j = objectList.indexOf(secondDomainObject);
		assertTrue(j >= 0);

		MyDomainObject myReturnedDomainObject;

		myReturnedDomainObject = objectList.get(i);

		assertEquals(firstDomainObject, myReturnedDomainObject);
		assertEquals(firstDomainObject.getName(), myReturnedDomainObject
				.getName());
	}

	public void testSearchByDetachedCriteria() {
		MyDomainObject firstDomainObject = new MyDomainObject();
		firstDomainObject.setName("this is the first one");
		firstDomainObject = persistenceService.save(firstDomainObject);

		MyDomainObject secondDomainObject = new MyDomainObject();
		secondDomainObject.setName("this is the second object");
		secondDomainObject = persistenceService.save(secondDomainObject);

		DetachedCriteria criteria = DetachedCriteria
				.forClass(MyDomainObject.class);
		criteria.add(Restrictions.like("name", "first", MatchMode.ANYWHERE));

		List<MyDomainObject> objectList = (List<MyDomainObject>) persistenceService
				.getInstances(criteria);

		assertFalse(objectList.isEmpty());
		int i = objectList.indexOf(firstDomainObject);
		assertTrue(i >= 0);

		int j = objectList.indexOf(secondDomainObject);
		assertTrue(j == -1);

		MyDomainObject myReturnedDomainObject;

		myReturnedDomainObject = objectList.get(i);

		assertEquals(firstDomainObject, myReturnedDomainObject);
		assertEquals(firstDomainObject.getName(), myReturnedDomainObject
				.getName());
	}
}

Functional Testing with HtmlUnit

//TODO

Functional Testing with Selenium

See http://shale.apache.org/shale-apps/selenium.html (Thank you Wendy Smoak)

Install the Firefox plugin Selenium IDE from http://www.openqa.org/selenium-ide/

<h3 align="right" style="margin-right:10px"><a href="selenium/core/TestRunner.html?test=../tests/TestSuite.html">Selenium Tests</a></h3>
<!-- See http://shale.apache.org/shale-apps/selenium.html -->
    <!-- Thank you Wendy Smoak-->
    <profiles>
        <profile>
            <id>selenium</id>
            <activation>
                <property>
                    <name>selenium</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.mortbay.jetty</groupId>
                        <artifactId>maven-jetty-plugin</artifactId>
                        <version>6.1.1</version>
                        <configuration>
                            <contextPath>/</contextPath>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <systemProperties>
                                <systemProperty>
                                    <name>slf4j</name>
                                    <value>false</value>
                                </systemProperty>
                                <systemProperty>
                                    <name>log4j.configuration</name>
                                    <value>file:${basedir}/target/classes/log4j.properties</value>
                                </systemProperty>
                            </systemProperties>
                            <webAppSourceDirectory>${project.build.directory}/${project.build.finalName}
                            </webAppSourceDirectory>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>commons-logging</groupId>
                                <artifactId>commons-logging</artifactId>
                                <version>1.1</version>
                            </dependency>
                            <dependency>
                                <groupId>org.slf4j</groupId>
                                <artifactId>slf4j-jcl</artifactId>
                                <version>1.0.1</version>
                            </dependency>
                            <dependency>
                                <groupId>log4j</groupId>
                                <artifactId>log4j</artifactId>
                                <version>1.2.12</version>
                            </dependency>
                        </dependencies>
                    </plugin>

                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>dependency-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>unzip-selenium</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>org.openqa.selenium.core</groupId>
                                            <artifactId>selenium-core</artifactId>
                                            <version>0.8.2</version>
                                        </artifactItem>
                                    </artifactItems>
                                    <outputDirectory>${project.build.directory}/selenium</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-selenium</id>
                                <phase>process-resources</phase>
                                <configuration>
                                    <tasks>
                                        <copy todir="${project.build.directory}/${project.build.finalName}">
                                            <fileset dir="${basedir}/src/main/webapp"/>
                                        </copy>
                                        <copy todir="${project.build.directory}/${project.build.finalName}/selenium/core">
                                            <fileset dir="${project.build.directory}/selenium/core"/>
                                        </copy>
                                        <copy todir="${project.build.directory}/${project.build.finalName}/selenium/tests">
                                            <fileset dir="${basedir}/src/test/selenium"/>
                                        </copy>
                                    </tasks>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
            <repositories>
                <repository>
                    <id>OpenQA</id>
                    <url>http://maven.openqa.org</url>
                </repository>
            </repositories>
        </profile>
    </profiles>
Powered by Atlassian Confluence