CDI注入和JUnit
CDI injection and JUnit
我正在尝试将 JUnit 测试添加到使用 CDI 和 JPA 的 Tomee Web 应用程序。撇开过去两周的不同障碍(包括对 Whosebug 和其他来源的非常深入的研究),我的问题似乎非常具体:
运行 单元测试给出了这个错误信息:
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)
...
这个我不明白,因为我已经按照要求对class进行了注释。知道我能做些什么来解决这个问题吗?
(或者错误是指注入的class - GenericService?我无法注释这个@ManagedBean,因为它已经是@Stateless)。
以下是我的项目的一些细节:
gradle 依赖关系:
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放在中间了,因为有一张票说明了顺序的重要性:EJB testing with TomEE embedded EJBContainer api: java.lang.ClassFormatError exception)
我的测试class是这样的:
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 或其他实现来替代我已经在应用程序本身中使用的实现。
非常感谢您!
我会推荐你阅读 http://tomee.apache.org/developer/testing/index.html ,有一些例子
关于您的测试,它使用 openejb embedded 而不是 tomee embedded 并将 bin/ 作为应用程序部署。 hack bind("inject") 仅适用于类路径部署(无模块)。
我正在尝试将 JUnit 测试添加到使用 CDI 和 JPA 的 Tomee Web 应用程序。撇开过去两周的不同障碍(包括对 Whosebug 和其他来源的非常深入的研究),我的问题似乎非常具体:
运行 单元测试给出了这个错误信息:
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)
...
这个我不明白,因为我已经按照要求对class进行了注释。知道我能做些什么来解决这个问题吗?
(或者错误是指注入的class - GenericService?我无法注释这个@ManagedBean,因为它已经是@Stateless)。
以下是我的项目的一些细节:
gradle 依赖关系:
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放在中间了,因为有一张票说明了顺序的重要性:EJB testing with TomEE embedded EJBContainer api: java.lang.ClassFormatError exception)
我的测试class是这样的:
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 或其他实现来替代我已经在应用程序本身中使用的实现。
非常感谢您!
我会推荐你阅读 http://tomee.apache.org/developer/testing/index.html ,有一些例子
关于您的测试,它使用 openejb embedded 而不是 tomee embedded 并将 bin/ 作为应用程序部署。 hack bind("inject") 仅适用于类路径部署(无模块)。