无法实例化TestExecutionListener

Rob*_*ram 11 java spring selenium-webdriver

当我从Eclipse中运行下面的selenium测试时,我Could not instantiate 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!");
   }
}
Run Code Online (Sandbox Code Playgroud)

这是日志

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
Run Code Online (Sandbox Code Playgroud)

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

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

我在上一个问题答案中注意到要添加的建议@WebAppConfiguration

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
@WebAppConfiguration
public final class TestWebpage {
Run Code Online (Sandbox Code Playgroud)

然后我在pom.xml中需要三个额外的依赖项来支持:

javax.servlet-api
spring-jdbc
spring-web
Run Code Online (Sandbox Code Playgroud)

当我实际上根本没有使用JDBC或使用spring-web/servlet的任何东西时,为什么我需要所有这些额外的东西 - 这只是我自己的一些配置的selenium测试.

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

配置类

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

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();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Rob*_*ram 15

如果我留下三个额外的依赖项

javax.servlet-api
spring-jdbc
spring-web
Run Code Online (Sandbox Code Playgroud)

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
public final class TestWebpage {
Run Code Online (Sandbox Code Playgroud)

我会得到这个记录:

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]
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
Run Code Online (Sandbox Code Playgroud)

没有错误,但显然Spring正在后台做一些工作.

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SeleniumConfig.class)
@TestExecutionListeners(listeners = {DependencyInjectionTestExecutionListener.class})
public final class TestWebpage {
Run Code Online (Sandbox Code Playgroud)

我记录如下:

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
Run Code Online (Sandbox Code Playgroud)

至少没有错误.

至于为什么我需要这些,我还不明白.我将此作为参考,至少显示消除Could not instantiate TestExecutionListener消息所需的最小变化.


Mic*_*fel 5

至少对于我使用 TestNG 的设置来说,最初的答案还不够。我必须添加以下注释:

@TestExecutionListeners(inheritListeners = false, listeners =
    {DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
Run Code Online (Sandbox Code Playgroud)


Jea*_*phe 5

要保持接近原始的Spring实现,请使用以下代码:

@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })
Run Code Online (Sandbox Code Playgroud)

在org.springframework.test.context.TestContextManager中定义:

    private static final String[] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES = new String[] {
        "org.springframework.test.context.web.ServletTestExecutionListener",
        "org.springframework.test.context.support.DependencyInjectionTestExecutionListener",
        "org.springframework.test.context.support.DirtiesContextTestExecutionListener",
        "org.springframework.test.context.transaction.TransactionalTestExecutionListener" };
Run Code Online (Sandbox Code Playgroud)

应该只驱逐ServletTestExecutionListener.


Dee*_*pak 5

任何INFO消息,例如:

无法实例化TestExecutionListener org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

如果您不使用或测试与JDBC或WEB相关的spring功能,可以放心地忽略它。这只是一条INFO消息,告诉我们Spring没有激活所需的依赖项(pom依赖项),因此未激活这些侦听器。如果您不使用这些功能,那很好。

但是,可以说您正在使用@Sql将一些测试数据加载到数据库中,并且看到此警告,然后我们需要连接所需的依赖项(spring-jdbc在您的项目中具有测试范围pom.xml),以便获得所需的侦听器(SqlScriptsTestExecutionListener在这种情况下)由Spring激活