[ajug-members] Opinions on a project re-write - To EJB3 or Not

Barry Hawkins barry at alltc.com
Fri May 30 16:46:23 EDT 2008


Burk,
     Heya!  Yes, this is a good example of a simple SLSB that can be  
unit tested; however, I have never seen such a simple SLSB in real  
application code.  If my SLSBs were that simple (i.e., not using JNDI,  
JPA, etc.), I'd have to wonder why I was bothering to run in an EJB  
container in the first place ;-).  Add in something as small as a JNDI  
annotation and one should experience the cataclysmic stack traces I  
became so familiar with over a year ago :-).

Barry

On May 30, 2008, at 8:00 AM, SilverAnvil wrote:

> Barry,
> I've heard that you can't unit test EJBs out of the container, but I  
> disagree. Here's proof. Let's start with a simple stateless session  
> bean:
> package com.testejbs;
>
> import javax.ejb.Stateless;
>
> @Stateless
> public class SimpleStatelessSessionBean implements  
> SimpleStatelessSessionRemote {
>     public int add(int first, int second) {
>         return first + second;
>     }
> }
>
> As you can see it has a public method that adds two integers. So  
> here's the unit test using JUnit 3.8:
> package com.cp.testejbs;
>
> import junit.framework.TestCase;
>
> public class SimpleSessionTest extends TestCase {
>     public SimpleSessionTest(String testName) {
>         super(testName);
>     }
>
>     public void  testAdd() {
>         int first = 2;
>         int second = 3;
>         int expectedAnswer = first + second;
>
>         SimpleStatelessSessionBean simpleBean = new  
> SimpleStatelessSessionBean();
>         int answer = simpleBean.add(first, second);
>         assertEquals(answer, expectedAnswer);
>     }
> }
>
> If you compile the code and run the test you'll see it works. BTW,  
> you can do the same thing with an EJB 2.0 bean as well.
>
> Burk
[...]

-- 
Barry Hawkins
All Things Computed
site: http://www.alltc.com
weblog: http://www.yepthatsme.com







More information about the ajug-members mailing list