This tutorial covers creating an EJB 3.1 application that
demonstrates how to create and run JUnit tests in GlassFish
embedded EJB container within a Java SE environment.
Time to Complete
Approximately 45 minutes.
EJB 3.1 introduced the EJB 3.1 Embeddable API for executing EJB
components within a Java SE environment. The embedded container
allows use of the EJB programming model in desktop
applications. The EJB 3.1 Embeddable API is designed for unit
testing of EJB modules. This includes the support for running an
embedded EJB container within a Java SE environment, so that the
EJB can run without an Application Server.
The embedded container allows client code and its corresponding enterprise beans to run within the same JVM and class loader. This provides better support for testing and offline processing (e.g. batch).The client uses a spec-defined bootstrapping API to start the container and identify the set of enterprise bean components for execution. The embeddable EJB container provides a managed environment with support for the same basic services that exist within a Java EE runtime: injection, access to a component environment, container-managed transactions, etc. In general, enterprise bean components are unaware of the kind of managed environment in which they are running. This allows maximum reusability of enterprise components across a wide range of testing and deployment scenarios without significant rework.
In this tutorial, you will create a Java SE application with a stateless EJB session bean. Next you will create a JUnit test class to test the session bean and run the test in the GlassFish embedded EJB container.
The Hardware and Software Requirements
The following is a list of hardware and software requirements:
- Download and install Java JDK 7 from this link.
- Download and install NetBeans 7.2 with Java EE which
includes GlassFish 3.1.2 (Java EE download bundle) from this link.
During installation, be sure to check the box to install
GlassFish and JUnit.
Before starting this tutorial, you should:
- Have the software installed as listed under Hardware and Software Requirements section.
- Ensure NetBeans is running.
Creating a Java SE Application
To create a Java SE Application, perform the following
steps in the NetBeans IDE.
You should now have a Java SE Application
Setting the Project Properties
You need to add the GlassFish embedded EJB container
to the classpath of the
Adding the GlassFish embedded EJB Container
Creating a Session Bean
Create a session bean by performing the following steps.
Testing in the embedded EJB Container
To test the business methods of the session bean, you will use the new embeddable EJB Container API. You first create a JUnit test class, CalculatorBeanTest. You will start the emedded EJB Container in that class. Next, you will look up the CalculatorBean using JNDI.
Creating a JUnit Test
Create the JUnit test class by performing the following steps.
Modifying the JUnit Test
Perform the following steps to modify
Executing the JUnit Test
To execute the JUnit Test, perform the following steps in NetBeans IDE.
In this tutorial, you have learned how to:
- Create JUnit tests for EJB 3.1 applications in Java SE
- Execute the JUnit tests in GlassFish embedded server .
- JSR 318: Enterprise JavaBeans 3.1
EE 6 Tutorial
- Enterprise JavaBeans Technology
the Embedded EJB container to Test Enterprise Applications
- Lead Curriculum Developer: Anjana Shenoy
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.