Unit Testing DB Layer in Seam

Posted on Friday, February 19, 2010

5


In our earlier post we talked about the preferred Seam architecture for an enterprise. As you would notice from various Seam forums that Seam would advocate thick objects and would not recommend the layered architecture for various applications. However, if you are working in an enterprise and want to adopt Seam for application development then going the layered way is your best bet.

Let us discuss the unit testing of these layers one by one using the inherent functions and features provided by Seam. Starting with the DB layer. Most of your applications would require some kind of interaction with the resource layer. This is where unit testing the resource layer comes in handy.

Ideally you would not like to test the database layer against the actual database. A good way to unit test the DAO layer is by using an in memory database like hsqldb. This would mean a change in the persistence.xml. Let us see how our persistence.xml looks like

<persistence>
	<persistence-unit name="testworld">
		<class>com.inphina.ike.domain.Ike</class>
		<class>com.inphina.ike.domain.IkeSession</class>
		<class>com.inphina.ike.domain.Employee</class>
		<class>com.inphina.ike.domain.FeedBack</class>
		<class>com.inphina.ike.domain.Employee</class>
		<properties>
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="false" />

			<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
			<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:aname" />
			<property name="hibernate.connection.username" value="sa" />

			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
		</properties>
	</persistence-unit>
</persistence>

As you would notice that the persistence-unit is named as “testworld”, this would help us create an EntityManagerFactory in our tests which connects to the in memory database.

Let us look like how a simple setup method would look like

@Before
	public void setUp() {
		ikeDaoImpl = new IkeDaoImpl();
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("testworld");
		entityManager = emf.createEntityManager();

		// injecting the entityManager
		setField(ikeDaoImpl, "em", entityManager);
	}

As you would notice that we are using the “testworld” persistence unit. One other interesting observation that you would make is the way we are injecting the entityManager into our dao.

The Dao has a Seam injection for the entity manager defined like this

@Name("ikeDao")
@AutoCreate
public class IkeDaoImpl implements IkeDao {

	@In(create=true)
	protected EntityManager em;
        .....
        .....

While unit testing Seam provides a class called SeamTest which helps us with the injections. We simply use the setField method to inject like this

// injecting the entityManager
		setField(ikeDaoImpl, "em", entityManager);

So now you have the complete infrastructure ready. You have the test injected with the right entityManager which is pointing to the in memory test database.

A very simple test could look something like this

public class IkeDaoImplTest extends SeamTest {

	private EntityManager entityManager;
	private IkeDaoImpl ikeDaoImpl;

	@Before
	public void setUp() {
		ikeDaoImpl = new IkeDaoImpl();
		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("testworld");
		entityManager = emf.createEntityManager();
		// injecting the entityManager
		setField(ikeDaoImpl, "em", entityManager);

	}

	@Test
	public void fetchAllIke() {

		Ike ike = new Ike();
		ike.setTheme("Basics");
		ike.setDate(new Date());

		entityManager.getTransaction().begin();
		ikeDaoImpl.createIke(ike);
		entityManager.getTransaction().commit();
		List<Ike> allIke = ikeDaoImpl.fetchAllIke();
		Assert.assertEquals(1, allIke.size());
	}

}

Get in touch

Interested to know more about our JBoss Seam capabilities? Get in touch with us on seam@inphina.com

Advertisements
Tagged: , ,
Posted in: Java