花了两天的时间,终于搞定Struts+Spring+Hibernate单元测试!

关于软件的测试,我在一个英文的外国网站上看到: 如果你的测试做的越多,那么你的软开发效率越高,所以马虎不得!

下面是搞定测试的历程.

1,第一反应就是用JUNIT, 当然没问题, 一搞就通,测测DAO是没有问题的,测试代码如下:

java代码: 


 1   
 2   package infoweb.dao;
 3   
 4   import junit.framework.*;
 5   import infoweb.pojo.*;
 6   import java.util.*;
 7   import org.springframework.context.ApplicationContext;
 8   import org.springframework.context.support.FileSystemXmlApplicationContext;
 9   import net.sf.hibernate.HibernateException;
 10  /**
 11   * <p>Title: </p>
 12   * <p>Description: </p>
 13   * <p>Copyright: Copyright (c) 2004</p>
 14   * <p>Company: </p>
 15   * @author 段洪杰
 16   * @version 1.0
 17   */

 18  
19  public class TestBoardTreeDAOImpl extends TestCase {

 20    private IBoardTreeDAO boardTreeDAO = null;
 21    private ApplicationContext ac;
 22  
23    protected void setUp() throws Exception {

 24      super.setUp();
 25      ac = new FileSystemXmlApplicationContext("F:/jbproject/info_web/infoweb/WEB-INF/applicationContext-hibernate.xml");
 26      boardTreeDAO =(BoardTreeDAOImpl) ac.getBean("boardTreeDAO");
 27    }

 28  
29    protected void tearDown() throws Exception {

 30      boardTreeDAO = null;
 31      super.tearDown();
 32    }

 33  
 34  
35    public void testgetBoardById() {

 36       Board board=boardTreeDAO.getBoardById("40288548ff334c2300ff334c27700003");
 37       String actualReturn=board.getName();
 38       String expectedReturn= "中国新闻";
 39       assertEquals("return value", expectedReturn, actualReturn);
 40    }

 41  
 42  
43    public void testDeleteBranch() {

 44      Board board= boardTreeDAO.getBoardById("40288548ff38b15e00ff38b163c00003");
 45      boardTreeDAO.deleteBranch(board);
 46  
 47    }

 48  
49    public void testGetChildren() {

 50      String parentid = "40288548ff334c2300ff334c27700003";
 51      List actualReturn=new ArrayList();
 52      List expectedReturn = new ArrayList();
 53       expectedReturn.add("港台新闻");
 54       expectedReturn.add("云南新闻");
 55      Iterator it = boardTreeDAO.getChildren(parentid);
 56  
57      while(it.hasNext()){

 58          Board board=(Board)it.next();
 59          actualReturn.add(board.getName());
 60      }

 61  
 62      assertEquals("return value", expectedReturn, actualReturn);
 63  
 64    }

 65  
66    public void testGetChildrenCount() {

 67      String parentid = "40288548ff334c2300ff334c27700003";
 68      int expectedReturn = 2;
 69      int actualReturn = boardTreeDAO.getChildrenCount(parentid);
 70      assertEquals("return value", expectedReturn, actualReturn);
 71    }

 72  
73    public void testGetParentByChild() {

 74      Board board=boardTreeDAO.getBoardById("40288548ff38b26900ff38b26b8b0001");
 75      String expectedReturn = "ROOT789";
 76      String actualReturn = boardTreeDAO.getParentByChild(board).getName();
 77      assertEquals("return value", expectedReturn, actualReturn);
 78  
 79    }

 80  
81    public void testGetParentByChildId() {

 82      String id = "40288548ff38b26900ff38b26b8b0001";
 83      String expectedReturn = "ROOT789";
 84      Board actualReturn =(Board) boardTreeDAO.getParentByChildId(id);
 85      assertEquals("return value", expectedReturn, actualReturn.getName());
 86  
 87    }

 88  
89    public void testGetRoots() throws HibernateException{

 90      List expectedReturn =new ArrayList();
 91      List actualReturn=new ArrayList();
 92      expectedReturn.add("中国新闻");
 93      expectedReturn.add("国际新闻");
 94  
 95      expectedReturn.add("我是一个人111");
 96      Iterator roots = boardTreeDAO.getRoots();
97      while(roots.hasNext()){

 98        Board board=(Board)roots.next();
 99        actualReturn.add(board.getName());
 100     }

 101     assertEquals("ROOTS版块值", expectedReturn, actualReturn);
 102 
 103   }

 104 
105   public void testSetChild() {

 106     Board board = new Board();
 107     board.setName("r00t123-1-111");
 108     String parentid = "40288548ff38b15e00ff38b163c00003";
 109     boardTreeDAO.setChild(board, parentid);
 110 
 111   }

 112 
113   public void testSetRoot() {

 114     Board board = new Board();
 115     board.setName("ROOT789");
 116     boardTreeDAO.setRoot(board);
 117 
 118   }

 119 
 120 }

 121 
 122 


 



2.下面想测STRUTS了,第二反应就是用cactus,打开JBUILDER例使用向导,两三下就搞定了,不过问题来了,cactus测试SERVLET没问题,测试STRUTS就不行了!

--------------- 卡住了!


3.想到了STRUTSTEST的套件,打开http://strutstestcase.sourceforge.net网址,下载一个下来安装!
使用模似方式吧. 写一个测试文件,运行不过,报错,找不到WIN-INF目录. 细读STRUTSTEST的套件英语说明书(好在不难读).说是要指定classpath. 指了一下,不过在JUBILDER中,指定的环境变量好象没用, 把JBUILDER的菜单翻了几遍,也没有JBUILDER中指定casspath的设置,我又不想在命令行中测试.

--------------- 又 卡住了!


4.决定用STRUTSTEST+cactus,很快就配好了,但行不过,硬是找不到原因.真气人. 决定放弃此方式,用 Spring自带的STRUTS测试例子中的方法,看来看去一大堆代码,也读得不太懂,运行时又报错,说是有的方法没有定义,我拷!!!!!!

--------------- 又又 卡住了!


5.再次决定用STRUTSTEST+cactus,研究了一下,发现jbuilder的cactus的版本太低,升级一下,一试就成功了.代码如下":

java代码: 


  
  package infoweb.web;
  
  import junit.framework.*;
  import org.apache.struts.action.*;
  import javax.servlet.http.*;
  
  
  import infoweb.pojo.Board;
 10 /**
 11  * <p>Title: </p>
 12  * <p>Description: </p>
 13  * <p>Copyright: Copyright (c) 2004</p>
 14  * <p>Company: </p>
 15  * @author 段洪杰
 16  * @version 1.0
 17  */

 18 
 19 import servletunit.struts.CactusStrutsTestCase;
 20 
21 public class TestSetBoardAction extends CactusStrutsTestCase {

 22 
23   public TestSetBoardAction(String testName) {

 24     super(testName);
 25   }

 26 
 27 
28  public void testSuccessfulBoard() {

 29     addRequestParameter("name", "dhj");
 30     setRequestPathInfo("/setBoardAction");
 31     actionPerform();
 32     verifyForward("success");
 33     verifyForwardPath("/main/success.jsp");
 34    assertEquals("deryl", getSession().getAttribute("authentication"));
 35    verifyNoActionErrors();
 36 
 37  }

 38 
 39 }

 40 
 41 


 

- 作者: airfish78 2004年11月16日, 星期二 10:48