Wednesday, April 5, 2006

Running JUnit on J2EE using Application Client

Step 1: Create an application client project:

* From the J2EE Perspective, right click Application Client Projects > New > Application Client Project
* Click Show Advanced and fill in the fields accordingly
* Uncheck Create a default Main class
* Click Finish
* An error will appear in the task view which can be ignored for now

Step 2: Configure resources for the application client:

* Open the application client deployment descriptor (application-client.xml)
* Switch to the References tab
* Add resource references such as data sources, or other server resources

Step 3: Set the build path:

* Right click the newly created project > Properties > Java Build Path
* Switch to the Libraries tab
* Click Add Variable
* Select the JUNIT_HOME variable and click Extend
* Select junit.jar and Click OK

Step 4: Add your JUnit Test Case:

a) Create a new JUnit class
* Create a new JUnit test by going to File > New > Other > Java > JUnit > JUnit Test Case
* Fill in the necessary fields and select the checkbox to create the main method as well as the TestRunner statement
* Click Next
* Select methods to test and click Finish

b) Use an existing JUnit class

* Import your existing JUnit class into the appClientModule directory
* Add a main method with the line# junit.textui.TestRunner.run(.class);

Step 5: Update the Deployment Descriptor

* Open the Deployment Descriptor on your (appClient) project
* Under Main Class click Edit
* On the Main Class section click Browse and locate your JUnit class with the main method
* Save and close the editor
* The previous error should now be gone

Step 6: Create a new runtime configuration:

* Go to: Run > Run
* Select WebSphere vX.x Application Client (X.x referring to the version of WAS being used i.e. 6.0). This maybe different on other WAS Version
* Click Ok
* Ensure that the Enterprise application and Application client module fields are set correctly
* Click the Classpath tab menu
* Click Advanced...
* Select Add Variables and click OK
* Repeat the instructions from step 3 to add junit.jar
* Click Apply to save the changes
* Click Run to launch the application client