2014-09-30 26 views
10

Po uruchomieniu testu selenu poniżej z poziomu środowiska Eclipse, w moim dzienniku pojawia się seria wiadomości Could not instantiate TestExecutionListener.Nie można utworzyć instancji TestExecutionListener

To jest aktualny 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!"); 
    } 
} 

I to jest 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.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 

Zauważ, że używam Wiosna 4.1.0.RELEASE.

One Solution, trzy dodatkowe Zależności

zauważyłem w answer to a previous question sugestia, aby dodać @WebAppConfiguration

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

Które Potem potrzebne trzy dodatkowe współzależności w moim pom.xml aby wspierać:

javax.servlet-api 
spring-jdbc 
spring-web 

Dlaczego potrzebuję tego wszystkiego, gdy w ogóle nie używam JDBC lub czegokolwiek używającego wiosnę/serwlet - to tylko sele test nium z pewną własną konfiguracją.

Czy jest łatwiejszy sposób? Czy brakuje mi czegoś większego?

Config Klasa

Jest to klasa skonfigurować z moich testów.

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

Moje pom.xml (przed I dodaje dodatkowych zależności).

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

Odpowiedz

15

Jeśli mogę zostawić w trzech dodatkowych zależności

javax.servlet-api 
spring-jdbc 
spring-web 

mogę zostawić moja klasa Test zdefiniowany jako to:

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

i będę się tego rejestrowania:

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 

Brak błędów, ale obviou chytry Spring robi sporo pracy w tle.

Ewentualnie mogę usunąć trzy dodatkowe zależności i dodać tę minimalną adnotację @TestExecutionListeners.

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

dostaję zalogowaniu jak poniżej:

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 

co najmniej błędów.

Jeśli chodzi o to, dlaczego potrzebuję tego, nie rozumiem jeszcze. Zostawiam to tutaj jako punkt odniesienia, przynajmniej po to, aby pokazać minimalne zmiany wymagane do pozbycia się wiadomości Could not instantiate TestExecutionListener.

+1

Dobra odpowiedź. Byłoby miło mieć trochę o tym, że '@ TestExecutionListeners' był pierwszy, ponieważ uważam, że jest to prawdziwa (czysta) odpowiedź. –

4

Przynajmniej dla mojej konfiguracji przy użyciu TestNG, oryginalna odpowiedź nie była wystarczająca. Musiałem dodać następującą adnotację:

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

pozostanie na poziomie zbliżonym do pierwotnego wdrożenia Wiosna, użyj zamiast tego:

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

jak określono w 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" }; 

Tylko ServletTestExecutionListener powinien zostać wyeksmitowany.

4

Wszelkie komunikaty INFO jak:

Nie można utworzyć wystąpienia TestExecutionListener org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

można bezpiecznie ignorowane, jeśli nie używasz lub testowania lub JDBC Funkcje sprężynowe związane z WEB. To jest tylko komunikat INFO informujący nas, że Spring nie aktywował tych słuchaczy, ponieważ wymagane zależności (zależności pom) nie zostały dodane. Co jest w porządku, jeśli nie korzystasz z tych funkcji.

Jednak powiedzmy używasz @Sql załadować niektórych danych do bazy danych testowych, i widzisz to ostrzeżenie, to musimy drutu w zależności wymaganych (spring-jdbc z zakresu testowego w projekcie pom.xml) w celu wymagana Listener (SqlScriptsTestExecutionListener w tym przypadku) zostanie aktywowany wiosną

Powiązane problemy