2012-10-13 193 views
1

我試圖通過Junit的樣品測試的HtmlUnit運行情況。例測試的HtmlUnit失敗

我的項目是基於Maven的。我是否需要在編譯和測試期間將所有依賴項添加到我的POM中? http://htmlunit.sourceforge.net/dependencies.html 現在我已經加入化的HtmlUnit依賴,httpconnections和nekohtml。

樣品測試:

@Test 
public void homePage() throws Exception { 
    WebClient webClient = new WebClient(BrowserVersion.CHROME_16); 
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); 

    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText()); 

    final String pageAsXml = page.asXml(); 
    assertTrue(pageAsXml.contains("<body class=\"composite\">")); 

    final String pageAsText = page.asText(); 
    assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols")); 

    webClient.closeAllWindows(); 
} 

測試失敗W¯¯以下錯誤:

homePage(ActiveComponentTest) Time elapsed: 2.06 sec <<< ERROR! 
com.gargoylesoftware.htmlunit.ObjectInstantiationException: unable to create HTML parser 
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.<init>(HTMLParser.java:427) 
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.<init>(HTMLParser.java:351) 
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:205) 
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:180) 
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:267) 
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:155) 
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:440) 
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:315) 
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:380) 
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:365) 
    at ActiveComponentTest.homePage(ActiveComponentTest.java:54) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74) 
Caused by: org.xml.sax.SAXNotRecognizedException: Feature 'http://cyberneko.org/html/features/scanner/allow-selfclosing-iframe' is not recognized. 
    at org.apache.xerces.parsers.AbstractSAXParser.setFeature(Unknown Source) 
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.<init>(HTMLParser.java:420) 
    ... 43 more 
homePage(ActiveComponentTest) Time elapsed: 2.064 sec <<< ERROR! 
java.lang.NullPointerException: No document element (http://htmlunit.sourceforge.net/) 

    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1190) 
    at com.gargoylesoftware.htmlunit.html.HtmlPage.isOnbeforeunloadAccepted(HtmlPage.java:2123) 
    at com.gargoylesoftware.htmlunit.TopLevelWindow.close(TopLevelWindow.java:112) 
    at com.gargoylesoftware.htmlunit.WebClient.closeAllWindows(WebClient.java:1916) 
    at ActiveComponentTest.tearDown(ActiveComponentTest.java:43) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74) 
+1

我不認爲你得到的錯誤有什麼在你的'pom.xml'文件中聲明的依賴關係。 –

回答

2

問題變成了我在我的POM我的項目依賴的一個不同是拉,和不兼容,nekoHTML的版本。

一旦我刪除有問題的依賴(這是一個緩繳從項目IM測試,而不是這個項目,我用來測試其他凸出 - 說來話長)一切工作就好了。

+0

是,同樣的錯誤,通過去除舊版本nekoHTML的解決。謝謝。 – Buddha

+0

你還記得什麼違規的依存度?我有同樣的錯誤... – jlars62

0

@ jlars62,你可能有這樣的事情在你的POM:

<dependency> 
     <groupId>net.sourceforge.htmlunit</groupId> 
     <artifactId>htmlunit</artifactId> 
     <version>2.14</version> 
    </dependency> 

,添加排除如下:

<dependency> 
     <groupId>net.sourceforge.htmlunit</groupId> 
     <artifactId>htmlunit</artifactId> 
     <version>2.14</version> 
     **<exclusions> 
      <exclusion> 
       <groupId>net.sourceforge.nekohtml</groupId> 
       <artifactId>nekohtml</artifactId> 
      </exclusion> 
     </exclusions>** 
    </dependency>