小编典典

带有Selenium ClassNotFoundException的Spring BootJUnit:org.apache.xml.utils.PrefixResolver

spring-boot

我正在尝试使用selenium运行一个简单的测试,但是运行测试时却收到ClassNotFoundException,并且我不知道要导入什么依赖项才能解决此问题。

我找不到任何与此有所不同的示例。

我正在使用Java 8和spring-boot 1.5.10.RELEASE

你们能帮我这个忙吗?

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class VersaoControllerTest extends AbstractTest{

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private WebDriver webDriver;

    @Test
    public void test() {
        this.webDriver.get("/");
    }
}

pom.xml

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <scope>test</scope>
    </dependency>

错误:

Caused by: java.lang.ClassNotFoundException: org.apache.xml.utils.PrefixResolver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_131]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[?:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_131]
    at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_131]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[?:1.8.0_131]
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[?:1.8.0_131]
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[?:1.8.0_131]
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[?:1.8.0_131]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[?:1.8.0_131]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[?:1.8.0_131]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_131]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[?:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_131]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[?:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_131]
    at com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration.<clinit>(JavaScriptConfiguration.java:407) ~[htmlunit-2.21.jar:2.21]
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.<init>(JavaScriptEngine.java:129) ~[htmlunit-2.21.jar:2.21]
    at com.gargoylesoftware.htmlunit.WebClient.init(WebClient.java:239) ~[htmlunit-2.21.jar:2.21]
    at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:213) ~[htmlunit-2.21.jar:2.21]
    at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:204) ~[htmlunit-2.21.jar:2.21]
    at org.springframework.boot.test.web.htmlunit.LocalHostWebClient.<init>(LocalHostWebClient.java:40) ~[spring-boot-test-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration.mockMvcWebClientBuilder(MockMvcWebClientAutoConfiguration.java:55) ~[spring-boot-test-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration$$EnhancerBySpringCGLIB$$90ad153.CGLIB$mockMvcWebClientBuilder$0(<generated>) ~[spring-boot-test-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration$$EnhancerBySpringCGLIB$$90ad153$$FastClassBySpringCGLIB$$cf4a3653.invoke(<generated>) ~[spring-boot-test-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration$$EnhancerBySpringCGLIB$$90ad153.mockMvcWebClientBuilder(<generated>) ~[spring-boot-test-autoconfigure-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) ~[spring-boot-test-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 27 more

阅读 260

收藏
2020-05-30

共1个答案

小编典典

java.lang.ClassNotFoundException

Java中的ClassNotFoundException是Java的子类,java.lang.Exception
Java虚拟机 尝试加载特定的类并且在类路径中找不到所请求的类时发生。这是一个检查的异常,使用块或using 子句可能
引发ClassNotFoundException的 异常需要显式的Exception处理方法。try-catch``throws

根据 Java Docs
ClassNotFoundException,
在以下情况下会出现:

  1. 当我们尝试使用 Class.forName() 方法加载类时 .class ,classpath中的文件或类的二进制文件不可用。
  2. Classloader 尝试使用 findSystemClass() 方法加载类时。
  3. 在Java中使用 ClassLoader类的 loadClass() 方法时。 __

ClassNotFoundExcepiton仅当JVM试图在运行时加载类时才会出现,而与编译时没有任何关系,这不同于NoClassDefFoundError。这是因为直到运行时JVM都不知道该类,并且只能通过上述指定的方法或通过使用Reflection从某个配置中读取类的名称,然后在这些配置文件上加载指定的类来完成。


这个用例

该错误确实提示我们出了什么问题,如下所示:

Caused by: java.lang.ClassNotFoundException: org.apache.xml.utils.PrefixResolver

根据您已共享的 pom.xml* ,该 <version>x.y.z</version>标签明显丢失。您需要更改 *如下 :

  • selenium-api

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
    


    org.seleniumhq.selenium
    selenium-api
    3.10.0

  • selenium-htmlunit-driver

     <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver -->
    


    org.seleniumhq.selenium
    selenium-htmlunit-driver
    2.52.0

另外,在使用 spring-boot 1.5.10时, 您可能还需要添加以下任意一项 <dependency>

  • selenium-java

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    


    org.seleniumhq.selenium
    selenium-java
    3.10.0

  • selenium-server

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
    


    org.seleniumhq.selenium
    selenium-server
    3.10.0

2020-05-30