`
sun113
  • 浏览: 51320 次
  • 来自: NeverNeverlaNd
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

easyMock学习--jdbc

阅读更多
使用EasyMock测试一段JDBC程序:

待测试的代码如下:

package testPlace;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Hello {

      private Connection connection;
      private ResultSet rs;
      private List lotNoList = new ArrayList();
      private int startpage;
      private int endpage;

      /**
       * @return Returns the connection.
       */
      public Connection getConnection() {
           return connection;
      }

      /**
       * @param connection The connection to set.
       */
      public void setConnection(Connection connection) {
           this.connection = connection;
      }

      /**
       * @return Returns the lotNoList.
       */
      public List getLotNoList() {
           return lotNoList;
      }

      /**
       * @param lotNoList The lotNoList to set.
       */
      public void setLotNoList(List lotNoList) {
           this.lotNoList = lotNoList;
      }
 
      public ResultSet getDataFromDb(int mode) {
           String sql = "";
           Statement sm = null;
           try {
                 if (mode == 0) {
                      sql = "SELECT * FROM MAT_TOKYO_DENPYOU WHERE FINPRT <> 'F' ";
                      sm = connection.createStatement();
                      sm.execute(sql);
                      rs = sm.getResultSet();
                 }
                 if (mode == 1) {
                      if (lotNoList != null) {
                            sql = "SELECT * FROM MAT_TOKYO_DENPYOU WHERE LOTNO IN ( ";
                            Iterator ite = lotNoList.listIterator();
                            while (ite.hasNext()) {
                                  sql = sql + ite.next().toString();
                                  if (ite.hasNext()) {
                                       sql = sql + ", ";
                                  }
                            }
                            sql = sql + " )";
                            sm = connection.createStatement();
                            sm.execute(sql);
                            rs = sm.getResultSet();
                      } else {
                            return null;
                      }
                 }

                 if (mode == 2) {
                      sql = "S ELECT * F ROM AMMM W HERE PAGENO >= "
                                  + startpage + "A ND PAGENO <= " + endpage;
                      sm = c onnection.createStatement();
                      sm.execute(sql);
                      rs = sm.getResultSet();
                 }
           } catch (SQLException e) {
                 e.printStackTrace();
                 return null;
           } finally {
                 if (sm != null) {
                      try {
                            sm.close();
                      } catch (SQLException e) {
                            e.printStackTrace();
                      }
                 }
           }
           return rs;
      }
}



测试完毕的代码如下:

package testPlace.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.easymock.MockControl;
import testPlace.Hello;

public class HelloTest extends TestCase {

      Hello hello;
      
      private MockControl connectionCtr;
      private MockControl statementCtr;
      private MockControl resultSetCtr;
      private Connection mockConn;
      private Statement mockStat;
      private ResultSet mockRs;
      private Statement stmt;
      private ResultSet rs;
      private String sql;

      protected void setUp() throws Exception {
           hello = new Hello();
           connectionCtr = MockControl.createControl(Connection.class);
           mockConn = (Connection) connectionCtr.getMock();
           statementCtr = MockControl.createControl(Statement.class);
           mockStat = (Statement) statementCtr.getMock();
           resultSetCtr = MockControl.createControl(ResultSet.class);
           mockRs = (ResultSet) resultSetCtr.getMock();
      }

      protected void tearDown() throws Exception {
           super.tearDown();
           hello = null;
      }

      /*
       * Test method for 'testPlace.Hello.getDataFromDb(int)'
       */
      public void testGetDataFromDb() {
           try {
                 connectionCtr.expectAndReturn(mockConn.createStatement(),mockStat,  2);
                 statementCtr.expectAndReturn(mockStat.execute("SELECT * FROM MAT_TOKYO_DENPYOU WHERE LOTNO IN ( a, b )"),   true, 2);
                 statementCtr.expectAndReturn(mockStat.getResultSet(),mockRs,2);
                 mockStat.close();
                 statementCtr.setVoidCallable(1, 2);
                 statementCtr.expectAndReturn(mockStat.close(),MockControl.ONE_OR_MORE);
                 connectionCtr.replay();
                 statementCtr.replay();
                 resultSetCtr.replay();

                 //这里不知道算不算逻辑
                 // transfer testList
                 List testList = new ArrayList();
                 testList.add("a");
                 testList.add("b");
                 hello.setLotNoList(testList);
                 hello.setConnection(mockConn);

                 assertNotNull(hello.getDataFromDb(1));
                 assertEquals(mockRs, hello.getDataFromDb(1));

                 connectionCtr.verify();
                 statementCtr.verify();
                 resultSetCtr.verify();
           } catch (Exception e) {
                 e.printStackTrace();
           }
      }
      
}
分享到:
评论
1 楼 sun113 2007-06-02  
不知道怎么回事,捣鼓了半天还是这个样子,不过编辑状态下,又会回来正常状态,不知道怎么搞得!help!!

相关推荐

Global site tag (gtag.js) - Google Analytics