2

Does anyone know anything about Embedded Glassfish? I want to run some of my EJB tests, but I do not want to start and stop the glassfish-embedded every time I run a test.

According to the plugin documentation I should put this in the POM :

            <plugin>
            <groupId>org.glassfish</groupId>
            <artifactId>maven-embedded-glassfish-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <app>target/ejbcontainer-1.0-SNAPSHOT.jar</app>
                <name>test</name>
                <ports>
                    <http-listener>8080</http-listener>
                    <https-listener>8181</https-listener>
                </ports>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>start</goal>  
                        <goal>deploy</goal>
                        <goal>undeploy</goal>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin> 
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <skip>true</skip>
            </configuration>
            <executions>
                <execution>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <skip>false</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>

This is all good. I can 'run' this embedded glassfish and I get this in my console which proves that its up and running :

Information: test was successfully deployed in 1,124 milliseconds. PlainTextActionReporterSUCCESSDescription: deploy AdminCommandApplication deployed with name test. [name=test Dec 16, 2013 6:03:29 PM PluginUtil doDeploy Information: Deployed test Hit ENTER to redeploy, X to exit

However when I 'Run' My Test files, a new instance of the embedded glassfish is created.

My test files are not picking up the currently running container.

Here is a Testfile if it helps :

public class Test extends TestCase {

    private Context ctx;
    private EJBContainer ejbContainer;

    public Test(String testName) {
        super(testName);
    }

    @BeforeClass
    public void setUp() {
        ejbContainer = EJBContainer.createEJBContainer();

        System.out.println("Opening the container");

        ctx = ejbContainer.getContext();
    }

    @AfterClass
    @Override
    public void tearDown() {
        ejbContainer.close();
        System.out.println("Closing the container");
    }

    @org.junit.Test
    public void testApp() throws Exception {
        TemperatureConverter converter = (TemperatureConverter) ctx.lookup("java:global/classes/TemperatureConverter");
        assertNotNull(converter);
    }
}

1 Answer 1

2

Just spotted this question as I've been playing around with embedded glassfish myself and having a few config issues, mainly related to logging output.

The way I use embedded glassfish for testing is to bind it to the Maven integration test phase e.g.

          <executions>
            <!-- Start embedded GlassFish -->
            <execution>
                <id>start</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start</goal>
                </goals>
            </execution>
            <execution>
                <id>deploy</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>deploy</goal>
                </goals>
            </execution>
            <execution>
                <id>stop</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>undeploy</goal>
                    <goal>stop</goal>
                </goals>
            </execution>
          </executions>

And use the Maven Failsafe Plugin to execute tests as part of the verify goal. These become more like integration tests though. If you name your test file with a suffix of IT e.g. myTestFileIT.java then they should be picked up automatically.

You can then run the tests by executing the following Maven command:

 mvn verify

I was originally using embedded Jetty which is where I had this setup working really well, I've found glassfish to be a bit more fiddly and quite time consuming to configure exactly as I require.

Hope this helps in some way with your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.