The EJB Session Bean Unit Testing with JUnit
The EJB embedded container is introduced by the Glassfish Application Server version 3 and it is a core part for unit testing. It’s light weight and strong enough so that the EJB unit testing can be executed without the starting of Glassfish Application Server. For sure, you may imagine it can be applied for other purpose as same as the embedded database.
Via NetBean, you can create the JUnit test case and test suit simply by right clicking at the node named “Test Packages” of your EJB project and choose the one you want. The following is the simply step for initiating the EJB embedded container: –
Creating the JUnit test case or test suit
1. Declare and initiate the EJB embedded container.
private static EJBContainer ejbContainer = EJBContainer.createEJBContainer();
2. Declare and initiate the JNDI context for looking up the EJB.
private static Context context = ejbContainer.getContext();
3. Simply use the context object for looking up your EJB and another JavaEE resources via JNDI, for example, The connection pool, the EJB and so on.
4. The rest is creating your test respectively to you business, unit or others test case by following the JUnit standard.
1. The EJBContainer is javax.ejb.embeddable.EJBContainer
2. The Context is javax.naming.Context
Executing the test case with the EJB embedded container
1. The EJB application is not required to be deployed to the Glassfish Application Server
2. The Glassfish Application Server must not be started since the ports will be clashed since the embedded will share the same ports.
Just simply right click at your test case or test suit and choose “Run File”.