Junit Testing in Jdeveloper

Embed Size (px)

Citation preview

  • 7/31/2019 Junit Testing in Jdeveloper

    1/13

    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 extremepace 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 forbusiness 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 HelpCheck for Updates.

    2) In that check Official Oracle extension and Updates. Click Next.

  • 7/31/2019 Junit Testing in Jdeveloper

    2/13

    3) In the Step 2, check the available updates for JUnit and click Next.

    4) Agree to the license agreement and click Next

  • 7/31/2019 Junit Testing in Jdeveloper

    3/13

    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.

  • 7/31/2019 Junit Testing in Jdeveloper

    4/13

    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 FileNew to open the New Gallery. In the Categories tree, expand General and

    select Unit Tests (JUnit). Double click Test Case.

  • 7/31/2019 Junit Testing in Jdeveloper

    5/13

    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.

  • 7/31/2019 Junit Testing in Jdeveloper

    6/13

    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.

  • 7/31/2019 Junit Testing in Jdeveloper

    7/13

    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 TestCase and click OK.

  • 7/31/2019 Junit Testing in Jdeveloper

    8/13

    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 testsare shown in a list at the bottom. Successful tests are shown with a green bar.

  • 7/31/2019 Junit Testing in Jdeveloper

    9/13

    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 createcustom 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 canbe overridden to clean up the test fixture at the end of a test.

    Step 10:

    In the navigator, select the project. Choose FileNew to open the New Gallery. In the

    Categories tree, expand General and select Unit Tests (JUnit). In the Items list, double-

    click Test Fixture.

    Step 11:

    Complete the details in the wizard and click OK.

  • 7/31/2019 Junit Testing in Jdeveloper

    10/13

    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 JUnitTestRunner 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 FileNew to open the New Gallery. In the

    Categories tree, expand General and select Unit Tests (JUnit). In the Items list, double-click Test Suite.

  • 7/31/2019 Junit Testing in Jdeveloper

    11/13

    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.

  • 7/31/2019 Junit Testing in Jdeveloper

    12/13

    Step 15:

    Select the test cases to be inserted and click on Finish. To make the JUnit test classesaccessible 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 thismethod 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.

  • 7/31/2019 Junit Testing in Jdeveloper

    13/13

    Step 18:

    The Test Suite is successfully executed.

    Conclusion:

    Thus test cases have been executed in Junit using Jdeveloper.