如果你的web层使用mvc并且逻辑全部放在javabean/ejb中,那么我觉得没有必要用httpunit测试,只需要junit测试逻辑组件就可以了。如果逻辑放在servelt或者jsp中,那估计测试的时间比写代码的时间还要长好几倍,如果把数据库操作也写到了jsp中,那么基本上能跑起来就已经不错了。

解决方案 »

  1.   

    package com.ziliu;import java.net.URL;
    import java.util.Iterator;
    import java.util.List;import junit.framework.TestCase;import org.w3c.dom.Document;import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlTable;
    import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
    import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;public class htmlUnit extends TestCase{

    private HtmlPage homePage = null;
    private HtmlPage searchResultPage = null; private Document pageDocument = null;
    private WebClient webClient = null;

    protected void setUp() throws Exception {

    webClient = new WebClient();
    final URL url = new URL("http://www.baidu.com");
    System.out.println("Hello!");
    homePage = (HtmlPage)webClient.getPage(url);
    assertNotNull(homePage);

    }

    public void testHomePage() throws Exception {

    System.out.println("testHomePage!");
        System.out.println("TitleText:" + homePage.getTitleText());
    // assertEquals("百度——全球最大中文搜索引擎  ", homePage.getTitleText());  }

    public void testSearch() throws Exception {

    //set the parameters for search
    System.out.println("testSearch!");
    List formList = (List) homePage.getAllForms();
    HtmlForm searchForm = (HtmlForm) formList.get(0); HtmlTextInput inputTitle = (HtmlTextInput)searchForm.getInputByName("wd");
    inputTitle.setValueAttribute("Toshiba");

    try {

    searchResultPage = (HtmlPage)searchForm.submit();
    assertNotNull(searchResultPage);
    System.out.println("TitleText:" + searchResultPage.getTitleText());

    HtmlTable table = (HtmlTable)searchResultPage.getDocumentElement().getHtmlElementsByTagName("table").get(2);
    List rows = table.getRows();
    //System.out.println( "Cell (0,0)="+ table.getCellAt(0,0).asText()); Iterator rowIterator = rows.iterator();

    while( rowIterator.hasNext() ) {

        HtmlTableRow row = (HtmlTableRow)rowIterator.next();
        System.out.println("Found row");
        List cells = row.getCells();
        Iterator cellIterator = cells.iterator();
        
        while( cellIterator.hasNext() ) {
            HtmlTableCell cell = (HtmlTableCell)cellIterator.next();
            System.out.println("Found cell: "+cell.asText());
        }
    }

    } catch (Exception e) {

    e.printStackTrace();

    }

    }

    protected void tearDown() {

    webClient = null;

    }}自己写了一个,需要http://htmlunit.sourceforge.net/下载htmlUnit包
      

  2.   

    asklxf(xuefeng)所言不错我只是想测试一下页面显示的内容,所以用htmlUnit
    不过发现它对中文支持好像有问题
      

  3.   

    测试结果如下Hello!
    testHomePage!
    TitleText:°??????????ò×??ó???????÷????
    Hello!
    testSearch!
    2005-1-19 14:37:48 com.gargoylesoftware.htmlunit.html.HtmlPage loadJavaScriptFromUrl
    警告: Expected content type of text/javascript or application/x-javascript for remotely loaded javascript element http://post.baidu.com/f?ct=301989888&tn=baiduForumSearch1&rn=10&pn=0&word=Toshiba but got [text/html]
    TitleText:°??????÷_Toshiba
    Found row
    Found cell: 
    Found cell: 找toshiba上淘宝网淘宝
    Found cell: 找toshiba信息在阿里巴巴
    Found cell: 找toshiba信息在搜捕网
    Found cell: 找toshiba相关项目title变成乱码了
      

  4.   

    用 JUnit 做 unit test, 用 JMeter 做性能测试