Table of Content
1. Create a DAO Test
2. Create a DAO Interface and implementation
3. Commit all related changes
We will use Test-Driven Development model. that's why we'll create a DaoTest to test that your DAO works. To start, create a ProjectDaoTest.java class in your src/test/java/**/dao directory. This class should extend org.appfuse.dao.BaseDaoTestCase.
package com.ibm.ics.dao;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.test.annotation.ExpectedException;
import com.ibm.ics.model.Project;
public class ProjectDaoTest extends BaseDaoTestCase {
@Autowired
private ProjectDao projectDao;
@Test
public void testFindProjectByName() throws Exception {
List<Project> people = projectDao.findByName("connections");
assertTrue(people.size() > 0);
}
@Test
@ExpectedException(DataAccessException.class)
public void testAddAndRemoveProject() throws Exception {
Project project = new Project();
project.setName("sametime");
project.setDescription("sametime project");
project = projectDao.save(project);
flush();
project = projectDao.get(project.getId());
assertEquals("sametime", project.getName());
assertNotNull(project.getId());
log.debug("removing project...");
projectDao.remove(project.getId());
flush();
// should throw DataAccessException
projectDao.get(project.getId());
}
}
The code you see above is what you need for a basic Spring integration test that initializes and configures an implementation of ProjectDao. Spring will use autowiring to set the "ProjectDao" bean as a dependency of this class.
You'll notice that this method relies on pre-existing data in order to pass. The DbUnit Maven Plugin is used to populate the database with test data before the tests are run, so you can simply add the new table/record to the src/test/resources/sample-data.xml file .
<table name='project'>
<column>id</column>
<column>name</column>
<column>description</column>
<row>
<value>1</value>
<value>connections</value>
<value>this is connections project description.</value>
</row>
</table>
If you reformat the XML in sample-data.xml, make sure no line breaks are added inside the tag values. If the xml is reformated <password>xxxxxxx</password> will be broken into three lines. Since the password was no longer decryptable with extra tabs and newline characters, no one can log in under any name. This is a feature of DBUnit that allows adding any characters to the database content.
Create a ProjectDao.java interface in the src/main/java/**/dao directory and specify the finder method for any implementation classes.
package com.ibm.ics.dao;
import java.util.List;
import com.ibm.ics.model.Project;
public interface ProjectDao extends GenericDao<Project, Long> {
public List<Project> findByName(String name);
}
At this point, you should be able to compile all your code using your IDE or mvn test-compile. However, if you try to run mvn test -Dtest=ProjectDaoTest, you will get some errors which are indicating that you need to specify a bean named "ProjectDao". To do that you need to create the ProjectDao implementation.
Create a ProjectDaoHibernate class that implements the finder method in ProjectDao. To do this, create a new class in src/main/java/**/dao/hibernate and name it ProjectDaoHibernate.java. It should extend GenericDaoHibernate and implement ProjectDao.
package com.ibm.ics.dao.hibernate;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.ibm.ics.dao.ProjectDao;
import com.ibm.ics.model.Project;
@Repository("projectDao")
public class ProjectDaoHibernate extends GenericDaoHibernate<Project, Long> implements
ProjectDao {
public ProjectDaoHibernate() {
super(Project.class);
}
public List<Project> findByName(String name) {
return getHibernateTemplate().find("from Project where name=?", name);
}
}
The @Repository annotation indicates this is a Spring bean. Now, if you try to run mvn test -Dtest=ProjectDaoTest, it should pass.
you can now compose one git commit with all new/changed files below.
src/test/resources/sample-data.xml
src/main/java/com/ibm/ics/dao/ProjectDao.java
src/main/java/com/ibm/ics/dao/hibernate/ProjectDaoHibernate.java
src/test/java/com/ibm/ics/dao/ProjectDaoTest.java