users@glassfish.java.net

Re: Embedded testing woes

From: Arun Gupta <Arun.Gupta_at_Sun.COM>
Date: Wed, 07 Apr 2010 16:16:23 -0700

On 4/7/10 1:12 PM, glassfish_at_javadesktop.org wrote:
> So many articles about EJBContainer, so many that don't work!
>
> There's a great article here (http://www.adam-bien.com/roller/abien/entry/embedding_ejb_3_1_container) showing how to get an EJB 3.1 container running. It claims that no further configuration is necessary. This implies I don't have to set up a filesystem, or tell the testing machinery where to create the embedded EJB container, or anything. That's great; so I don't. I just say EJBContainer.createEJBContainer() and off I go.
I followed this blog ...

... created a template Maven project
... changed generated App.java to:

@Stateless
public class App {

     public static void main(String[] args) {
         System.out.println("Hello World!");
     }

     public static String sayHello(String name) {
         return "Hello " + name;
     }
}

... added a new method in generated AppTest.java as:

     public void testEJB() throws NamingException {
         EJBContainer ejbC = EJBContainer.createEJBContainer();

         Context ctx = ejbC.getContext();

         App app = (App) ctx.lookup("java:global/classes/App");

         assertNotNull(app);

         String NAME = "Duke";

         String greeting = app.sayHello(NAME);

         assertNotNull(greeting);

         assertTrue(greeting.endsWith(NAME));

         ejbC.close();
     }

... and invoked mvn test to see:

Apr 7, 2010 3:06:10 PM com.sun.enterprise.security.SecurityLifecycle
onInitialization
INFO: Security service(s) started successfully....
Apr 7, 2010 3:06:11 PM com.sun.ejb.containers.BaseContainer initializeHome
INFO: Portable JNDI names for EJB App :
[java:global/classes/App!org.glassfish.embedded.samples.App,
java:global/classes/App]
Apr 7, 2010 3:06:11 PM org.glassfish.admin.mbeanserver.JMXStartupService
shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
Apr 7, 2010 3:06:11 PM com.sun.enterprise.v3.server.AppServerStartup stop
INFO: Shutdown procedure finished
Apr 7, 2010 3:06:11 PM AppServerStartup run
INFO: [Thread[GlassFish Kernel Main Thread,5,main]] exiting
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 8.429 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

So the instructions for a minimal EJB did work fine for me. I had to
change the dependency to "compile" instead of "test" in order to compile
the bean. There are other ways to solve this problem but the promise of
"no further configuration" seems fine with this setup.

I'll try adding JPA to the mix and see this changes.

-Arun

>
> (It starts up, I should add, barfs out several SEVERE messages that look alarming concerning the fact that it doesn't know where its directory is, even though I'm told I don't have to supply one.)
>
> When I fire up a similar JUnit test under Maven, lo and behold Glassfish comes up, tells me no modules were found (!!), and then, understandably, fails when I attempt to do any lookup--any at all, including of "java:", "java:global", etc.--since nothing got installed in it. So much for no further configuration necessary!
>
> Another article here (http://blogs.sun.com/alexismp/entry/testing_ejb_3_1_s) implies that EJBs that are found at container startup time will be deployed. What does that mean? Do I have to package up an ejb-jar? Can't I leave my classes in their maven directories (target/classes, target/test-classes)? What's going on here? Isn't this configuration? Shouldn't my code get found, as described in the article? If not, where are the steps I need to perform so that Glassfish finds my stuff?
>
> So, having read these two articles and the product documentation, I still can't figure out how to test my EJB. What piece of documentation should I read next?
>
> Thanks,
> Laird
> [Message sent by forum member 'ljnelson']
>
> http://forums.java.net/jive/thread.jspa?messageID=395759
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>

-- 
Blog: http://blogs.sun.com/arungupta
Twitter: http://twitter.com/arungupta