3

我创建了一个 EJB3.1 并使用 @inject 注入了 CDI bean,但在单元测试时遇到了一些问题,但是当从 servlet 测试时它工作正常。我在 WEB-INF 文件夹中有 beans.xml。下面是我的 EJB 代码:

@Stateless
public class CdiUsingEjb {
    @Inject
    private HelloServletCDIPojo helloServletCDIPojo;

    public String greet() {
        assert helloServletCDIPojo != null;
        return helloServletCDIPojo.from();
    }
}

下面是我的 CDI bean:

public class HelloServletCDIPojo {
    public String from() {
        return "from HelloServletStateless CDI";
    }
}

我创建了一个 JUnit 类:

public class CdiUsingEjbTest {
    @EJB
    private CdiUsingEjb cdiUsingEjb;

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

    @Test
    public void test() {
        assertNotNull(cdiUsingEjb);
    }
}

当我运行这个 JUnit 测试时,我得到了以下错误。

org.apache.openejb.OpenEjbContainer$AssembleApplicationException: org.apache.openejb.OpenEJBException: Creating application failed: /Users/prasannakumar/freelanceWorkspace/javaWorkspace/TomEEExamples: couldn't start owb context
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:310)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43)
    at in.jugchennai.prassee.ejb.HelloServletStatelessEJBTest.setup(HelloServletStatelessEJBTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
4

0 回答 0