EJB support in cdi-unit

454 views
Skip to first unread message

Sri Ram

unread,
Jan 30, 2015, 11:35:23 PM1/30/15
to cdi-...@googlegroups.com
Hey,

I was reading through the  'Ejb support'  (section 10) of the documentation. I really didn't understand it well.

Example provided in documentation :
@RunWith(CdiRunner.class)
@AdditionalClasses({ EJB.class, EJBByClass.class, EJBStatelessNamed.class})
@SupportEjb
class TestEjb {
 
   
@EJB
    EJBI inject
;
 
   
@EJB(beanName = "statelessNamed")
    EJBI injectNamed
;
 
   
@EJB(beanName = "TestEjb.EJBByClass")
    EJBI injectStateless
;
 
   
static class EJB implements EJBI {
   
}
 
   
static class EJBByClass implements EJBI {
   
}
 
   
@Stateless(name = "statelessNamed")
   
static class EJBStatelessNamed implements EJBI {
   
}        
 
}
}

I've few doubts, please clarify it.
1) Is it possible to use inject the @EJB beans as normal cdi beans in the test?
2) Why that example has static classes? And what is EJBI?
3) Can you point me to some simple example?
4) Does this following example will work with cdi-unit?

@RunWith(CdiRunner.class)
@AdditionalClasses({ EJB.class, MyBeanImpl.class})
@SupportEjb
class TestEjb {
 
   
@EJB
   
MyBean myBean;

   
@Test
   
public void testSomething() {
       
String output = myBean.getSomething("test");
        assertEquals
("test", output);
   
}
 
 
}
}

@Stateless
class MyBeanImpl implements MyBean {
   
   
@Override
   
public String testSomething(String arg0) {
       
return arg0;
   
}
}


Bryn Cooke

unread,
Feb 2, 2015, 4:28:44 AM2/2/15
to cdi-...@googlegroups.com
Hi

I've few doubts, please clarify it. 
1) Is it possible to use inject the @EJB beans as normal cdi beans in the test? 
Basically CDIUnit makes @EBJ an alias for @Inject.

2) Why that example has static classes? And what is EJBI?

No reason in particular. I've moved them out so they are non static in the example.

3) Can you point me to some simple example?
The user guide has the simplest example http://jglue.org/cdi-unit-user-guide/#ejb

4) Does this following example will work with cdi-unit?
It should do.
@AdditionalClasses({ EJB.class, MyBeanImpl.class})
can change to 
@AdditionalClasses({ MyBeanImpl.class})

It was my mistake in the docs.

Just remember that when using cdi-unit @EJB = @Inject

Bryn

Sri Ram

unread,
Feb 3, 2015, 1:58:15 PM2/3/15
to cdi-...@googlegroups.com
Thanks Bryn for the clarification. Now I understand how it works.

i changed that example as you said, but still it didn't work. It throws the following exception :
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.159 sec <<< FAILURE!
testSomething
(org.junit.cdiunit.MyBeanUnitTest)  Time elapsed: 1.075 sec  <<< ERROR!
org
.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type MyBean with qualifiers @Default
  at injection point
[UnbackedAnnotatedField] @Inject @EJB org.junit.cdiunit.MyBeanUnitTest.myBean
  at org
.junit.cdiunit.MyBeanUnitTest.myBean(MyBeanUnitTest.java:0)
WELD
-001475: The following beans match by type, but none have matching qualifiers:
 
- Managed Bean [class org.junit.cdiunit.MyBeanImpl] with qualifiers [@EJbQualifier @Any]

    at org
.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:370)
    at org
.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:291)
    at org
.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
    at org
.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:165)
    at org
.jboss.weld.bootstrap.Validator.validateBean(Validator.java:529)
    at org
.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
    at org
.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
    at org
.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org
.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java
.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java
.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java
.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java
.lang.Thread.run(Thread.java:745)


So, I've forked your project and added the ejb examples that I'm talking about.

Please check that ejb example from my fork : https://github.com/ramsrib/cdi-unit/tree/master/cdi-unit-tests-ejb

Am I missing something there?

Thanks again for fixing the documentation. :)

Bryn Cooke

unread,
Feb 6, 2015, 4:30:59 AM2/6/15
to cdi-...@googlegroups.com
Hi,

I had a brief look at this and can see that there is an issue, but I'm not sure what the problem is. Try putting a beans.xml in the correct location.

Unfortunately I have just started a new job so can't dedicate the time to looking at EJB support in detail.
If you want to have a look the EJBExtension is the place where everything happens. It just adds qualifiers to and inject annotations to simulate an EJB environment.

Sorry I can't help further at this time.

Bryn
Reply all
Reply to author
Forward
0 new messages