JUnit测试用例中@EJB注入的空值

时间:2012-07-12 15:39:55

标签: ejb-3.1

我在MyEclipse 10.0中使用WAS 8.0,EJB 3.1,CDI和EJB可嵌入容器。还在classpath中定义了beans.xml和可嵌入的容器jar。在测试类中的EJB引用(课程)上获取运行时null。

public class CourseTest扩展了TestCase {

@EJB    
private Course course;

@Before
public void setUp() throws Exception {
    EJBContainer.createEJBContainer().getContext().bind("inject", this);
}

@Test
public void test() {

    // Was the EJB injected?
    assertTrue(course != null);

1 个答案:

答案 0 :(得分:0)

您的testcase类不是由容器构造的,因此不会执行注入。您需要从EJBContainer.createEJBContainer().getContext()手动查找EJB。

(顺便说一句,EJBContainer API每个进程只允许一个活动的EJB容器。我建议将EJBContainer.createEJBContainer()的结果存储在一个字段中并从@After方法中关闭它。)