For EJB3 testing, an interesting solution based on JBoss embedded package. Of course other alternative are available (Cactus, EJB3Unit...).
Thanks to this solution, it is now possible to launch an embedded JBoss container in standalone (without the need to install and configure a full JBoss container).
This solution is very interesting for testing purpose because there is no need to deploy and un-deploy each time on the server.
This JBoss package includes all mandatory libraries used to validated an EJB3 solution:
- 1 EJB3 container
- The EJB3 Hibernate implementation
- 1 embedded java database: HSQLDB
- JUnit (3) for testing purpose
For this tutorial, the embedded-jboss-beta3 is used and can be downloaded here
All the tutorial sources is available here.
Tutorial objective: Testing an EJB3 solution with JUnit based on
- 1 entity bean (Person)
- 1 session bean used for CRUD operation (Create, Remove, Update, Display)
- 1 JUnit test case used to validate session bean operation
Step 1: Download the embedded JBoss library
Step 2: Create a startup project
Create a project with your favorite IDE and import the JBoss embedded library (from the library and from the optional-lib directories). Do not forget to add all the .jar files in the classpath. Embedded configuration directory needs to be in the classpath also (/embedded-jboss/bootstrap).
Step 3: Create your Entity bean
For this tutorial, the Person class is created
Step 4: Create your session bean
For this tutorial, the PersonService session bean (interface and local implementation)
Step 5: Create your JUnit test case
It's the most interesting part of the tutorial.
To create and deploy JUnit tests easily, JBoss provides an extension of the JUnit TestCase class, the BaseTestCase. This class needs to package the application hosting the testes. A dynamical jar builder is used to create the package.
Step 6: Build your applications and tests with ant
Ant 1.7 is used for this tutorial.
Step 7: Run the tests
The tests ant task is used. This task will execute all tests available in the test directory.
An other ant task tests-report-html can be used to generate the report in html format.
Step 8 (optional): Running multiple tests
To avoid deploying and starting the JBoss container for each test case, it's possible to group all test cases in a suite by extending the JUnit TestSuite class.
For the tutorial, the AllTests class has been used and 2 test cases has been added in the suite. The all-tests task is used to run the TestSuite.
Embedded JBoss seems to be an attractive solution for EJB3 testing with JUnit. For now, only the internal java DB is used for persistence but it seems possible to configure an other database like Mysql. I will try to cover this feature in a new post.
The JUnit4 framework has not been tested and can be evaluated in an other post.