 |
AppPerfect Unit Tester |
Tutorial : Unit Testing Struts Action Classes
This tutorial provides a brief introduction to the Unit Tester product of the
AppPerfect DevTest4J using a set practice exercises. This tutorial assumes you have
successfully downloaded and installed AppPerfect DevTest4J 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
- Creating Common Project
- AppPerfect Unit Tester
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
use the PetStore demo application running inside Apache Tomcat. The modified PetStore application
and Tomcat are both bundled with this product.
All exercises assume you have installed the product in C:\AppPerfect\DevTest folder and will
be referred as DEVTEST_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 Common Project
In AppPerfect DevTest4J, you can create a common project and use the same project to perform tests in all
the bundled products.
Exercise 1: Launch AppPerfect DevTest4J
Action:
- Click on Start -> Programs ->AppPerfect DevTest4J x.x.x -> AppPerfect DevTest4J
- On launching AppPerfect DevTest4J a Welcome page will be displayed. Go through the brief description
given for each product and click on the product icon to view its perspective.
NB: Welcome page is displayed only when DevTest4J x.x.x is launched and last time no project
was opened.
- To switch between different product perspectives click on corresponding project button in toolsbar.
Exercise 2: Creating a Common Project
Action:
- Launch the Common Project Wizard by clicking File ->New... menu option.
The New Project wizard will be launched.
- Go through the instruction provided on top of the General tab.
- 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.
- In the Source tab click on the "Add File" button on the r.h.s., select "Files of type:" as *.jsp in the
Open dialog and add jsp files from the location "DEVTEST_HOME\tomcat\webapps\petstore"
- Now click on the "Add Folder" button on the r.h.s. and select the folder "DEVTEST_HOME\tomcat\webapps\petstore\src".
Click the Next button.
- Use the default JDK which is bundled with AppPerfect DevTest4J and click on the Next button.
- In the "Environment" tab we will provide the classpath for the sources added in the project.
- Click on the "Add File" button on the r.h.s. and navigate to the location -
"DEVTEST_HOME\tomcat\webapps\petstore\WEB-INF\lib" - select all the jars present in the "lib" folder.
- Click on "Verify Classpath" button to validate the classpath.
- 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.
- In the "Target" tab select project type as "SERVER".
- Keep the default URL in the "Starting URL" field i.e., "http://localhost:8080/petstore".
Select the "AppServers Settings" tab.
- Select the "Specify AppServer Settings" checkbox and select "Tomcat_5.x" from the drop down menu.
NB: This is required only if you want to launch AppServer automatically while
profiling, recording and replaying in AppPerfect Unit Tester or if you want to
profile AppServer in AppPerfect Java Profiler.
- Specify the Server Home path - "DEVTEST_HOME\tomcat" and the "Startup file" for the server -
"DEVTEST_HOME\tomcat\bin\catalina.bat".
- Select the "Launch server automatically (When required)" checkbox. Click on the Finish button.
- 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 common project we will create tests to demonstrate the functionalities of AppPerfect
Unit Tester product.
AppPerfect Unit Tester
NB:Please follow the steps provided in the "Creating Common Project" section to first create a common project,
then proceed further.
Exercise 1: Define a Unit Tester project
Action:
- Once the common project is successfully created another dialog - Define Project Properties dialog -
will be displayed.
- Read the instructions at top of each tab.
- In the General tab select Application type as "Struts based" and specify following path -
"DEVTEST_HOME\tomcat\webapps\petstore" as Document Root folder. Please select checkboxes for
Generate test cases for Java and JSP files. Please select checkbox for Generate test cases Struts
Action Classes. Please de-select checkbox for Generate test cases for EJBs.
- The Test Case Generation tab allows you to specify Test Class Generation settings, you can select the
various TestCase options. Keep default values.
- In Test Case Execution tab you can specify Test Class Execution settings. Keep default values.
- Click on the OK button to close this dialog.
Exercise 2: Testcase Generation
Action:
- 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 HttpUnit framework is used. Launch Test Generation Dialog by clicking on Project ->
Generate Test Cases... menu option.
- Now start the configured server using Start Server button, AppPerfect will start the server using the
startup script file, so that it can create test classes for JSP / HTML pages using the browser recording
facility available in the product. In this case AppPerfect will start Tomact5.x server.
- All .jsp files selected for unit testing are listed, map your .jsp file to the equivalent action file.
In our case, we need to specify "selection.do", "mainscreen.do", "order.do" as the mapped action file.
Once server starts, click on the Start Recording button.
- An instance of Microsoft Internet Explorer will be launched with the PetStore home page loaded.
Click on enter the store link. This will populate the database and display the PetStore main page.
Note that populating the database might take some time.
- Click on each link on the left: Birds, Reptiles, Dogs, Cats, Fish
- Order an item by clicking on: Birds -> Amazon Parrot -> Adult Male Amazon Parrot -> Add to cart
- Modify the quantity and click on Update Cart button.
- Check out by clicking on the Check Out link.
- At the 'Sign in' page, default values are already provided. Click on Submit.
- Sign out of PetStore by clicking on Sign-out at top right of screen.
- Close browser by clicking on File -> Close in the browser window.
- Click on the Stop Recording button to stop the recording. To Stop server click on Stop Server button.
After server is stopped, click on Test Classes tab.
- In 'Generating test classes' section AppPerfect will generate test classes for .java, .jsp files
(which were added in the source tab of Common Project Wizard) and add instances created to Object Repository.
You can see the number Source files, Test classes generated, Testcases generated and Files ignored etc.
Click on Start Generation button.
- A check mark will be displayed against the files for which test classes are generated. Click OK once
generation is done.
- Select the Test Classes tab on the l.h.s. and expand the Server Pages node and Source Files node to view
the testclasses generated for the JSP & JAVA files.
- Select a Testclass to view its details on the r.h.s panel.
- Right click on a testclass and select "Open with Java Editor" to view/edit its source code.
- The source will be displayed in the editor on the r.h.s. For this exercise, we will not modify
the testclass.
Exercise 3: Execute the unit test and view/analyze results
Action:
- Start unit testing by clicking on Project -> Run.
- Tomcat server will be launched using the modified startup file viz., catalina_AppPerfect.bat.
The server startup status will be displayed in the Server Console.
- The testcase class is executed by sending multiple requests to the server using combination of parameter
values as defined in the testcase.
- If error code is detected in any of the responses (http code 400 and above), it is displayed as a failed
test case in the Results tab.
- Select the Execution Summary node in the Results tab to get an overview of the test result.
- 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.
- 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.