我有一个用于单元测试的maven项目,并希望使用CDI。我将weld-se依赖项放在pom.xml中,如下所示:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>1.1.8.Final</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP3</version>
</dependency>我在一个JUnit测试运行器中引导焊接:
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
private final Class klass;
private final Weld weld;
private final WeldContainer container;
public WeldJUnit4Runner(final Class klass) throws InitializationError {
super(klass);
this.klass = klass;
this.weld = new Weld();
this.container = weld.initialize();
}
@Override
protected Object createTest() throws Exception {
final Object test = container.instance().select(klass).get();
return test;
}
}以及使用此runner的单元测试。该测试正在注入一个应用程序范围的bean。问题是,weld无法初始化,因为在唯一的注入点上有一个“未满足的依赖项”,就好像我的应用程序作用域bean对于weld是完全未知的。但是该bean位于src/test/java/...使用我的测试(但在另一个java包中)。
在src/ beans.xml /resources中有一个空的测试资源。
我注意到weld在启动时会发出警告,但我不认为这是我问题的原因:
604 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
605 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled有人能帮我一下吗?
发布于 2014-03-18 16:56:13
看看CDI-Unit吧。它为JUnit测试类提供了一个Runner:
@RunWith(CdiRunner.class) //使用CDI-Unit类MyTest { @Inject something;//这将在测试运行之前注入!... }
来源:CDI-Unit user guide。
CDI-Unit也记录了下面的警告,但尽管如此,它工作得很好:
WARN (InterceptionTypeRegistry.java) - WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
WARN (InterceptionTypeRegistry.java) - WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled发布于 2012-10-26 12:48:02
需要注意的几件事:用于Arquillian或DeltaSpike CdiCtrl module的Weld SE容器
发布于 2015-08-31 01:13:12
将以下beans.xml添加到src/test/resources/META-INF目录:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>警告的原因:找不到类javax.ejb.PostActivate和javax.ejb.PrePassivate。您缺少一个依赖项。
将此依赖项添加到您的pom.xml:
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>致以问候。
https://stackoverflow.com/questions/13076189
复制相似问题