2017-09-04 22 views
0

我需要爲基於Maven的Web應用程序編寫基於JBehave的UAT。 測試已執行,但@Injected類未初始化,敬請嘗試開始。基於Maven的項目,JBehave和Selenium未能初始化CDI

我使用jbehave - Maven的插件是這樣的:

<plugin> 
     <groupId>org.jbehave</groupId> 
     <artifactId>jbehave-maven-plugin</artifactId> 
     <version>4.1.1</version> 

     <executions> 
      <execution> 
       <id>unpack-view-resources</id> 
       <phase>pre-integration-test</phase> 
       <goals> 
        <goal>unpack-view-resources</goal> 
       </goals> 
      </execution> 
      <execution> 
       <id>embeddable-stories</id> 
       <phase>integration-test</phase> 
       <configuration> 
        <includes> 
         <include>**/*Stories.java</include> 
        </includes> 
        <ignoreFailureInStories>true</ignoreFailureInStories> 
        <ignoreFailureInView>true</ignoreFailureInView> 
       </configuration> 
       <goals> 
        <goal>run-stories-as-embeddables</goal> 
       </goals> 
      </execution> 
     </executions> 
    </plugin> 

正如我在測試中使用CDI,我需要初始化焊接,因此它與測試工作。但是CDI的初始化不會發生,並且我得到NPE的測試代碼。

我聲明的依賴性是這樣的:

<dependency> 
    <groupId>org.jbehave</groupId> 
    <artifactId>jbehave-weld</artifactId> 
    <version>4.1</version> 
    <exclusions> 
    <exclusion> 
     <groupId>javax.enterprise</groupId> 
     <artifactId>cdi-api</artifactId> 
    </exclusion> 
    <exclusion> 
     <groupId>org.jboss.weld.se</groupId> 
     <artifactId>weld-se</artifactId> 
    </exclusion> 
    </exclusions> 
</dependency> 
<dependency> 
    <groupId>org.jbehave</groupId> 
    <artifactId>jbehave-core</artifactId> 
    <version>4.1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.jbehave</groupId> 
    <artifactId>jbehave-maven-plugin</artifactId> 
    <version>4.1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.jboss.weld.se</groupId> 
    <artifactId>weld-se</artifactId> 
    <version>2.3.5.Final</version> 
</dependency> 
<dependency> 
    <groupId>org.jboss.weld.se</groupId> 
    <artifactId>weld-se-core</artifactId> 
    <version>2.3.5.Final</version> 
</dependency> 
<dependency> 
    <groupId>javax.enterprise</groupId> 
    <artifactId>cdi-api</artifactId> 
    <version>1.2</version> 
</dependency> 
<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.12</version> 
</dependency> 

什麼必須做,初始化CDI測試開始之前?

+0

項目中是否有META-INF/beans.xml文件?如果不是,那麼你必須創建它,這裏是一個[empty beans.xml](https://gist.github.com/sejersbol/845053)的例子,只需將其複製並粘貼到項目中的資源即可。你如何在Java代碼中引導和初始化Weld,你能展示這部分代碼嗎? – krokodilko

+0

他不能。它在版本1.2中是可選的。 – 2017-09-04 19:56:57

回答

0

首先,您正在使用「過時」版本的CDI。有一個全新版本的規範CDI 2.0。隨意使用。已經有一個實施 - Weld version 3.0。作爲替代方案,您可以使用Apache OpenWebBeans,這是CDI已經支持CDI 2.0的另一個實現。

無論如何,回到你的問題。你沒有提到你要實施什麼樣的測試。如果您想使用依賴注入進行測試,只需編寫一個提供所有依賴關係的JUnit運行程序即可。有一個nice tutorial這樣做。該教程中,你可以看到這個代碼片段:

package com.memorynotfound.cdi.test; 

import org.junit.runners.BlockJUnit4ClassRunner; 
import org.junit.runners.model.InitializationError; 

public class WeldJUnit4Runner extends BlockJUnit4ClassRunner { 

    public WeldJUnit4Runner(Class<Object> clazz) throws InitializationError { 
     super(clazz); 
    } 

    @Override 
    protected Object createTest() { 
     final Class<?> test = getTestClass().getJavaClass(); 
     return WeldContext.INSTANCE.getBean(test); 
    } 
} 

與@RunWith(WeldJUnit4Runner.class)標註您的測試,你有你的測試依賴注入能力。

+0

感謝您的回覆。我正在執行基於正在運行的應用程序和基於硒的測試的UAT(用戶驗收測試)。 我有一個基於過時的Cucumber測試的類似項目的測試的替代版本,我必須使用JBehave重寫/重構/重做。所以,從測試等訪問UI組件的所有問題都解決了。我只需要一種使用JBehave使用CDI的方法。 – Claus