我需要爲基於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測試開始之前?
項目中是否有META-INF/beans.xml文件?如果不是,那麼你必須創建它,這裏是一個[empty beans.xml](https://gist.github.com/sejersbol/845053)的例子,只需將其複製並粘貼到項目中的資源即可。你如何在Java代碼中引導和初始化Weld,你能展示這部分代碼嗎? – krokodilko
他不能。它在版本1.2中是可選的。 – 2017-09-04 19:56:57