2014-09-30 20 views
10

Khi tôi chạy thử nghiệm selen của tôi bên dưới từ bên trong Eclipse, tôi nhận được một loạt các thông báo Could not instantiate TestExecutionListener trong nhật ký của tôi.Không thể khởi tạo TestExecutionListener

Đây là thử nghiệm thực tế.

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

Và đây là nhật ký

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 

Lưu ý rằng tôi đang sử dụng Spring 4.1.0.RELEASE.

Một giải pháp, ba Dependencies tắm

tôi nhận thấy trong answer to a previous question những gợi ý để thêm @WebAppConfiguration

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

Mà tôi thì cần ba phụ thuộc thêm trong pom.xml của tôi để hỗ trợ:

javax.servlet-api 
spring-jdbc 
spring-web 

Tại sao tôi cần tất cả điều này khi tôi không thực sự sử dụng JDBC chút nào, hoặc bất kỳ thứ gì sử dụng spring-web/servlet - đây chỉ là một sele thử nghiệm nium với một số cấu hình của riêng tôi.

Có cách nào dễ dàng hơn không? Tôi có thiếu cái gì lớn hơn không?

Lớp cấu hình

Đây là lớp tôi định cấu hình thử nghiệm của mình.

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 của tôi (trước khi tôi đã thêm các phụ thuộc thêm).

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

Trả lời

15

Nếu tôi để lại trong ba phụ thuộc thêm

javax.servlet-api 
spring-jdbc 
spring-web 

tôi có thể rời khỏi lớp học thử nghiệm của tôi được định nghĩa như thế này:

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

và tôi sẽ nhận khai thác gỗ này:

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 

Không có lỗi, nhưng obviou sly Spring đang làm một chút công bằng trong nền.

Hoặc, tôi có thể xóa thêm ba phụ thuộc và thêm chú thích tối thiểu @TestExecutionListeners này.

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

tôi nhận được đăng nhập như sau:

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 

Ít nhất không có lỗi.

Vì sao tôi cần bất kỳ điều này, tôi chưa hiểu. Tôi để lại điều này ở đây như là một tài liệu tham khảo, ít nhất là để hiển thị các thay đổi tối thiểu cần thiết để loại bỏ các thông báo Could not instantiate TestExecutionListener.

+1

Câu trả lời hay, sẽ rất hay khi có một chút về '@ TestExecutionListeners' trước tiên vì tôi cảm thấy đó là câu trả lời thực sự (sạch). –

4

Ít nhất đối với thiết lập của tôi bằng TestNG, câu trả lời ban đầu không đủ. Tôi đã có thêm chú thích sau:

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

Để duy trì chặt chẽ đến việc thực hiện mùa xuân ban đầu, sử dụng này để thay thế:

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

theo quy định tại 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" }; 

Chỉ nên gỡ bỏ ServletTestExecutionListener.

4

Bất kỳ thông điệp INFO như:

Không thể nhanh chóng TestExecutionListener org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

một cách an toàn có thể được bỏ qua nếu bạn không sử dụng hoặc thử nghiệm JDBC hoặc Các tính năng mùa xuân liên quan đến WEB. Đó chỉ là một thông báo INFO cho chúng ta biết rằng Spring đã không kích hoạt các trình lắng nghe này vì các phụ thuộc bắt buộc (các phụ thuộc pom) chưa được thêm vào. Đó là tốt, trong trường hợp bạn không sử dụng các tính năng đó.

Tuy nhiên, hãy nói rằng bạn đang sử dụng @Sql để tải một số dữ liệu thử nghiệm vào cơ sở dữ liệu, và bạn thấy cảnh báo này, THÌ chúng tôi cần dây trong các phụ thuộc bắt buộc (spring-jdbc với phạm vi kiểm tra trong dự án của bạn pom.xml) để yêu cầu người nghe (SqlScriptsTestExecutionListener trong trường hợp này) được kích hoạt bởi Spring

Các vấn đề liên quan