Java 如何在JUnit测试中引导焊接se

Java 如何在JUnit测试中引导焊接se,java,junit,cdi,weld,Java,Junit,Cdi,Weld,我有一个用于单元测试的maven项目,并且希望使用CDI。我已将weld se依赖项放在pom.xml中,如下所示: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> &

我有一个用于单元测试的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>
以及使用此runner的单元测试。测试正在注入一个应用程序范围的bean。问题是,由于对唯一注入点的“未满足的依赖关系”,weld无法初始化,好像weld完全不知道我的应用程序范围bean。但是这个bean在src/test/java/…中。。。使用我的测试(但在另一个java包中)

我在src/test/resources中有一个空的beans.xml

我注意到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

有人能帮我一下吗?

有两件事要看:焊接SE容器或看看。它为JUnit测试类生成一个
Runner

来源:

CDI装置也会记录以下警告,但尽管它运行良好:

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

将以下
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>

尊敬。

我刚刚尝试使用delta spike,但在cdiContainer.boot()调用中得到了完全相同的错误。一定是我做错了什么…看起来你是如何使用它的,Weld不知道扫描什么类。确实,但我应该告诉Weld扫描什么吗?启动测试时,所有类都位于myproject/target/test类以及META-INF/beans.xml下。可能是因为这些类都不在jar文件中吗?(我认为不是,但可能我错了)您是从测试类路径构建的。您必须查看beans.xml是否位于类路径上的正确位置。
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
<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>
<dependency>
    <groupId>javax.ejb</groupId>
    <artifactId>javax.ejb-api</artifactId>
    <version>3.2</version>
</dependency>