Tag: testing

unit testing使用Spring JDBC的DAO类

我有几个DAO对象用于从数据库中检索信息,我真的想为它们编写一些自动化测试,但我很难弄清楚如何去做。 我正在使用Spring的JdbcTemplate运行实际查询(通过JdbcTemplate准备语句)并将结果映射到模型对象(通过RowMapper类)。 如果我要编写unit testing,我不确定如何/应该模拟对象。 例如,由于只有读取,我会使用实际的数据库连接而不是模拟jdbcTemplate,但我不确定是否正确。 这是批次中最简单的DAO的(简化)代码: /** * Implementation of the {@link BusinessSegmentDAO} interface using JDBC. */ public class GPLBusinessSegmentDAO implements BusinessSegmentDAO { private JdbcTemplate jdbcTemplate; private static class BusinessSegmentRowMapper implements RowMapper { public BusinessSegment mapRow(ResultSet rs, int arg1) throws SQLException { try { return new BusinessSegment(rs.getString(…)); } catch (SQLException e) { return null; } […]