CDI注射与JUnit
我正在尝试将JUnit测试添加到使用CDI和JPA的Tomee web应用程序中。除去过去两周的不同障碍(包括对stackoverflow和其他来源的深入研究),我的问题似乎相当具体: 运行单元测试会显示以下错误消息:CDI注射与JUnit,junit,ejb,cdi,openjpa,apache-tomee,Junit,Ejb,Cdi,Openjpa,Apache Tomee,我正在尝试将JUnit测试添加到使用CDI和JPA的Tomee web应用程序中。除去过去两周的不同障碍(包括对stackoverflow和其他来源的深入研究),我的问题似乎相当具体: 运行单元测试会显示以下错误消息: org.apache.openejb.Injector$NoInjectionMetaDataException: org.demo.service.GenericServiceTest : Annotate the class with @javax.annotation.M
org.apache.openejb.Injector$NoInjectionMetaDataException:
org.demo.service.GenericServiceTest : Annotate the class with @javax.annotation.ManagedBean so it can be discovered in the application scanning process
at org.apache.openejb.Injector.inject(Injector.java:54)
at org.apache.openejb.OpenEjbContainer$GlobalContext.bind(OpenEjbContainer.java:656)
...
我不明白这一点,因为我已经根据需要对类进行了注释。你知道我能做些什么来解决这个问题吗
(或者错误是指注入的类-GenericService吗?我无法注释这个@ManagedBean,因为它已经是@Stateless)
以下是我的项目的一些细节:
渐变依赖项:
dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
compile "com.googlecode.json-simple:json-simple:1.1.1"
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.6'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
compile 'log4j:log4j:1.2.16'
testCompile "org.apache.openejb:tomee-embedded:1.7.3"
compile "javax:javaee-api:7.0"
compile 'mysql:mysql-connector-java:5.1.16'
}
(我将testCompile放在两者之间,因为有一张票据说明了订单的重要性:)
我的测试课程是:
package org.demo.service;
import java.io.File;
import java.util.Properties;
import javax.annotation.ManagedBean;
import javax.ejb.embeddable.EJBContainer;
import javax.inject.Inject;
import javax.naming.NamingException;
import org.apache.openejb.OpenEjbContainer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ManagedBean
public class GenericServiceTest {
private static EJBContainer ejbContainer;
@Inject
private GenericService service;
@Test
public void test() throws NamingException {
System.out.println("service: " + service);
}
@BeforeClass
public static void start() {
Properties p = new Properties();
try {
p.put(EJBContainer.MODULES, new File("bin"));
} catch (Exception e1) {
e1.printStackTrace();
}
p.put(OpenEjbContainer.Provider.OPENEJB_ADDITIONNAL_CALLERS_KEY, GenericService.class.getName());
ejbContainer = javax.ejb.embeddable.EJBContainer.createEJBContainer(p);
}
@Before
public void inject() throws NamingException {
ejbContainer.getContext().bind("inject", this);
}
@AfterClass
public static void shutdownContainer() {
if (ejbContainer != null) {
ejbContainer.close();
}
}
}
也许我在这里完全朝着错误的方向运行-请告诉我是否应该选择不同的方法-我只想在我的web应用程序中添加单元测试(最好不要引入Weld/JBoss或其他实现作为我在应用程序本身中已经使用的实现的替代方案)
提前非常感谢 我建议你阅读,这里有一些例子
关于您的测试,它使用openejb embedded而不是tomee embedded,并将bin/部署为应用程序。黑客绑定(“注入”)仅适用于类路径部署(无模块)。@ManagedBean不是CDIBean。你的方向完全错了。尝试使用Arquillian-Arquillian提供了您想要的。谢谢Romain-这真的很有帮助!JUnits正在运行。回一个问题:我正在使用:@RunWith(SingleApplicationComposerRunner.class)和一个带有@Classes注释的模型类来包含所有要注入的类,这可能会变得非常多。有没有一种方法可以包括要扫描CDI的目录?(at)默认设置为“current”模块(org.apache.openejb one)和(at)JAR可以匹配classpath-~模块名上的文件名。