Testing using Junit in Jdeveloper:

advertisement
Junit testing in Jdeveloper:
By Arthi Subramanian – Fall 2008
Introduction:
JUnit is an open source regression testing framework for Java which is used to write and
run tests that verify Java code. Tests written in JUnit help you write code at an extreme
pace and spot defects quickly.
JDeveloper's JUnit wizards can be used to create test fixtures, cases, and suites. In
addition to wizards for creating test components for generic projects, specialized wizards
for business components projects are also provided.
Installation:
1) JUnit is an optional feature that is not distributed with JDeveloper. In order to
download and install choose the HelpCheck for Updates.
2) In that check “Official Oracle extension and Updates”. Click Next.
3) In the Step 2, check the available updates for JUnit and click Next.
4) Agree to the license agreement and click Next
5) Enter the Oracle user name and password and click OK.
6) The updates will be downloaded and click on Finish. Restart Jdeveloper for the
updates to be installed.
Creating a Junit Test Case:
Step 1:
Open the project to be tested in Jdeveloper. In this case a small java program to obtain the
details of books in a catalog is presented. In the navigator, select the project or the
particular class to be tested.
Step 2:
Choose FileNew to open the New Gallery. In the Categories tree, expand General and
select Unit Tests (JUnit). Double click Test Case.
Step 3:
A Create Test Case Wizard appears. In the wizard select the class and corresponding
methods to be tested. Click Next.
Step 4:
Enter the values in the fields name, package and extends fields. The package name in this
case can be different from the one used in the java program. Click Next.
Step 5:
Click Finish. Once this is done the CatalogAppTest.java will be added to the application
navigator.
Step 6:
Thus the test case has been created for the Java program. Open the CatalogAppTest.java
file and include test values to check the functioning of the code.
Running the Test Case:
Step 7:
First run the java program and ensure it does not display any errors. Right click on the
program in the application navigator and click Run.
Step 8:
Right click the test case (named as CatalogAppTest.java in this case) and click on run. A
message box will be displayed asking for the way to start the target. Choose as a Junit
Test Case and click OK.
Step 9:
While the test is run JUnit shows its progress with a progress bar below the input field.
The bar is initially green but turns into red as soon as there is an unsuccessful test. Failed
tests are shown in a list at the bottom. Successful tests are shown with a green bar.
Creating a Custom Text Fixture:
A test fixture is a set of objects, having known values that provide data for the test cases.
Any class can serve as a test fixture, but JDeveloper provides wizards to help you create
custom test fixture classes and various specialized test fixture classes. With Junit the
fixture's objects are stored in instance variables of the test case subclass and initialized by
overridding the setUp method. The symmetric operation to setUp is tearDown which can
be overridden to clean up the test fixture at the end of a test.
Step 10:
In the navigator, select the project. Choose FileNew to open the New Gallery. In the
Categories tree, expand General and select Unit Tests (JUnit). In the Items list, doubleclick Test Fixture.
Step 11:
Complete the details in the wizard and click OK.
When multiple test cases need to be run:
Step 12:
When a group of test cases are written, a test suite needs to be used. In Jdeveloper they
are especially convenient to use. The JUnit Test Suite wizard has options to insert a
main() method and a call to a TestRunner class. Within JDeveloper, this will open the
JUnit TestRunner log window to display the test results. Edit the method if you wish to
use a different test runner.
Step13:
In the navigator, select the project. Choose FileNew to open the New Gallery. In the
Categories tree, expand General and select Unit Tests (JUnit). In the Items list, doubleclick Test Suite.
Step 14:
Enter the name, package details for the test suite and check the “generate these message
stubs” check box. This will include the public static void main class.
Step 15:
Select the test cases to be inserted and click on Finish. To make the JUnit test classes
accessible to a TestRunner designed to work with earlier versions of JUnit, declare a
static method suite that returns a test as follows:
public static Test suite() {
return new TestSuite(CatalogAppTest.class);}
Step 16:
The suite() method creates a TestSuite instance and adds the test cases to it. Edit this
method if you wish to add or remove test cases.
Include the junit.textui.TestRunner.run()to
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
Step 17:
Now that we've written a test suite containing a collection of test cases and other test
suites, we can run either the test suite or any of its test cases individually. Running a
TestSuite will automatically run all of its subordinate Test Case instances and
TestSuite instances. Select the Test suite from the application navigator and click on
Run.
Step 18:
The Test Suite is successfully executed.
Conclusion:
Thus test cases have been executed in Junit using Jdeveloper.
Download