问题与EJB 3.1与CDI豆吹入运行JUnit(Issue with EJB 3.1 inject

2019-09-20 09:36发布

我创建了一个EJB3.1和使用@Inject但在单元测试然而,当从servlet其做工精细测试面临的一些问题,注入CDI豆。 我在WEB-INF文件夹中的beans.xml。 下面是我的EJB的代码:

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

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

下面是我的CDI豆:

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)
文章来源: Issue with EJB 3.1 injected with CDI bean while running JUnit