Unit Testing EJBs

This tutorial provides a brief introduction to the AppPerfect Java Unit Test using a set of hands-on practice exercises. This tutorial assumes you have successfully downloaded and installed AppPerfect Unit Test on your machine with the default options. Apart from this pre-requisite, this tutorial is self contained. Instructions given below are Windows-specific; if you are using a non-Windows machine, please use equivalent commands/instructions.

This document is divided into following sections

  1. Creating Project
  2. AppPerfect Java Unit Test

Within each section, multiple exercises are listed. Each exercise forms a logical unit which is expected to take no more than a few minutes to perform.

All exercises assume you have installed the product in C:\AppPerfect\UnitTest folder and will be referred as UnitTest_HOME henceforth in tutorial. If you have installed the product in some other folder, modify the instructions below appropriately.

This tutorial is not intended as a comprehensive training guide. Please refer to the product documentation for detailed information. However, this tutorial will give you a very good overview of the products and substantially improve your productivity with the product.

Creating Project

Exercise 1: Launch AppPerfect Java Unit Test

  1. Click on Start -> Programs ->AppPerfect Unit Test x.x.x -> AppPerfect Java Unit Test
  2. On launching AppPerfect Java Unit Test a Welcome page will be displayed. Go through the brief description given for product.
    NB: Welcome page is displayed only when Unit Test x.x.x is launched and last time no project was opened.

Exercise 2: Creating a Project

  1. Launch the Project Wizard by clicking File ->New... menu option. The New Project wizard will be launched.
  2. Go through the instruction provided on top of the General tab.
  3. Keep the default project name and location for the purpose of this exercise. We don't have to provide "Notification" settings or "Remote Application/AppServer" settings for this exercise. Click on the Next button.
  4. In the Source tab provide the source files for the EJB project.
  5. Use the default JDK which is bundled with AppPerfect Java Unit Test and click on the Next button.
  6. In the "Environment" tab we will provide the classpath for the sources added in the project.
  7. Click on "Verify Classpath" button to validate the classpath.
  8. Classpath validation dialog will be launched and the classpath will be verified. A message saying that the classpath specified is correct should be displayed. Click on the "OK" button. Click on the Next button.
  9. In the "Target" tab select project type as "SERVER".
  10. Provide the proper URL for accessing the application. Select the "AppServers Settings" tab.
  11. Select the "Specify AppServer Settings" checkbox and specify the AppServer settings on which EJB application is deployed.
  12. De-select the "Launch server automatically (When required)" checkbox. Click on the Finish button.
  13. If applications asks for Starting URL validation, click on 'No' button. Normally Confirmation for Starting URL validation is not asked when "Launch server automatically (When required)" is checked. A confirmation message saying that the project is saved will be displayed. Click on the OK button.

Now using this Project we will create tests to demonstrate the functionalities of AppPerfect Unit Test product.

AppPerfect Java Unit Test

NB: Please follow the steps provided in the "Creating Project" section to first create a Project, then proceed further.

Exercise 1: Define a Unit Test project

  1. Once the Project is successfully created another dialog - Define Project Properties dialog - will be displayed.
  2. Read the instructions at top of each tab.
  3. In the General tab select Application type as "Simple". Please de-select checkboxes for Generate test cases for JSP and Struts Action Classes. Please select checkbox for Generate test cases for Java and EJB files. Provide EJB settings required for unit testing of EJB classes.
  4. The Test Case Generation tab allows you to specify Test Class Generation settings, you can select the various TestCase options. Keep default values.
  5. In Test Case Execution tab you can specify Test Class Execution settings. Keep default values.
  6. Click on the OK button to close this dialog.

Exercise 2: Test case Generation

  1. Once a project is defined, the next step is to generate the necessary unit test cases. AppPerfect Unit Tester automates the generation of such unit test cases. For .java files, the JUnit framework is used. For .jsp files, the HtmlUnit framework is used. Launch Test Generation Dialog by clicking on Project -> Generate Test Cases... menu option.
  2. Click on EJB Mapping tab and provide required JNDI names for the EJBs.
  3. Click on Test Classes tab.
  4. In 'Generating test classes' section AppPerfect will generate test classes for source files provided (which were added in the source tab of Project Wizard) and add instances created to Object Repository. You can see the number Source files, Test classes generated, Test cases generated and Files ignored etc. Click on Start Generation button.
  5. A check mark will be displayed against the files for which test classes are generated. Click OK once generation is done.
  6. Select the Test Classes tab on the l.h.s. and expand the EJB node and Source Files node to view the test classes generated for the EJB and JAVA files.
  7. Select a Test class to view its details on the r.h.s panel.
  8. Right click on a test class and select "Open with Java Editor" to view/edit its source code.
  9. The source will be displayed in the editor on the r.h.s. For this exercise, we will not modify the test class.

Exercise 3: Execute the unit test and view/analyze results

  1. Start unit testing by clicking on Project -> Run.
  2. Select the Execution Summary node in the Results tab to get an overview of the test result.
  3. Select the Test Class node. On the r.h.s select Execution Results and expand the Source Classes and Server Pages node and select each java / jsp file to view its test result.
  4. A list of all the exceptions / assertions will be displayed under the Exceptions view on the r.h.s. Select individual exception / assertion and click "View stack trace" to view the error details. Rectify the error and repeat the test as needed.