ejb@glassfish.java.net

Re: Jnuit test for EJB3 session bean

From: Kenneth Saks <Kenneth.Saks_at_Sun.COM>
Date: Mon, 21 Apr 2008 17:11:03 -0400

On Apr 21, 2008, at 3:12 PM, Deepa Singh wrote:

> Thanks a lot Ken. Glad to know that EJB 3.1 will be supporting
> simplified local access without separate business interface.
> Now my follow-up question
>
> What would be quick way to unit test a Session bean? Should I either
> use in-container testing approach using Cargo API to start, stop
> J2EE container and deploy application, or go mock testing route with
> out-of-container testing scenario using MockEJB perhaps?

An approach in which the components are running within an actual EE
managed container environment is best. I haven't used Cargo but if
it makes it easy to integrate the deployment/execution/undeployment
steps to a server that's good. There are also implementations (e.g.
OpenEJB) that have vendor-specific APIs for instantiating the
container within the client JVM. That lets you use a wider variety
of testing frameworks without having to start, deploy, and tear-down
the server for each set of test runs.

>
>
> My main concern is not to miss any glaring functionality but testing
> just getters and setters don't make sense also.
>
> Seems like EJB 3.1 will be solving problem of quick unit testing of
> Session Bean.

That's the plan :-)

  --ken

>
> On Mon, Apr 21, 2008 at 10:30 AM, Kenneth Saks
> <Kenneth.Saks_at_sun.com> wrote:
>
> On Apr 21, 2008, at 12:54 PM, Deepa Singh wrote:
>
> Hello All,
> I have very simple EJB3.0 session bean with no resource injection,
> no reference to other Java EE component.
> Without deploying EJB 3.0 to an appserver, I am still able to run my
> JUnit test case for that session bean. I thought that until I deploy
> that bean, call it using its JNDI reference name, I should not be
> able to access it. But test is still working without deployment.
>
> Can some one give an explanation what is going on?
>
> Hi Deepa,
>
> Your test is using new() to explicitly instantiate the bean class so
> the container isn't involved at all. You won't be getting any of
> the EJB semantics.
>
> Some vendors support 1st class EJB component execution within a Java
> SE client but there are no EJB 3.0 requirements in that area. It's
> something we're planning to address within the EJB 3.1 spec.
>
> --ken
>
>
>
>
> EJB 3.0 session bean
> import javax.ejb.Stateless;
> import javax.ejb.*;
>
>
> @Stateless
> @Remote({CompanySessionLocal.class})
> public class CompanySessionBean implements CompanySessionLocal {
>
> public String helloWorld() {
> String helloworld="Hello World";
> return helloworld;
> }
>
> }
>
> JUnit Test Case:
> public class CompanySessionBeanTest {
>
> public CompanySessionBeanTest() { }
>
> @Test
> public void helloWorld() {
> System.out.println("helloWorld");
> CompanySessionBean instance = new CompanySessionBean();
> String expResult = "Hello World";
> System.out.println("From bean-"+expResult);
> String result = instance.helloWorld();
> assertEquals(expResult, result);
>
> }
>
>
> Thanks,
> Deepa
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ejb-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: ejb-help_at_glassfish.dev.java.net
>
>