java – 无法实例化TestExecutionListener

前端之家收集整理的这篇文章主要介绍了java – 无法实例化TestExecutionListener前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我从 Eclipse中运行下面的硒测试时,我得到一系列不能在我的日志中实例化TestExecutionListener消息.

这是实际的测试.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
public final class TestWebpage {
   private static final Logger LOG = Logger.getLogger(TestWebpage.class);

   @Autowired
   private WebDriver driver;

   @Test
   public void testLoadingPage() {
      LOG.debug("Hello World!");
   }
}

这是日志

0    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Loaded default TestExecutionListener class names from location [Meta-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener,org.springframework.test.context.support.DependencyInjectionTestExecutionListener,org.springframework.test.context.support.DirtiesContextTestExecutionListener,org.springframework.test.context.transaction.TransactionalTestExecutionListener,org.springframework.test.context.jdbc.sqlScriptsTestExecutionListener]
5    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.sqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
6    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
7    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
8    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@152c95a3,org.springframework.test.context.support.DirtiesContextTestExecutionListener@22140b31]
127  [main] INFO  org.springframework.context.support.GenericApplicationContext  - Refreshing org.springframework.context.support.GenericApplicationContext@35523de0: startup date [Wed Oct 01 01:20:22 EST 2014]; root of context hierarchy
3961 [main] DEBUG org.rmb.selenium.external.TestWebpage  - Hello World!
3963 [Thread-8] INFO  org.springframework.context.support.GenericApplicationContext  - Closing org.springframework.context.support.GenericApplicationContext@35523de0: startup date [Wed Oct 01 01:20:22 EST 2014]; root of context hierarchy

注意我使用的是Spring 4.1.0.RELEASE.

一个解决方案,三个额外的依赖

我在answer to a previous question中注意到了添加@WebAppConfiguration的建议

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
@WebAppConfiguration
public final class TestWebpage {

那我在我的pom.xml中需要三个额外的依赖来支持

javax.servlet-api
spring-jdbc
spring-web

为什么我根本不使用JDBC,或者使用spring-web / servlet的任何东西,都需要这些额外的东西 – 这只是我自己配置​​的一个硒测试.

有更简单的方法吗?我错过了更大的东西吗?

配置类

这是我配置我的测试的类.

public final class SeleniumConfig {

   @Bean
   public String baseUrl() {
      return "http://localhost:8888/";
   }

   @Bean
   public WebDriver driver() {
      return new CloseableFirefoxDriver();
   }

   class CloseableFirefoxDriver extends FirefoxDriver implements DisposableBean {
      public void destroy() throws Exception {
         quit();
      }
   }
}

POM

我的pom.xml(在我添加额外的依赖项之前).

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>WebAppWithSeleniumTest</groupId>
   <artifactId>WebAppWithSeleniumTest</artifactId>
   <packaging>war</packaging>
   <version>0.0.1-SNAPSHOT</version>
   <name>WebAppWithSeleniumTest Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
      </dependency>
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.16</version>
      </dependency>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>2.43.1</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>${spring.version}</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
      </dependency>
   </dependencies>
   <build>
      <finalName>WebAppWithSeleniumTest</finalName>
      <resources>
         <resource>
            <directory>src/main/resources</directory>
            <targetPath>${basedir}/target/classes</targetPath>
            <includes>
               <include>log4j.properties</include>
            </includes>
         </resource>
      </resources>
   </build>
   <description>Web App with Selenium Tests - a base</description>
   <properties>
      <spring.version>4.1.0.RELEASE</spring.version>
   </properties>
</project>

解决方法

如果我离开三个额外的依赖关系
javax.servlet-api
spring-jdbc
spring-web

我可以把我的测试类定义为:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
public final class TestWebpage {

我会得到这个日志记录:

0    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Loaded default TestExecutionListener class names from location [Meta-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener,org.springframework.test.context.jdbc.sqlScriptsTestExecutionListener]
20   [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@3997ebf6,org.springframework.test.context.support.DependencyInjectionTestExecutionListener@25048104,org.springframework.test.context.support.DirtiesContextTestExecutionListener@4ab24098,org.springframework.test.context.transaction.TransactionalTestExecutionListener@7caee177,org.springframework.test.context.jdbc.sqlScriptsTestExecutionListener@3d548b94]
132  [main] INFO  org.springframework.context.support.GenericApplicationContext  - Refreshing org.springframework.context.support.GenericApplicationContext@6f55137: startup date [Wed Oct 01 21:55:02 EST 2014]; root of context hierarchy
4183 [main] DEBUG org.rmb.selenium.external.TestWebpage  - Hello World!
4186 [Thread-8] INFO  org.springframework.context.support.GenericApplicationContext  - Closing org.springframework.context.support.GenericApplicationContext@6f55137: startup date [Wed Oct 01 21:55:02 EST 2014]; root of context hierarchy

没有错误,但显然,春天在后台正在做一些相当的工作.

或者,我可以删除三个额外的依赖项,并添加这个最小的@TestExecutionListeners注释.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
@TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class})
public final class TestWebpage {

我记录如下:

0    [main] INFO  org.springframework.test.context.support.DefaultTestContextBootstrapper  - Using TestExecutionListeners: [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4fce6eaf]
117  [main] INFO  org.springframework.context.support.GenericApplicationContext  - Refreshing org.springframework.context.support.GenericApplicationContext@42695958: startup date [Wed Oct 01 21:59:05 EST 2014]; root of context hierarchy
4189 [main] DEBUG org.rmb.selenium.external.TestWebpage  - Hello World!
4190 [Thread-8] INFO  org.springframework.context.support.GenericApplicationContext  - Closing org.springframework.context.support.GenericApplicationContext@42695958: startup date [Wed Oct 01 21:59:05 EST 2014]; root of context hierarchy

至少没有错误.

至于为什么我需要这些,我还不明白.我在这里作为参考,至少显示摆脱无法实例化TestExecutionListener消息所需的最小更改.

原文链接:https://www.f2er.com/java/125315.html

猜你在找的Java相关文章