我在使用spring-test-htmlunit运行HTMLUnit测试时遇到问题。在WebClient的实例上调用getPage()时,我得到一个noSuchMethod错误,如下所示:
java.lang.NoSuchMethodError: com.gargoylesoftware.htmlunit.CookieManager.getCookies(Ljava/net/URL;)Ljava/util/Set;
at org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilder.cookies(HtmlUnitRequestBuilder.java:267)
at org.springframework.test.web.servlet.htmlunit.HtmlUnitRequestBuilder.buildRequest(HtmlUnitRequestBuilder.java:104)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
at org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection.getResponse(MockMvcWebConnection.java:111)
at org.springframework.test.web.servlet.htmlunit.MockMvcWebConnection.getResponse(MockMvcWebConnection.java:96)
at org.springframework.test.web.servlet.htmlunit.DelegatingWebConnection.getResponse(DelegatingWebConnection.java:68)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1357)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1274)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:382)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:304)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:451)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:436)
我尝试从spring-test-htmlunit中排除HTMLUnit版本并添加最新版本。我还对thymeleaf模板编译所需的nekoHTML有依赖性。
相关的pom.xml
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.21</version>
</dependency>
<dependency>
<groupId>org.springframework.test.htmlunit</groupId>
<artifactId>spring-test-htmlunit</artifactId>
<version>1.0.0.RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
违规通话
HtmlPage page = webClient.getPage("http://localhost:8080/");
答案 0 :(得分:0)
通常,这是依赖冲突,您可以通过以下方式打印当前使用的JAR:
System.out.println(WebClient.class.getProtectionDomain().getCodeSource().getLocation());
System.out.println(HTMLElements.class.getProtectionDomain().getCodeSource().getLocation());
确保Neko版本与相应的HtmlUnit版本兼容。
此外,spring boot包含特定的HtmlUnit版本,可能不是您想要的版本。