2014-09-30 20 views
10

Quando eseguo il test di selenio qui sotto da Eclipse, ottengo una serie di messaggi Could not instantiate TestExecutionListener nel mio registro.Impossibile istanziare TestExecutionListener

Questo è il test effettivo.

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

e questo è il registro

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.springframewor[email protected]152c95a3, org.springfra[email protected]22140b31] 
127 [main] INFO org.springframework.context.support.GenericApplicationContext - Refreshing [email protected]523de0: 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 [email protected]523de0: startup date [Wed Oct 01 01:20:22 EST 2014]; root of context hierarchy 

Nota che sto usando Primavera 4.1.0.RELEASE.

One Solution, tre Dipendenze extra

ho notato nel answer to a previous question il suggerimento di aggiungere @WebAppConfiguration

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

che ho poi avuto bisogno di tre dipendenze in più nel mio pom.xml per sostenere:

javax.servlet-api 
spring-jdbc 
spring-web 

Perché ho bisogno di tutto questo extra quando in realtà non utilizzo affatto JDBC o nulla che usi spring-web/servlet - questo è solo un sele test del nio con alcune delle mie configurazioni personali.

C'è un modo più semplice? Mi manca qualcosa di più grande?

Config Classe

Questa è la classe a configurare i miei test con.

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

mio pom.xml (prima ho aggiunto le dipendenze in più).

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

risposta

15

Se lascio nelle tre dipendenze extra

javax.servlet-api 
spring-jdbc 
spring-web 

posso lasciare la mia classe di test definita come questo:

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

e mi metterò questa registrazione:

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: [or[email protected]3997ebf6, org.springframewor[email protected]25048104, org.springfra[email protected]4ab24098, org.springframew[email protected]7caee177, org.sp[email protected]3d548b94] 
132 [main] INFO org.springframework.context.support.GenericApplicationContext - Refreshing [email protected]55137: 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 [email protected]55137: startup date [Wed Oct 01 21:55:02 EST 2014]; root of context hierarchy 

Nessun errore, ma obviou sly Spring sta facendo un bel po 'di lavoro in background.

In alternativa, è possibile rimuovere le tre dipendenze aggiuntive e aggiungere questa minima annotazione @TestExecutionListeners.

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

vengo accedendo come di seguito:

0 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframewor[email protected]4fce6eaf] 
117 [main] INFO org.springframework.context.support.GenericApplicationContext - Refreshing [email protected]695958: 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 [email protected]695958: startup date [Wed Oct 01 21:59:05 EST 2014]; root of context hierarchy 

Almeno nessun errore.

Per quanto riguarda il motivo per cui ho bisogno di questo, non capisco ancora. Lascio questo qui come riferimento, per lo meno per mostrare le modifiche minime necessarie per sbarazzarsi dei messaggi Could not instantiate TestExecutionListener.

+1

Una buona risposta, sarebbe stato bello avere il bit su '@ TestExecutionListeners' è stato il primo perché ritengo che sia la vera risposta (pulita). –

4

Almeno per il mio setup con TestNG, la risposta originale non era abbastanza. Ho dovuto aggiungere la seguente annotazione:

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

di stare vicino alla realizzazione primavera originale, utilizzare questo, invece:

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

come definito nella 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" }; 

Solo ServletTestExecutionListener deve essere rimosso.

4

Tutti i messaggi INFO come:

Impossibile creare un'istanza TestExecutionListener org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

possono essere ignorati, se non si sta utilizzando o testando JDBC o Caratteristiche primaverili relative al WEB. Questo è solo un messaggio INFO che ci informa che Spring non ha attivato questi ascoltatori in quanto non sono state aggiunte le dipendenze richieste (dipendenze pom). Che va bene, nel caso tu non stia usando quelle caratteristiche.

permette tuttavia dire che si sta utilizzando @sql caricare alcuni dati di prova in un database, e si vede questo avvertimento, allora abbiamo bisogno di filo in dipendenze richieste (spring-jdbc con ambito di test nel progetto pom.xml) in modo che la richiesta ascoltatore (SqlScriptsTestExecutionListener in questo caso) da attivare entro la primavera

Problemi correlati