单元测试使用Spring JDBC的DAO类 [英] Unit testing a DAO class that uses Spring JDBC

查看:128
本文介绍了单元测试使用Spring JDBC的DAO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个DAO对象用于从数据库中检索信息,我真的想为它们编写一些自动化测试,但我很难搞清楚如何去做。

I have several DAO objects that are used to retrieve information from a database and I really want to write some automated tests for them but I'm having a hard time figuring out how to do it.

我正在使用Spring的 JdbcTemplate 来运行实际查询(通过预备语句)并将结果映射到模型对象(通过 RowMapper 类)。

I'm using Spring's JdbcTemplate to run the actual query (via a prepared statement) and map the results to the model object (via the RowMapper class).

如果我要编写单元测试,我不是确定我将如何/应该模拟对象。例如,由于只有读取,我会使用实际的数据库连接,而不是模拟jdbcTemplate,但我不确定是不是。

If I were to write unit tests, I'm not sure how I would/should mock the objects. For example, since there are only reads, I would use the actual database connection and not mock the jdbcTemplate, but I'm not sure that's right.

这是(简化) )批次中最简单的DAO的代码:

Here's the (simplified) code for the simplest DAO of the batch:

/**
 * Implementation of the {@link BusinessSegmentDAO} interface using JDBC.
 */
public class GPLBusinessSegmentDAO implements BusinessSegmentDAO {
    private JdbcTemplate jdbcTemplate;

    private static class BusinessSegmentRowMapper implements RowMapper<BusinessSegment>  {
        public BusinessSegment mapRow(ResultSet rs, int arg1) throws SQLException { 
            try {
                return new BusinessSegment(rs.getString(...));
            } catch (SQLException e) {
                return null;
            }
        }
    }

    private static class GetBusinessSegmentsPreparedStatementCreator 
        implements PreparedStatementCreator {
        private String region, cc, ll;
        private int regionId;

        private GetBusinessSegmentsPreparedStatementCreator(String cc, String ll) {
            this.cc = cc;
            this.ll = ll;
        }

        public PreparedStatement createPreparedStatement(Connection connection)
                throws SQLException {           
            String sql = "SELECT ...";

            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, cc);
            ps.setString(2, ll);
            return ps;
        }
    }

    public GPLBusinessSegmentDAO(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public Collection<BusinessSegment> getBusinessSegments(String cc, String ll) {
        return jdbcTemplate.query(
                new GetBusinessSegmentsPreparedStatementCreator(cc, ll), 
                new BusinessSegmentRowMapper());
    }

}

任何想法都会受到赞赏。

Any idea would be appreciated.

谢谢!

推荐答案

请看下面的链接:


  1. 使用Spring和DbUnit测试SQL查询

  2. 使用JdbcTemplate测试代码的MockObjects或DBUnit

  1. Testing SQL queries with Spring and DbUnit
  2. MockObjects or DBUnit for testing Code using JdbcTemplate

希望这有帮助。

编辑:

这是GitHub版本的RowMapperTests ,以便于参考。

这篇关于单元测试使用Spring JDBC的DAO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆