Tuesday, April 27, 2010

EJB Unit testing with OpenEJB 3.1.2 and Netbeans 6.8 (again without Maven)

I recently became interested in researching the possibility of unit-testing my Enterprise JavaBeans (EJBs) 3.0. This led me to look at OpenEJB. Upon my journey through the OpenEJB documentation, I found that there were many examples at the OpenEJB Download page. However these examples are based on the Maven way of doing things and although it was possible to run them in Eclipse it wasn't so easy to do the same in Netbeans as I was offline when I was trying the examples (quite frankly my Maven skills is down there with my graphics skills (that is, almost non-existent)).
After reading through the simple-stateless unit test example I decided to try and run it in my Netbeans 6.8, it went well so I decided to document it for others who might be interested. Btw, my operating system (OS) is Windows Vista but I believe this should work on any OS that supports Java.

The first steps are (duh):
a) Download and Install Netbeans 6.8
b) Download the OpenEJB Standalone Server (openejb-3.1.2.zip) and the OpenEJB Examples (openejb-examples-3.1.2.zip) from the OpenEJB website.

If you have done that launch Netbean 6.8 and
a) Choose New Project... from the menu.
b) Choose Java from the Categories list and select Java Application from the Projects list. Click on the Next button
c) Enter simple-stateless as the Project Name and click the Finish button
d) In the Projects explorer to your left, right-click on the Source Packages and choose New->Java Package... (if you don't see the Java Package... option choose Other... and look under the Java Categories list)
e) Enter org.superbiz.calculator as the Package Name and click Finish
f) Again, right-click on the Test Packages and choose New->Java Package...
g) Enter org.superbiz.calculator as the Package Name and click Finish
h) Again, right-click on the Source Packages and choose New->Folder (if you don't see the Folder option choose Other... and look under the Other Categories list)
i) Enter META-INF as the Folder Name and click Finish.
j) Unzip the OpenEJB Examples zip file that you downloaded and look inside the directory. You will see a directory called simple-stateless. Enter that directory and navigate to src->main->java->org->superbiz->calculator. Select all the Java files (namely: CalculatorImpl.java, CalculatorLocal.java and CalculatorRemote.java), copy (Ctrl+C) and paste it into Netbeans by first selecting the org.superbiz.calculator package of the Source Packages that you created, right-click and choose Paste (Ctrl+V)
k) Return to the simple-stateless directory and navigate to src->main->resources->META-INF. Copy the ejb-jar.xml file and paste it into Netbeans in the META-INF folder you created earlier.
l) Return again to the simple-stateless directory and navigate to src->test->java->org->superbiz->calculator. Copy the CalculatorTest.java file and paste it into Netbeans by first selecting the org.superbiz.calculator package of the Test Packages that you created, right-click and choose Paste.
m) Unzip the OpenEJB Statndalone Server that you downloaded. Go back to Netbeans and select Libraries from the Projects explorer. Right-click and choose Add Library...
n) Select Create... from the Add Library dialog box that appears. Enter OpenEJB as the Library Name in the Create New Library dialog box and click Ok.
o) Ensure that the Classpath tab is selected from the Customize Library dialog and click Add JAR/Folder. Navigate to the unzip OpenEJB Standalone Server and open the lib directory. Choose all the JAR files in the lib directory and click the Add JAR/Folder button of the Browse JAR/Folder file chooser.
p) Click Ok to close the Customize Library dialog box.
q) Make sure the OpenEJB library is selected from the Available Libraries of the Add Library dialog and click the Add Library button


That is all that is required. Now all you have to do is choose the CalculatorTest.java from the Test Packages, right-click and choose Run File (Shift+F6) and watch your test fail spectaculously. Cool isn't it.
Now right-click on the simple-stateless project and choose Clean and Build. When this is done, choose the CalculatorTest.java file and Run File again.
Now the unit test should pass.

Hope this wasn't too long. As I learn more I will definitely post it. Thanks

No comments:

Post a Comment