2017-09-04 81 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