Netbeans, JUNit and Glassfish embedded.

JUnit testing a Java EE app, you use the glassfish embedded container to run and test your beans. The glassfish embedded jar should be added to your Netbeans testing libraries by Netbeans when you first create tests (right click a class, Tools -> Create Tests). The jar can be found in the Glasfish install directory under glassfish/lib/embedded/glassfish-embedded-static-shell.jar.

You then create the EJB container in your tests like :

//create the embedded EJB container.
EJBContainer ejbc = EJBContainer.createEJBContainer();

//get the Context from the container so we can lookup the class we're testing
Context ctx = ejbc.getContext();

Now in the JUNit test class we can find an instance of oru class from the context and test it’s methods
If you don’t know the JNDI lookup name exactly of your class, you can find it in the startup output of glassfish (In the Output pane of Netbeans). It should look something like :

INFO: EJB5181:Portable JNDI names for EJB MyClass[java:global/classes/MyClass!bean.MyClass, java:global/classes/MyClass]

//Look up our class.
MyClass myClass = MyClass ctx.lookup("java:global/classes/MyClass");

//now execute whatever methods you want


If you’re hittnig the persistence libraries, you may run into errors in your tests. Null pointer exceptions when getting an entity manager or DB connection, or NamingException when trying to do a JNDI lookup for the DataSource, especially if you’ve defined them int glassfish-resources.xml and persistence units. Even though when you deploy to a normal Glassfish container, there are no errors using JNDI lookup.

This is because the Glassfish embedded container does not know about the datasources specified in the persistence unit files, or their JNDI names. So we need a method to tell our embedded glassfish what configuration to use when it starts up.

Luckily for us, there’s a property called


that we can pass to the EJBContainer on startup that will tell it to load a config. But what config file to load? Well, assuming that you want to use the same datasources to run your tests as your using on the local machine to deploy the app, you can grab

domain.xml file from the domain folder of glassfish. Where to find? If you go to Netbeans, in the Services tab, right click on the GlassFish server instance you want to get the config from, and click properties. You should see an entry called “Domains Folder”. Go to that folder, find your domain (the default is domain1), go to that folder, and in the config folder there should be a domain.xml file.

Where to put this file? In your Netbeans project folder (the one with src, dist, build etc. folders in it), create another folder called “test-resources” (or whatever you want). Copy the domain.xml file into there.

Now go to your test where we created the EJB container and change this line :

EJBContainer ejbc = EJBContainer.createEJBContainer();


Map properties = new HashMap();
properties ("org.glassfish.ejb.embedded.glassfish.configuration.file", "test-resources/domain.xml");
ejbc= EJBContainer.createEJBContainer(properties);

now re-run your tests and they should pass.


You probably would want to create a test suite, and then create the EJB before the suite runs, to avoid doing it for every class since it’s pretty resource heavy to start it up and shut it sown for every test.