FeaturesPluginsDocs & SupportCommunityPartners

XTest Unit Tests Tutorial

Version:1.0
Authors:
Daniel Pfeffer
Vitezslav Stejskal

Abstract: This is the tutorial of XTest Unit tests support framework.

Document History:
14 Mar 2001 : Daniel Pfeffer, version 0.1, status: draft, created
20 Mar 2001 : Daniel Pfeffer, version 0.2, updated
11 May 2001 : Vitezslav Stejskal, version 1.0, updated
11 Oct 2001: Martin Brehovsky, version 1.1, updated accomodate recent changes in XTest
Contents:
Introduction
Prerequisites
Lessons

Introduction

Unit tests are are checking code unit (classes) functionality according system design specification. The test application can be used for functionality, validation and compatibility tests. This document describes steps how to build and run unit test using XTest unit tests support framework. This document contains for each step "Lab" lessons describing in details steps to get first "hands on" Xtest based unit tests experiences.

Prerequisites

The JUnit module and XTest needs to be installed for developing and running XTest unit tests. If you intend to use NetBeans extension to JUnit (preffered when running in XTest harness), you will need also to install this extension (nbjunit.jar, available with XTest harness).

Lessons

Assuming some existing module to be tested, the test development process can be split in following steps:

  • Lab 1 - create the code we will test
  • Lab 2 - create selected class or package test application skeleton
  • Lab 3 - fill in the test code in test application
  • Lab 4 - run (debug) the test application within IDE with test results reported in output window

Lab 1 - Create tested product

Let's create some classes for illustration ...

  1. Choose the root directory of your imaginary product source code base (e.g. c:\myproduct).
  2. Create 'mymodule' sub-directory of your product code base, where we will place source codes of your module (the code we will test) and its unit tests.
  3. Create following structure of sub-directories in module's directory.
    c:\myproduct\mymodule ...

    ...\src
    ...\test
    ...\test\unit
    ...\test\unit\src

  4. In the IDE explorer mount directory c:\myproduct\mymodule\src
  5. Create sample HelloWorld class in mounted file system
    public class HelloWorld extends Object {
    public HelloWorld(){}

    public String greeting() {
    return "Hello world!";
    }
    }

  6. Create another sample class in "src/myorg" package
    package myorg;
    public class Hello2 extends Object {
    public Hello2() {}

    public String hello(String name) {
    return "Hello " + name + "!";
    }
    }

Lab 2 - Create test application skeleton

The NetBeans JUnit Module can be effectively used to create skeleton of test application. Make sure you have JUnit module installed before you will proceed this lab.

  1. In the IDE explorer mount directory c:\myproduct\mymodule\test\unit\src
  2. Create test code skeleton using JUnit Module, right-click node representing mounted module sources (...\mymodule\src) and follow context menu (Tools -> Tests -> Create).
  3. In unit test generator configuration dialog select mounted .../test/unit/src directory to be file system containing generated tests.
  4. Press OK, HelloWorldTest and myorg.Hello2Test classes should be generated.
  5. Compile generated test sceletons.
  6. If you want to use NetBeans extension to JUnit, which offers extra functionality over plain JUnit, like filtering test methods or asserting files, you need also to do the following steps (future version of JUnit module will be capable of generating also tests using NetBeans extension to JUnit):
    1. Import NetBeans extension to JUnit classes (import org.netbeans.junit.*;).
    2. Change the base class of your test class from TestCase to NbTestCase.
    3. In method suite(), instead of creating new TestSuite() class, create new NbTestSuite class.

Lab 3 - Fill in the test code

The test functions should be filled in skeletons generated by JUnit module. The JUnit assert functions should be used to check conditions and report failures when expected results doesn't match results obtained in test execution.

You may write your test code casually without checking for exceptional situations, because any exception thrown by your test code is automatically catched and reported by the JUnit test framework causing your test to fail.

Examples shown on this page use NetBeans extension to JUnit.

  1. Complete the "HelloWorldTest" class with following code:
    import java.io.*;
    import java.net.URL;
    import junit.framework.*;
    import org.netbeans.junit.*;

    public class HelloWorldTest extends NbTestCase {

    public HelloWorldTest(java.lang.String testName) {
    super(testName);
    }

    public static void main(java.lang.String[] args) {
    junit.textui.TestRunner.run(suite());
    }

    public static Test suite() {
    TestSuite suite = new NbTestSuite(HelloWorldTest.class);

    return suite;
    }

    /** Test of greeting method, of class HelloWorld. */
    public void testGreeting1() {
    String greeting;

    System.out.println("testGreeting1");
    greeting = testObject.greeting();
    assert(null != greeting);
    }

    public void testGreeting2() throws IOException {
    File test;
    File pass;
    FileWriter wr;
    String greeting;

    System.out.println("testGreeting2");
    greeting = testObject.greeting();

    test = new File(dataDir, "greeting.test");
    pass = new File(dataDir, "greeting.pass");
    wr = new FileWriter(test);
    wr.write(greeting);
    wr.close();

    assertFile(test, pass, dataDir);
    }

    protected HelloWorld testObject;
    protected File dataDir;

    protected void setUp() {
    dataDir = new File(getClass().getResource("data").getFile());
    testObject = new HelloWorld();
    }
    }

  2. Complete the "Hello2Test" class with following code:
    package myorg;

    import junit.framework.*;
    import org.netbeans.junit.*;

    public class Hello2Test extends NbTestCase {

    public Hello2Test(java.lang.String testName) {
    super(testName);
    }

    public static void main(java.lang.String[] args) {
    junit.textui.TestRunner.run(suite());
    }

    public static Test suite() {
    TestSuite suite = new NbTestSuite(Hello2Test.class);

    return suite;
    }

    /** Test of hello method, of class myorg.Hello2. */
    public void testHello() {
    System.out.println("testHello");

    String greeting = testObject.hello("Joe");
    assertEquals(greeting, "Hello Joe!");
    }

    protected Hello2 testObject;

    protected void setUp() {
    testObject = new Hello2();
    }
    }

Lab 4 - Run test application

The test can be simply started from IDE and in case of any defects easily "debugged" using test results reported in IDE output window. JUnit module supports three types of test execution - external, internal and debug, use menu Project -> Settings -> Unit tests to change active type of execution (see JUnit module documentation for details).

  1. Right-click the RootSuite class and follow context menu Tools -> Tests -> Run
  2. Test classes should be recompiled and executed, test results are reported in IDE Output window. Note, that reported failures are linked with test source codes where they originated (double-click red lines in Output Window).
  3. There should be one failure reported in your tests results. This is caused by the missing test data.
  4. During the tests execution there should be created .../test/unit/src/data/greeting.test file, rename this file to greeting.pass.
  5. Run tests again, all tests should pass now.
  6. Change content of the greeting.pass file and run tests again. You should see the failure caused by AssertionFileFailedException, check the .../data/greeting.diff file, which should contain differences between the 'golden' file greeting.pass and greeting.test file generated during the test execution.

Hint: You can execute tests form any of your test classes by right-clicking the Tools -> Tests -> Run context menu.

Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by