Embedding Glassfish V3 in Unit Test - Two Jars, Three Lines Of Code And Five Seconds Start With Deployment

There is already a great blog entry about using Glassfish v3 in embedded mode (I "reused" some ideaas and even code from the post - this is how web works :-)). It explains the whole process, to install, build and test Glassfish v3 using maven 2. However I just liked to do it without maven - and as easy as possible. To start glassfish you only need two jar:

  • gf-embedded-api-1.0-alpha-4.jar
  • web-all-10.0-build-20080430.jar

Having them in the classpath, you can start glassfish v3 just from your code - in process:

  GlassFish glassfish = new GlassFish(port);
It is not only possible to start glassfish in embedded code, it is even possible to deploy applications on the fly. The following code does exactly that:

ScatteredWar war = new ScatteredWar(NAME, new File("src/main/resources"), new File("src/main/resources/WEB-INF/web.xml"),   Collections.singleton(new File("build/classes").toURI().toURL()));
glassfish.deploy(war);

...and the full test code:

import org.glassfish.embed.GlassFish;
import org.glassfish.embed.ScatteredWar;
//and other obvious imports

public class HelloServletTest{
   
private final String NAME = "AppTest";
public static int port = 9999;

private GlassFish glassFish;

    @Before
    public void bootGlassfish() throws Exception{
        this.glassFish = newGlassFish(port);
        assertNotNull(this.glassFish);
    }
    @Test
    public void testServlet() throws Exception {
        URL url = new URL("http://localhost:" + port + "/" + NAME + "/HelloServlet");
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                url.openConnection().getInputStream()));
        assertEquals("Hallo from servlet", br.readLine());
    }

    private GlassFish newGlassFish(int port) throws Exception {
        GlassFish glassfish = new GlassFish(port);
        ScatteredWar war = new ScatteredWar(NAME,
                new File("src/main/resources"),
                new File("src/main/resources/WEB-INF/web.xml"),
                Collections.singleton(new File("build/classes").toURI().toURL()));
        glassfish.deploy(war);
        System.out.println("Ready ...");
        return glassfish;
    }
   
    @After
    public void shutdown(){
           this.glassFish.stop();
    }

The unit test is only a sample, in general I woul start glassfish in the static @BeforeClass method - for one test it is good enough.
Glassfish v3 is not production ready yet - but it becomes more and more interesting for development. Cannot wait for embeddable EJB 3 containers :-).

I checked in the whole project (with servlet, glassfish jars and the unit test) into the p4j5. The project name is: EmbeddedGlassfishWeb. The remaining question is: from where I have this two jars - the answer is simple: from the maven repository :-).

So, and I'm going to present this stuff now  - I'm at Jazoon :-).

Comments:

I am also looking forward to an embedded Glassfish 3 to unit test EJBs 3.0. JBoss has an embedded server for a long time now, but it seems neglected (they probably focus on their 5.0 release).

So lacking a nice embedded server, I am currently looking at EJB3Unit, which does the injecting and mimiks database access using HSQLDB. I plan to blog about it the next days...

Posted by Markus Junginger on June 26, 2008 at 03:10 PM CEST #

5 seconds is amazing, yet still too long for me to wait for unit tests to finish.

EJB will only survive if you can finally test/mock/stub them super-easily and run them without any container whatsoever.

Posted by Sakuraba on June 26, 2008 at 10:28 PM CEST #

Sakuraba,

in general: you can start Glassfish and deploy your app once, then test different aspects. The more tests you have, the faster will it appear :-).

There is another option as well - you could drink some coffee during the 5 seconds :-)

Posted by Adam Bien on June 27, 2008 at 08:07 AM CEST #

hi there,

good to know that we can embed glassfish. I'm wondering where to the JAR file (gf-embedded-api-1.0-alpha-4.jar or whatever is the current version ) ?

Thank you,

BR,
~A

Posted by anjan bacchu on July 18, 2008 at 01:56 AM CEST #

@Sakuraba: I'm sure 5 seconds could be less if ran inside an IDE (and Adam's argument about the app server starting only once).

@Anjan: if you haven't found where to download glassfish embedded, try http://maven.dyndns.org/glassfish/org/glassfish/embedded (and its main project site: https://embedded-glassfish.dev.java.net/)

Posted by Alexis MP on September 09, 2008 at 09:52 PM CEST #

Hi Adam,
was Glassfish Embeddable API changed in 3.0 versions ?
How you can use it now?

thank you,
D.

Posted by Domenico on October 20, 2009 at 02:17 PM CEST #

I know this is an old article/post, but lots of people keep referring to it, so this seems like the right place to point people at Arquillian, a JBoss project that runs a JUnit test as though it were a pseudo-application client. The test itself can be run against embedded glassfish. See http://community.jboss.org/docs/DOC-14376?uniqueTitle=false for details.

Posted by Laird Nelson on April 07, 2010 at 06:05 PM CEST #

Oh, and @Domenico: http://docs.sun.com/app/docs/doc/821-1208/gihyr?a=view

Posted by Laird Nelson on April 07, 2010 at 06:06 PM CEST #

Updated link: https://blogs.oracle.com/arungupta/entry/embeddable_glassfish_in_action_servlet

Posted by Matthew Cornell on March 05, 2012 at 08:45 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License