Unit test a DAO
dao, easymock, java, testing
Solution
It looks as though `getSimpleJdbcTemplate` is the biggest problem for unit testing. One way you could test is to extend the class under test and override the `getSimpleJdbcTemplate` method e.g.
public class ElectionDaoTest {
/** Class under test */
private ElectionsDaoImpl dao;
@Before
public void setUp() {
dao = new ElectionsDaoImpl(){
SimpleJdbcTemplate getSimpleJdbcTemplate(){
// Return easy mock version here.
}
};
}
@Test
// Do tests
}
There may be an easier way with EasyMock, but I'm not that familiar with it.
Problem
I've been trying to do a unit test of my DAO but I haven't find out the way to do it yet and I'm feeling a little desperate. I have a tiny DAO that looks like this: ``` public interface ElectionsDao { List<String> getDates(); } ``` I'm using Spring framework to do DI using `SimpleJdbcTemplate`. My implementation looks like this: ``` public class ElectionsDaoImpl extends SimpleJdbcDaoSupport implements ElectionsDao { public List<String> getDates() { List<String> dates = new ArrayList<String>(); try { dates = getSimpleJdbcTemplate().query("SELECT electiondate FROM electiondate", new StringRowMapper()); } catch (DataAccessException ex){ throw new RuntimeException(ex); } return dates; } protected static final class StringRowMapper implements ParameterizedRowMapper<String> { public String mapRow(ResultSet rs, int line) throws SQLException { String string = new String(rs.getString("electiondate")); return string; } } } ``` What I want to do is just a unit test of `getDates()` using EasyMock but I haven't found the way to do it. I'm so confused. Can anybody help me please?