Working With Testing Tools
Overview
Testing tools are used in IDEs to create and run tests. The different types of testing frameworks are:
- White box or Unit testing:
- Code logic Unit testing: Runs tests on a mock objects type framework
- Integration Unit testing: Interacts with the container to run tests
- Functional Unit testing: Tests the values returned from a server code. Enables testing full use-cases such as, a login use-case which comprises of several requests or responses
- Black box or Functionality testing
- System Integration testing
Unit Testing
Developers write unit tests to check a program for expected behavior. However, unit testing is not weaved into the development process. This prevents developers from measuring the progress of development. If a program feature lacks an automated unit test, it can be assumed that it does not work. The tests should demonstrate that the program works and:
- Tests retain their value over time
- Someone other than the original author is able to execute the tests and interpret the results
- Tests from various authors can be combined and run without any interference
- Existing tests can be leveraged to create new ones
Functionality Testing
Functionality or black box testing is generally performed within other test scripts to ensure that a product is performing within acceptable parameters. The major components of the product being tested are subjected to a work load and it is verified that any output that results from these tests is in agreement with the expected output if the product were working properly. A `pass' for each aspect of the series of tests indicates that the device functions as expected under heavy utilization. A `fail' indicates that the device could not serve a particular request.
Testing Tools in Studio
The testing tools provided along with Studio enables you to measure your progress, spot unintended side effects, and focus your development efforts. Studio features:
- JSP Test tool: Functionality testing for web
- JUnit: For unit testing Java classes
- Jakarta Cactus: For unit testing server-side components such as, JSP, EJB, filter, servlets and tag libs
Studio simplifies the usage of these tools by eliminating the need to compile or run the test at the command prompt. This chapter describes how to use these tools.
What is JSP Test Tool
Studio features the JSP Test Tool for performing functionality tests. This tool tests web components such as HTMLs and JSPs. It can not be used to test Swing components. This is a capture replay tool, designed to exercise every operation that can be performed on that server, record the testcases, and display the results.
Recording in JSP Test Tool
The JSP Test Tool keeps a record of the series of actions performed as a part of a test case and compares the results when the test case is replayed at a later date. For example, the tool receives Req1 and sends it to the server. The response from the server is recorded and sent to the client.
If the same test case is run later, the tool checks the size of Res2 against the record of Res1. If the sizes differ, the result is marked as a failure.
JSP Test GLobal Settings
The JSP Test Global Settings panel enables the user to configure Server, Filter, and Error settings. The proxy server settings applied prior to the test, appear in the respective fields. To view the panel:
Server Settings
Table 1: Fields available in the Global Settings panel
|
Attribute
|
Description
|
|
Sever IP
|
Specifies the IP address of the server that hosts the test case
|
|
Server Port
|
Specifies the port at which the server receives the request
|
|
Capture Port
|
Specifies the port at which the JSP Test Tool runs
|
Filter Settings
If the user does not want the tool to record and compare graphical elements for a testcase, the file extensions such as *.gif, *.jpj, and *.bmp can be provided in the `Hide File with the following extensions'. To skip recording test conditions for certain elements, the extensions can be entered in the check errors for files with the following extensions text box can be used. These settings are optional.
Error Settings
While running a test, the tool verifies if the response received is the expected response. To display errors in the test case interface, the user can select from the following:
- Response header is not 200 OK: 200 OK implies that the request has succeeded. The information returned with the response depends on the method used in the request such as GET/POST.
- Response size is different from recorded size for static files.
Using JSP Test Tool
To understand recording in JSP Test Tool, consider the following example:
- Open the sample BankDesk. Start the Server if it is not running already.
- Right click on the desk and select Deploy.
- Right click on BankDesk in the explore panel and select New Folder > JSP Test Module.
- Type name of the test module as Bank_test in the panel. The location of the test module is the desk, by default. Use the browse button to target another folder.
- Right click on the test module and select New Test. Type a Test Name as test1. Test Module is selected as Bank_test by default. This field is non-editable. Description is optional. Click OK.
- Click Settings in the Record panel.
|
Attribute
|
Description
|
|
Start
|
Starts the process of recording requests
|
|
Stop
|
Stops the process of sending requests. Can be used when a wrong operation is performed
|
|
Pause
|
Pauses in the middle of recording
|
|
Close
|
Closes the panel
|
|
Settings
|
Opens the Settings panel
|
|
Tests
|
Provides information on the test name, total number of requests sent, number of requests served successfully and number of requests not found
|
|
Requests
|
Provides the following informations:
i) Get/Post: Type of request sent to the server.
ii) Request: Lists the exact HTTP request made to the server.
iii) Passed: Shows if the request has passed or failed during a replay.
|
- Use the Global Settings option which is selected by default. Select Customize Settings to apply different settings.
- Click Start button. Enter the initial URL as http://localhost:8181/bankWeb/bank.jsp.
- Perform some action on the JSP such as, creating a new account or transfer of money. The Record panel records details.
- Right click on the test in the explore panel and select replay. All the actions are performed again. The new HTTP requests are compared to the corresponding request recorded.
Note: If the requests match the record, a tick appears in the Record panel. If the string specified in `Page Error Info' panel is found, the test fails and a cross appears. Click on cross or tick to view details.
- Click any HTTP request listed in the record panel to view JSP details.
Table 2:
|
Attribute
|
Description
|
|
Base
|
Displays the HTML page code recorded earlier
|
|
Current
|
Displays the HTML code for the current page
|
|
HTML
|
Displays the HTML page
|
- Double click on request to view the Request header panel.
Table 3:
|
Attribute
|
Description
|
|
Request Parameters
|
Lists the information to be posted through that JSP and the related values. Enables to change the request parameters.
|
|
Request Headers
|
Provides information on type of image accepted, Referer, Accept-language, Proxy-Connection, User Agent, Host, Cookie
|
|
Page Error Info
|
Enables user to search for string literals in the HTML response.
|
Specify the string to be searched for in the `Page Error Info' panel. For example, when it is specified that the response is an error if the specified string is found, the JSP Test Tool searches for the string in each JSP during a replay.
What is JUnit
JUnit is an open source Java testing framework used to write and run repeatable tests. It has the following features:
- Assertions to test expected results
- Test fixtures to share common test data
- Test suites to organize and run tests
- Graphical and textual test runners
Read the section `Writing and running a JUnit test' that gives an example to explain how to use JUnit in Studio. To write and test a code, you should be familiar with the concept of a testcase, fixture, suite and test runner. These are briefly explained here.
For more information on JUnit, visit http://www.junit.org/news/article/index.htm.
Key terms
Simple Test Case
A test case is an object to test the basic concept in the code. It can be one of the following:
- A print statement
- A debugger expression
- A test script
Test Fixture
A test fixture is a set of common initializer of objects based on which two or more tests operate. To write a test fixture:
- Create a subclass of TestCase
- Add an instance variable for each part of the fixture
- Override setUp() to initialize the variables
- Override tearDown() to release any permanent resources allocated in setUp
Test Case
A test case extends the class Assert and implements the test.
In JUnit, to write a test against a fixture:
- Write the test case method in the fixture class. Make it public.
- Create an instance of the test case class and pass the name of the testcase() method to the constructor.
When the test is run, the name of the test is used to look up the method to run. When there are several tests, they can be organized into a Suite.
Suite
A TestSuite is an object that runs any number of test cases simultaneously.
Test Runner
Test Runner tool provided with JUnit defines the suite to be run and displays its results. A static method suite that returns a test suite, makes the suite accessible to the Test Runner.
Writing and running a test using JUnit
Before running a test using JUnit, download JUnit JAR, Junit3.8.1.zip from
http://www.junit.org/index.htm. To configure JUnit in Studio:
- Select File > IDE Options > Testing > JUnit. Specify the JUnit JAR location in the field.
- Select a test runner to view the output. The Text Based test runner opens a new tab in the output panel and displays the result in a text format. The SwingUI Runner opens a new panel and displays results in the dialog.
Creating and running a test
- Create a new blank desk, TestDesk. Create a Java module, Test and a Java file, com.Test1.
- Add one method to com.Test1. For example,
- In the explore panel, right click on the Java file and select Generate JUnit Test Case.
- Retain the default class name, com.TestTest1 that appears in the JUnit Test Case panel. Select the methods setUp() and addNos and click OK.
- Give an appropriate argument in the test case. For example,
- In the explore panel, right click on the test case and select Run JUnit Test. The results are displayed in the swingUI test runner, JUnit panel.
Viewing a test failure
Consider the following example of test failure:
Use an assertion in the test case as,
Run the Test. The result will appear as a failure. Details are also provided
What is Jakarta Cactus
Cactus is a test framework for unit testing server-side Java code for servlets, EJBs, Tag Libs, filters and JSPs. It extends JUnit and provides several redirectors that create proxy instances of the test on the server-side.
Studio simplifies working with Cactus by eliminating the following manual steps:
- Creating an instance of the test on the server side. Copies the testcase to the correct location on the server
- Making an entry for the Cactus servlet re directories and makes all Cactus related changes in the web.xml
For more information on Jakarta Cactus, read the Jakarta Cactus documentation.
Writing and running a test Using Cactus
Before writing a test, download the Cactus JARs. For more information, visit
http://jakarta.apache.org/cactus/getting_started.html. Configure Cactus in Studio as follows:
- Select File > IDE Options > Testing > Cactus from the main menu.
- In the dialog, provide the location the Cactus JAR files.
- Select the Browser Based test runner and save settings.
Note: The swingUI and text based test runners will be made available at a later date.
For this example, consider the desk, TestDesk created for JUnit test.
Using Cactus to run a EJB test case
- Create a new EJB module, EJBMod, a web module, WebMod, and a Java module JavaMod.
- Create stateless session bean, TestBean.
- Write a business method addNos and compile the file. Check in the remote interface, the business method should show there. If not, add the method manually.
- In the explore panel, right click on TestBean and select Generate Cactus Test Cases.
- Name the test case as com.pramati.TestCase2. Select the source module as Java module. Select setup() and lookupcode() of framework methods, and business method addNos. Select all begin() an end() methods and click OK. When the test is run, the sequence of execution is, begin(), addNos method, and end ().
Note: If for a test case, some external parameters are required, the parameters can be initialized in begin (). For more information on begin and end methods, visit http://jakarta.apache.org/cactus/getting_started.html.
- Give appropriate argument and compile the test case. For example,
- In the explore panel, right click on the test case and select Run Cactus Test. If Server is not already started, it starts on selecting Run Cactus Test and deploys the EJB module and web module.
The result is displayed in a browser in XML format.
To obtain the result in HTML format, copy the Cactus XLS file in the web module and run the test again. For more information on this, read instructions in http://jakarta.apache.org/cactus/integration/integration_browser.html.