ユニットテストresultsetの結果
5313 ワード
junit単測を書くときはjdbcのresultsetをシミュレートする必要があります.次のテストmockクラスを使用しました.
<mockrunner.version>0.4.1</mockrunner.version>
<dependency>
<groupId>com.mockrunner</groupId>
<artifactId>mockrunner</artifactId>
<scope>test</scope>
</dependency>
MockResultSet rs = new MockResultSet("myMock");
rs.addColumn("columnA", new Integer[]{1});
rs.addColumn("columnB", new String[]{"Column B Value"});
rs.addColumn("columnC", new Double[]{2});
// make sure to move the cursor to the first row
try
{
rs.next();
}
catch (SQLException sqle)
{
fail("unable to move resultSet");
}
// process the result set
MyObject obj = processor.processResultSet(rs);
// run your tests using the ResultSet like you normally would
assertEquals(1, obj.getColumnAValue());
assertEquals("Column B Value", obj.getColumnBValue());
assertEquals(2.0d, obj.getColumnCValue());
自分で書いた:
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
/**
* <pre>
* TaskInitInfoRowMapper.java
* @author xuepeng01<br>
* @version 1.0
* Create Time 2015 6 4 8:44:27<br>
* Description : TaskInitInfo RowMapper
* </pre>
*/
public class TaskInitInfoRowMapper implements RowMapper<TaskInitInfo> {
/*
* (non-Javadoc)
* @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet,
* int)
*/
@Override
public TaskInitInfo mapRow(ResultSet rs, int arg1) throws SQLException {
TaskInitInfo obj = new TaskInitInfo();
obj.setId(rs.getLong("id"));
obj.setTaskId(rs.getLong("task_id"));
obj.setDay(rs.getString("day"));
obj.setGmtCreate(new Date(rs.getTimestamp("gmt_create").getTime()));
obj.setGmtModify(new Date(rs.getTimestamp("gmt_modify").getTime()));
obj.setMessage(rs.getString("message"));
obj.setState(rs.getInt("state"));
obj.setTryTimes(rs.getInt("try_times"));
return obj;
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import com.baidu.rigel.dmap.commons.bean.impl.TaskInitInfo;
import org.joda.time.DateTime;
import org.junit.Test;
import com.mockrunner.mock.jdbc.MockResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
/**
* <pre>
* TaskInitInfoRowMapperTest.java
* @author xuepeng01<br>
* @version 1.0
* Create Time 2015 6 23 9:04:19<br>
* Description : TaskInitInfoRowMapper
* </pre>
*/
public class TaskInitInfoRowMapperTest {
/**
* Test method for
* {@link com.baidu.rigel.dmap.business.dao.mapper.TaskInitInfoRowMapper#mapRow(java.sql.ResultSet, int)}
* .
*/
@Test
public void testMapRow() {
try {
MockResultSet rs = new MockResultSet("myMock");
long id = 11L;
long taskId = 11L;
String day = "20150623";
String message = "hello";
int state = 2;
int tryTimes = 2;
rs.addColumn("id", new Long[] { id });
rs.addColumn("task_id", new Long[] { taskId });
rs.addColumn("day", new String[] { day });
long time = DateTime.now().getMillis();
rs.addColumn("gmt_create",
new Timestamp[] { new Timestamp(time) });
rs.addColumn("gmt_modify", new Timestamp[] { new Timestamp(time) });
rs.addColumn("message", new String[] { message });
rs.addColumn("state", new Integer[] { state });
rs.addColumn("try_times", new Integer[] { tryTimes });
rs.next();
TaskInitInfoRowMapper obj = new TaskInitInfoRowMapper();
TaskInitInfo o = obj.mapRow(rs, 1);
assertEquals(id, o.getId().longValue());
} catch (SQLException e) {
e.printStackTrace();
fail();
}
}
}
参照: http://stackoverflow.com/questions/878848/easy-way-to-fill-up-resultset-with-data