Testing EJB 3.0 applications
After you have created a EJB 3.0 applications you can create a Servlet or JSF to test the EJB 3.0 applications.
Testing EJB 3.0 applications with a servlet
To test an EJB application with a servlet, first inject an EJB reference using the ‘@EJB’ injection annotation from EJB 3.0. Once you have injected the EJB, you can call the methods that are available from the Remote or Local interface.
The following snippet is taken from the EJB 3.0 Counter sample. You’ll see the statelessCounter field is declared with the type being LocalCounter which is our local interface for the EJB. The @EJB annotation in front of it injects an instance of it into our servlet.
- Create a servlet in a Web project using the Servlet wizard. File -> New -> Other…. Choose the ‘Servlet’ wizard.
- Fill in the package and class name fields for your servlet.
- After the servlet has been created and the Java Editor opened on the servlet class, insert the @EJB annotation tag along with the reference to the Local or remote interface class as a new field in the servlet.
- You can now invoke any of the methods in the local or remote interface from within the doPost() or doGet() methods of your servlet.
// Use injection to get the ejb @EJB private LocalCounter statelessCounter;
Testing EJB 3.0 applications with a JSF file
This kind of testing currently requires some manual configuration steps. For an example of the code involved import the EJB 3.0 Counter sample from the samples gallery and locate the EJBCounter.jsf file in the WebContent folder as well as the page code Java classes in the Java sources in the EJBCounterJSF project.
Related concepts
Developing EJB 3.0 Applications
Differences between EJB 3.0 and EJB 2.1
Securing enterprise applications
Deploying EJB 3.0 applications
Related tasks