Announcement Announcement Module
No announcement yet.
MockObjects or DBUnit for testing Code using JdbcTemplate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • MockObjects or DBUnit for testing Code using JdbcTemplate


    I'm looking at a way to test the following code against a mock object,
    or test database using DBUnit:
    public List getLinePairs() {
            JdbcTemplate targetTemplate = new JdbcTemplate(target);
            RowMapper hqLineMapper = new RowMapper() {
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    return new LinePair(rs.getString(hqAbbrColumnName), rs.getString(catAbbrColumnName));
            return targetTemplate.query(selectTargetHqLineSql, hqLineMapper);
    What is the recommended approach? MockObjects' MockDataSource and MockConnection2 seem easy enough to setup, but I'm getting an NPE because the statment wrapped by targetTemplate.query() is null.

    Is it easier or more appropriate to setup a dbunit database and test the DataSource code against a real database? Or is there a secret getting all the right mock statements initialized?

  • #2
    puffybsd, you can review Spring tests for JDBC abstraction layer (from Spring distribution), more particulary org.springframework.jdbc.core.RowMapperTests' method testStaticQueryWithRowMapper.



    • #3


      Thanks, that's exactly what I was looking for! My new motto is RTFT.


      • #4
        Although this is an old post maybe someone may weigh in on my question

        I am looking for a way to setup a test suite for a site that is using EJB-JSF-Hibernate and was hoping to hook into the EJBs using SpringSecurity... would I be able to add spring integration easily ? I see Part III 9. testing and Part VI Integration 20. EJBs that it looks doable but was wondering how hard that would be ?

        Can you mix Spring and JSF configurations? Usually you put your test src code in a test pkg alongside the main... would I have to run another 'spring module' configured alongside the code I want to test?

        Hoping for some insight, any responses welcome (Thx) !