Issue with EJB 3.1 injected with CDI bean while running JUnit

大憨熊 提交于 2019-12-22 10:18:47

问题


I created a EJB3.1 and injected CDI bean using the @inject but facing some issues while unit testing however when tested from the servlet its working fine. I have the beans.xml in the WEB-INF folder. Below is my EJB code:

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

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

Below is my CDI bean:

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

I created a JUnit class:

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);
    }
}

When I run this JUnit test I got the following error.

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)

来源:https://stackoverflow.com/questions/11709548/issue-with-ejb-3-1-injected-with-cdi-bean-while-running-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!