问题
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