2012-07-16 56 views
0

我想在一個網絡爬蟲上設置單元測試,並且對我如何測試它們感到困惑。 (我只是做單元測試一次,它是在一個計算器程序。)JUnit測試HTML解析

以下是方案二實例方法:

protected static void HttpURLConnection(String URL) throws IOException { 

    try { 
     URL pageURL = new URL(URL); 

     HttpURLConnection connection = (HttpURLConnection) pageURL 
       .openConnection(); 
     stCode = connection.getResponseCode(); 
     System.out.println("HTTP Status code: " + stCode); 

     // append to CVS string 
     CvsString.append(stCode); 
     CvsString.append("\n"); 

     // retrieve URL 
     siteURL = connection.getURL(); 
     System.out.println(siteURL + " = URL"); 

     CvsString.append(siteURL); 
     CvsString.append(","); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
} 

和:

public static void HtmlParse(String line) throws IOException { 

    // create new string reader object 
    aReader = new StringReader(line); 

    // create HTML parser object 
    HTMLEditorKit.Parser parser = new ParserDelegator(); 

    // parse A anchor tags whilst handling start tag 
    parser.parse(aReader, new HTMLEditorKit.ParserCallback() { 
     // method to handle start tags 
     public void handleStartTag(HTML.Tag t, MutableAttributeSet a, 
       int pos) { 
      // check if A tag 
      if (t == HTML.Tag.A) { 
       Object link = a.getAttribute(HTML.Attribute.HREF); 
       if (link != null) { 
        links.add(String.valueOf(link)); 

        // cast to string and pass to methods to get title, 
        // status 
        String pageURL = link.toString(); 
        try { 
         parsePage(pageURL); // Title - To print URL, HTML 
         // page title, and HTTP status 
         HttpURLConnection(pageURL); // Status 
         // pause for half a second between pages 
         Thread.sleep(500); 

        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (BadLocationException e) { 
         e.printStackTrace(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 
    }, true); 
    aReader.close(); 
} 

我已經建立一個測試類的Eclipse和有輪廓的測試方法沿着這些線路:

@Test 
public void testHttpURLConnection() throws IOException { 
    classToTest.HttpURLConnection(?); 
    assertEquals("Result", ? ?) 
} 

我真的不知道WH可以從這裏出發。我甚至不確定我是應該測試實時URL還是本地文件。 我在這裏發現這個問題:https://stackoverflow.com/questions/5555024/junit-testing-httpurlconnection 但我不能真的按照它,我不知道它被解決無論如何。 任何指針讚賞。

回答

1

對於您的問題沒有一個確鑿的答案,您測試的內容取決於您的代碼的功能以及測試的深度。

所以,如果你有一個解析方法是採用HTML,並返回字符串:「這是一個解析的HTML」(顯然不是非常有用的,但就是一個點),你會測試它想:

@Test 
public void testHtmlParseSuccess() throws IOException {   
    assertEquals("this is a parsed html", classToTest.parse(html)) //will return true, test will pass 
} 

@Test 
    public void testHtmlParseSuccess() throws IOException {   
     assertEquals("this is a wrong answer", classToTest.parse(html)) //will return false, test will fail 
    } 

除了assertEquals()之外還有很多其他方法,所以你應該看看here

最終由您來決定要測試哪些部件以及如何測試它們。

+0

謝謝 - 我要檢查那本食譜。 – Robert 2012-07-16 12:33:47

1

想想你的方法應該有什麼效果。在第一種情況下,當HttpURLConnection(url)被調用時應該發生的預期事情似乎是狀態碼和url被追加到一個名爲CvsString的東西上。你將不得不在CvsString中實現一些東西,以便你可以檢查你所期望的事情是否真的發生了。

但是:看看你的代碼,我建議你參考一本關於單元測試的書,以及如何重構代碼,使它變得可測試。在你的代碼片段中,我看到很多原因,爲什麼單元測試你的代碼很困難,如果不是不可能的話, G。整體使用靜態方法,帶有副作用的方法,關注點很少分離等等。因此,在這種情況下不可能完全回答你的問題。

不要誤解我的意思,這並不意味着冒犯了你。很值得學習這些東西,它會提高你的編碼能力。

+0

感謝您的意見。 – Robert 2012-07-16 12:52:51