`
sun113
  • 浏览: 51806 次
  • 来自: 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!!

相关推荐

    J2EE 学习计划J2EE 学习计划J2EE 学习计划J2EE 学习计划

    熟练掌握JDBC API(如`DriverManager`、`Connection`、`PreparedStatement`、`ResultSet`等)的使用方法,对于实现数据的增删改查等基本操作至关重要。这将帮助开发者更加灵活地与各种关系型数据库进行交互。 ### ...

    spring3.0包文件

    3. **数据访问/集成(Data Access/Integration)**:Spring 3.0增强了对各种数据库的支持,包括JDBC、Hibernate、JPA等。使用JdbcTemplate和NamedParameterJdbcTemplate提供了一种简洁的方式来处理SQL,减少了模板...

    spring 3.2 帮助文档

    2. **数据访问/集成(Data Access/Integration)**:Spring提供了对各种数据库的全面支持,包括JDBC、ORM(对象关系映射)框架如Hibernate和MyBatis,以及对JPA(Java Persistence API)的支持。此外,它还支持对...

    jTester使用指南(带书签).pdf

    db.url=jdbc:mysql://localhost:3306/testdb db.username=root db.password=password ``` **4.3 DataMap 语法详解** - **插入操作**:使用 `INSERT` 语句向数据库中插入记录。 - **验证操作**:使用 `SELECT` ...

    即时到账 Java Demo.zip

    这个压缩包包含了实现即时到账功能所需的源代码和相关配置,可以帮助开发者理解并学习相关技术。 首先,我们要讨论的是Java编程语言。Java是一种跨平台的、面向对象的语言,其特性包括自动内存管理、丰富的类库以及...

Global site tag (gtag.js) - Google Analytics