Using the Embedded EJB Container to Test Enterprise Applications

This tutorial demonstrates how to create and run JUnit tests for a Java EE enterprise application.
In this tutorial you will create a web application with an entity class and a session bean.
You will first create a JUnit test class for the session bean and run the test in the embedded EJB container.
You will then add an entity class to the project and modify the test class to add a test method
for the entity class.

Note. It is not necessary to add an @LocalBean
annotation or implement an interface in this tutorial.
By default the bean exposes a no-interface view when no view is explicitly specified.

Save your changes.

Testing the Session Bean

In this exercise you will create a test class for the session bean that will test the addNumbers method.
The IDE can generate the new test class and skeleton test methods based on the methods in the target class.

Note. The first time that you create a JUnit unit test you need to specify the JUnit version.
Select JUnit 4.x in the Select JUnit Version dialog box and click Select.

When you click OK, the IDE generates the MyBeanTest.java file and opens the class in the editor.

In the Projects window you can see that the IDE generated the test class under the Test Packages node.
By default, the IDE generates a skeleton test method in the test class
that calls javax.ejb.embeddable.EJBContainer.createEJBContainer() to create an EJB container instance.
The createEJBContainer() method is one of the methods in the
EJBContainer class that is part of the EJB 3.1 Embeddable API.

If you expand the Test Libraries node in the Projects window, you can see that the IDE automatically added
GlassFish Server (embeddable container) and JUnit 4.x as test libraries.
If you expand the GlassFish Server library, you can see that the library contains the
glassfish-embedded-static-shell.jar.

Note. The glassfish-embedded-static-shell.jar JAR
does not contain the sources for the embedded EJB container.
The glassfish-embedded-static-shell.jar JAR requires a local installation of GlassFish.
The classpath for the local GlassFish installation is determined by the target server for the project.
You can change the target server in the project's Properties dialog box.

Modify the generated skeleton test method to specify values for numberA, numberB
and expResult and remove the default call to fail.

Modifying the Test to Specify Container Properties

When you used the Create Tests wizard, the IDE generated a default skeleton test class that contained code for starting the EJB container.
In this exercise you will modify the generated code that starts the container to enable you to
specify additional properties for the embedded container instance.

Add the following code (in bold) to the test class.

@Test
public void testAddNumbers() throws Exception {
System.out.println("addNumbers");
int numberA = 1;
int numberB = 2;
// Create a properties map to pass to the embeddable container:
Map<String, Object> properties = new HashMap<String, Object>();
// Use the MODULES property to specify the set of modules to be initialized,
// in this case a java.io.File
properties.put(EJBContainer.MODULES, new File("build/jar"));
// Create the container instance, passing it the properties map:
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer(properties);
// Create the instance using the container context to look up the bean
// in the directory that contains the built classes
MyBean instance = (MyBean) container.getContext().lookup("java:global/classes/MyBean");
int expResult = 3;
// Invoke the addNumbers method on the bean instance:
int result = instance.addNumbers(numberA, numberB);
assertEquals(expResult, result);
// Close the embeddable container:
container.close();
}

Right-click in the editor and choose Fix Imports (Alt-Shift-I; ⌘-Shift-I on Mac) to add import statements for java.util.HashMap and java.util.Map.

Run the test again to confirm that the modified test works and that the container is created correctly.

You can click the Rerun button in the Test Results window.

Using @BeforeClass and @AfterClass Annotations

In this exercise you will modify the test class to create individual methods for creating and shutting down the
container instance.
This can be useful when you want to run several tests that can use the same container instance.
In this way you do not need to open and close a container instance for each test,
and instead create one instance that is created before the tests are run and is then closed after all the tests are completed.

In this exercise you will move the code that creates the EJB container to the setUpClass method.
The setUpClass method is annotated with @BeforeClass
that is used to indicate a method that will be run first, before the other methods in the test class.
In this example, the container instance will be created before the testAddNumbers test method and
the container will exist until it is shut down.

Similarly, you will move the code that shuts down the container to the tearDownClass method that
is annotated with @AfterClass.

Add the following field to the test class.

private static EJBContainer container;

Copy the code that creates the container from the testAddNumbers test method to the setUpClass method
and

If you run the test again to confirm that the container is created and shut down correctly,
you will see output similar to the following in the Test Results window.

You can see that the setUpClass method ran before the addNumbers test and printed "Opening the container".

Testing an Entity Class

In this section you will create an entity class and persistence unit and modify the session bean to inject the entity manager and access the entities.
You will add a simple method to the new entity class that prints the id number of the entry to the output.
You will then add some simple methods to the session bean to create and verify entries in the database.

Creating the Entity Class

In this section you will use the New Entity Class wizard to create an entity class and persistence unit with the database connection details.

Right-click the WebAppJUnit project in the Projects window and choose New > Other.

Select Entity Class in the Persistence category. Click Next.

Type SimpleEntity as the Class Name.

Select bean from the Package dropdown list.

Type int as the Primary Key Type. Click Next.

Use the default Persistence Unit Name and Persistence Provider.

Select jdbc/sample as the data source and Drop and Create as the strategy. Click Finish.

When you click Finish, the new entity class opens in the editor.
If you expand the Configuration Files node in the Projects window, you can see that the
IDE automatically generated the persistence.xml file that defines the properties of the
persistence unit WebAppJUnitPU.

In the editor, add the following private field to the entity class.

private String name;

Right-click in the Source Editor and choose Insert Code (Alt-Insert; Ctrl-I on Mac) and
select Getter and Setter to open the Generate Getters and Setters dialog box.