java 无法实例化 TestExecutionListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26125024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:19:50  来源:igfitidea点击:

Could not instantiate TestExecutionListener

javaspringselenium-webdriver

提问by Robert Mark Bram

When I run my selenium test below from within Eclipse, I get a series of Could not instantiate TestExecutionListenermessages in my log.

当我在 Eclipse 中运行下面的 selenium 测试时,我Could not instantiate TestExecutionListener在日志中收到一系列消息。

This is the actual test.

这是实际测试。

@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!");
   }
}

And this is the log

这是日志

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

Note that I am using Spring 4.1.0.RELEASE.

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

One Solution, three Extra Dependencies

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

I noticed in the answer to a previous questionthe suggestion to add @WebAppConfiguration

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

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

Which I then needed three extra dependencies in my pom.xml to support:

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

javax.servlet-api
spring-jdbc
spring-web

Why do I need all this extra when I am not actually using JDBC at all, or anything using spring-web/servlet - this is just a selenium test with some of my own configuration.

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

Is there an easier way? Am I missing something bigger?

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

Config Class

配置类

This is the class I configure my tests with.

这是我配置测试的类。

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

聚甲醛

My pom.xml (before I added the extra dependencies).

我的 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>

回答by Robert Mark Bram

If I leave in the three extra dependencies

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

javax.servlet-api
spring-jdbc
spring-web

I can leave my test class defined as this:

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

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

and I will get this logging:

我会得到这个日志:

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

No errors, but obviously Spring is doing a fair bit of work in the background.

没有错误,但显然 Spring 在后台做了大量工作。

Alternatively, I can remove the three extra dependencies and add this minimal @TestExecutionListenersannotation.

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

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

I get logging as below:

我得到如下记录:

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

At least no errors.

至少没有错误。

As to why I need any of this, I don't understand yet. I am leaving this here as a reference, at the very least to show the minimal changes required to get rid of the Could not instantiate TestExecutionListenermessages.

至于为什么我需要这些,我还不明白。我把它留在这里作为参考,至少是为了显示摆脱Could not instantiate TestExecutionListener消息所需的最小更改。

回答by Jean-Christophe

To stay close to the original Spring implementation, use this instead:

为了接近原始的 Spring 实现,请改用它:

@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })

as defined in org.springframework.test.context.TestContextManager:

如 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" };

Only ServletTestExecutionListener should be evicted.

只有 ServletTestExecutionListener 应该被驱逐。

回答by Deepak

Any INFO messages like:

任何 INFO 消息,例如:

Could not instantiate TestExecutionListener org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

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

Can safely be ignored if you are not using or testing JDBC or WEB related spring features. That is just an INFO message telling us that Spring did not activate these listeners as the required dependencies (pom dependencies) have not been added. Which is fine, in case you are not using those features.

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

However lets say you are using @Sql to load some test data into a database, AND you see this warning, THEN we need to wire in required dependencies (spring-jdbcwith test scope in your project pom.xml) in order for the required listener (SqlScriptsTestExecutionListenerin this case) to be activated by Spring

但是,假设您正在使用 @Sql 将一些测试数据加载到数据库中,并且您看到此警告,那么我们需要连接所需的依赖项(spring-jdbc在您的项目中具有测试范围pom.xml)以获取所需的侦听器(SqlScriptsTestExecutionListener在本例中)由 Spring 激活

回答by Michael Piefel

At least for my setup using TestNG, the original answer was not quite enough. I had to add the following annotation:

至少对于我使用 TestNG 的设置,原始答案还不够。我不得不添加以下注释:

@TestExecutionListeners(inheritListeners = false, listeners =
    {DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})