淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

利用easyMock模拟JDBC查询

上一篇 / 下一篇  2009-01-03 23:40:39 / 个人分类:自动化测试框架实现与优化

参考:http://solnone.blogspot.com/search/label/Java

mock,就是假的意思。easymock或者jmock之类的框架最常用于减少外部重量级资源依赖。

 

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.ArrayList;

importjava.util.List;

 

publicclassDBImpl {

   publicListquery(Connection conn)throwsSQLException {

      Listlist=newArrayList();

      PreparedStatement ps = conn.prepareStatement("select user from user");

      try{

          ResultSet rs = ps.executeQuery();

          try{

             while(rs.next()) {

                 list.add(rs.getString(1));

             }

          }finally{

             rs.close();

          }

      }finally{

          ps.close();

      }

      returnlist;

   }

}

 

 

importstaticorg.easymock.EasyMock.createControl;

importstaticorg.easymock.EasyMock.expect;

importstaticorg.easymock.EasyMock.matches;

 

importstaticorg.junit.Assert.assertEquals;

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.util.Arrays;

importjava.util.List;

importorg.easymock.IMocksControl;

importorg.junit.After;

importorg.junit.Before;

importorg.junit.Test;

 

publicclassDBImplTest {

   privateDBImpldbimpl;

 

   @Before

   publicvoidsetUp()throwsException {

      dbimpl=newDBImpl();

   }

 

   @After

   publicvoidtearDown()throwsException {

      dbimpl=null;

   }

 

   @Test

   publicvoidtestQuery()throwsSQLException {

      IMocksControl control =createControl();

      //建立模擬物件

      Connection conn = control.createMock(Connection.class);

      PreparedStatement ps = control.createMock(PreparedStatement.class);

      ResultSet rs = control.createMock(ResultSet.class);

      //模擬物件預期行為

 

      expect(conn.prepareStatement(matches("select user from user")))

             .andReturn(ps);

      expect(ps.executeQuery()).andReturn(rs);

      expect(rs.next()).andReturn(true);

      expect(rs.getString(1)).andReturn("root");

      expect(rs.next()).andReturn(true);

      expect(rs.getString(1)).andReturn("tester");

      expect(rs.next()).andReturn(true);

      expect(rs.getString(1)).andReturn("中文");

      expect(rs.next()).andReturn(false);

      rs.close();

      ps.close();

      //錄製完成,切到replay狀態

      control.replay();

      //調用方法

      Listexpected = Arrays.asList(newString[]{"root","tester","中文"});

      

      //这个conn是假的

      List result =dbimpl.query(conn);

      assertEquals(expected, result);

      //驗證

      control.verify();

   }

}

 

 

CLASSPATH加入easymock.jarcglib-2.2.jar以及easymockclassextension.jar.

调试上述程序,dbimpl.query(conn)如期执行真正的查询操作,无须建立真正的数据库联接。

 

 


TAG: easymock jmock jdbc

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar