How to use embedded glassfish from junit?

In my previous post I explaind that with the new EJB you can use an embedded EJB container to test your EJB. But there is also another way and that is to use embedded glassfish that is started from inside an junit. The nice thing here is that it you can start an embedded glassfish with the container you need, with EJB or with WEB or both if you need.

You can get the whole package to get you started from here.

Again we start of with an pom.xml for maven

<project xmlns="" xmlns:xsi=""

Now we take the sources from here and copy them to src/main/java/nl/coralic/blog/ejb31/

The presistance.xml from that sample goes here src/main/resources/META-INF

If you have read my other post “How to test EJB3.1 with embedded container?” you have seen that for an embedded container we needed an domain.xml and ds_jdbc_ra. Luckily using embedded glassfish this way we don’t need that anymore. Two reasons for that:
1)when you do your test glassfish does complain about that it can not find ds_jdbc_ra and then goes off and creates it for it self, so no problem here
2)we don’t need the domain.xml to tell glassfish how to configure it self because we can do that in our junit test

We need some junit to test our EJB, first we create an junit test.

package test;

import javax.naming.InitialContext;
import javax.naming.NamingException;


import org.glassfish.api.embedded.LifecycleException;
import org.junit.Test;

public class ItemTest {
	public void addItem() throws NamingException, LifecycleException
		InitialContext ic = new InitialContext();
		ItemService itemService = (ItemService)ic.lookup("java:global/ejbembeddedtest-0.0.1-SNAPSHOT/ItemService");
		Item item = new Item();

		System.out.println("Get all items from DB and show itemname for the first one: " + itemService.getItems().get(0).getItemName());


And as last we create an junit suite that starts and stops our server and invokes our test.

package test;


import junit.framework.Test;
import junit.framework.TestSuite;

import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.CommandRunner;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.api.embedded.ContainerBuilder;
import org.glassfish.api.embedded.EmbeddedDeployer;
import org.glassfish.api.embedded.EmbeddedFileSystem;
import org.glassfish.api.embedded.LifecycleException;
import org.glassfish.api.embedded.Server;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@Suite.SuiteClasses( { ItemTest.class })
public class TestItemSuite
	public static Server server;
	private static EmbeddedDeployer embdeployer;

	public static void setUp() throws LifecycleException

		Server.Builder builder = new Server.Builder("coralicglassfish");
		EmbeddedFileSystem.Builder efsb = new EmbeddedFileSystem.Builder();
		efsb.installRoot(new File("target/glassfish/installroot"));
		efsb.instanceRoot(new File("target/glassfish/instanceroot"));
		EmbeddedFileSystem efs =;

		Server server =;

		ContainerBuilder ejbContainer = server.createConfig(ContainerBuilder.Type.ejb);


		String com = "create-jdbc-connection-pool";
		ParameterMap props = new ParameterMap();
		props.add("datasourceclassname", "org.apache.derby.jdbc.EmbeddedDataSource");
		props.add("restype", "javax.sql.DataSource");
		props.add("ping", "true");
		props.add("property", "ConnectionAttributes=create\\=true:DatabaseName=target/itemDataBase");
		props.add("DEFAULT", "itemDataBase");
		CommandRunner run = server.getHabitat().getComponent(CommandRunner.class);
		ActionReport rep = server.getHabitat().getComponent(ActionReport.class);
		run.getCommandInvocation(com, rep).parameters(props).execute();

		String command = "create-jdbc-resource";
		ParameterMap par = new ParameterMap();
		par.add("connectionpoolid", "itemDataBase");
		par.add("jndi_name", "jdbc/itemDataBase");
		CommandRunner runner = server.getHabitat().getComponent(CommandRunner.class);
		ActionReport report = server.getHabitat().getComponent(ActionReport.class);
		runner.getCommandInvocation(command, report).parameters(par).execute();

		File fileToDeploye = new File("target/ejbembeddedtest-0.0.1-SNAPSHOT.jar");
		EmbeddedDeployer deployer = server.getDeployer();
		DeployCommandParameters params = new DeployCommandParameters();
		deployer.deploy(fileToDeploye, params);


	public static Test suite()
		TestSuite suite = new TestSuite("Test");
		// $JUnit-BEGIN$
		// $JUnit-END$
		return suite;

	public static void tearDown() throws LifecycleException
		if (embdeployer != null)
		if (server != null)

Everything need for this test is setup all you have to do is fire up the test with maven

mvn clean compile jar:jar test