More Mandatory Mocking


package net.unicon;
import java.sql.*;
import net.unicon.util.DBUtil;

public class Thing3 {

    private static final String query = "select score from exam_result_smv where er_id = ?";
    
    public int testMe(Connection connection, int erId) throws SQLException {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int score = 0;
        try {
            stmt = connection.prepareStatement(query);
	    stmt.setInt(1, erId);
	    rs = stmt.executeQuery();
	    while (rs.next()) {
	        score = rs.getInt(1);
	    }
        } finally {
	    DBUtil.safeClose(rs);
	    DBUtil.safeClose(stmt);
        }
	return score;
    }
}

package net.unicon;

import java.sql.*;

class ThingTest3 extends GroovyTestCase { 

  void testTestMe() {
    int erId  = 42;  /erId to be looked up
    int score = 21;  //score to be returned

    int rowcount = 0;  //state for result set
    def rs = [ getInt : { pos -> assertEquals(1, pos); score; },
               next : { -> rowcount++ == 0 },  //lexical scope is important
	       close : { -> } 
             ] as ResultSet;

    def stmt = [ executeQuery : { -> rs; }, //no arguments to this closure
                 setInt : { pos, id -> 
                            assertEquals(1, pos); 
	                    assertEquals(erId, id);
	                  },
	         close : { -> } 
               ] as PreparedStatement;


    def conn = [ prepareStatement :  { q->  
           assertEquals(Thing3.query, q); //Whoa!!
	          return stmt;  //can't resist "return"
    }] as Connection;

    def thing = new Thing3();
    assertEquals(score, thing.testMe(conn, erId));
  }
}

Next