gpt4 book ai didi

java - 带有 Selenium ClassNotFoundException : org. apache.xml.utils.PrefixResolver 的 Spring Boot JUnit

转载 作者:行者123 更新时间:2023-11-30 10:18:36 25 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

java.lang.ClassNotFoundException

ClassNotFoundException在 Java 中是 java.lang.Exception 的子类并在 Java 虚拟机 尝试加载特定类但未在类路径中找到请求的类时发生。它是一个已检查的异常,需要明确的异常处理方法才能通过使用 try-catch 可能抛出 ClassNotFoundException。阻止或使用 throws条款。

根据 Java 文档 ClassNotFoundException出现在以下情况:

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

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 -->
    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-api</artifactId>
    <version>3.10.0</version>
    </dependency>
  • selenium-htmlunit-driver

     <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver -->
    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-htmlunit-driver</artifactId>
    <version>2.52.0</version>
    </dependency>

因为您正在使用 spring-boot 1.5.10 另外您可能还需要添加以下任一 <dependency> :

  • selenium-java

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.10.0</version>
    </dependency>
  • selenium-server

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.10.0</version>
    </dependency>

关于java - 带有 Selenium ClassNotFoundException : org. apache.xml.utils.PrefixResolver 的 Spring Boot JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49153649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com