use of com.alibaba.cobar.manager.jdbcmock.domain.DBUtility in project cobar by alibaba.
the class JdbcTest method test.
public void test() {
IMocksControl control = EasyMock.createControl();
DBUtility mockDBUtility = control.createMock(DBUtility.class);
Connection mockConnection = control.createMock(Connection.class);
Statement mockStatement = control.createMock(Statement.class);
ResultSet mockResultSet = control.createMock(ResultSet.class);
try {
mockDBUtility.getConnection();
EasyMock.expectLastCall().andStubReturn(mockConnection);
mockConnection.createStatement();
EasyMock.expectLastCall().andStubReturn(mockStatement);
mockStatement.executeQuery(SQLEquals.sqlEquals("SELECT * FROM sales_order_table"));
EasyMock.expectLastCall().andStubReturn(mockResultSet);
mockResultSet.next();
EasyMock.expectLastCall().andReturn(true).times(3);
EasyMock.expectLastCall().andReturn(false).times(1);
mockResultSet.getString(1);
EasyMock.expectLastCall().andReturn("DEMO_ORDER_001").times(1);
EasyMock.expectLastCall().andReturn("DEMO_ORDER_002").times(1);
EasyMock.expectLastCall().andReturn("DEMO_ORDER_003").times(1);
mockResultSet.getString(2);
EasyMock.expectLastCall().andReturn("Asia Pacific").times(1);
EasyMock.expectLastCall().andReturn("Europe").times(1);
EasyMock.expectLastCall().andReturn("America").times(1);
mockResultSet.getDouble(3);
EasyMock.expectLastCall().andReturn(350.0).times(1);
EasyMock.expectLastCall().andReturn(1350.0).times(1);
EasyMock.expectLastCall().andReturn(5350.0).times(1);
control.replay();
Connection conn = mockDBUtility.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from sales_order_table");
int i = 0;
String[] priceLevels = { "Level_A", "Level_C", "Level_E" };
while (rs.next()) {
SalesOrder order = new SalesOrderImpl();
order.loadDataFromDB(rs);
assertEquals(order.getPriceLevel(), priceLevels[i]);
i++;
}
control.verify();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations