TestComplete Unit Tests - Java Applications

Applies to TestComplete 15.69, last modified on November 13, 2024
Information in this topic applies to desktop applications only.

This topic explains how you can create and organize the unit tests of your Java applications using TestComplete.

Requirements

  • You must have a license for TestComplete Desktop module.

  • Your application must be compiled as Open with debug information. Note that TestComplete can recognize Java applications as “Open” only if they are created with the AWT, SWT or Swing library.

  • You must make your test methods as object methods. This corresponds to the unit test structure, in which one testing class serves for the testing of one or several objects in the application under test.

Preparing Application for Unit Testing

To make test routines available to TestComplete, add the TestComplete unit testing assembly to your Java application:

  1. Add the <TestComplete>\Bin\Extensions\tcJavaUnitTesting.jar file, shipped with TestComplete, to the CLASSPATH environment variable, or specify the file in the -classpath (or -cp) command-line argument when starting the Java compiler (javac) or Java engine (java). This way you will be able to use the UnitTesting class defined in the file.

  2. Open your Java project in your development tool.

  3. Add a call to the com.AutomatedQA.testcomplete.UnitTesting.AddClasses method in your application:

    Java

    com.AutomatedQA.testcomplete.UnitTesting.AddClasses(new Class[]{MyTestClass.class});

    The AddClasses method uses one parameter - an array of testing classes. In our example, the array only holds one element, which refers to the class MyTestClass.

  4. Recompile your application.

Calling Test Routines From TestComplete

After you compile your application, you can call test routines from TestComplete:

  1. Launch your Java application.

  2. In the application, call the AddClasses method to make the test classes and their methods available to TestComplete.

    Note: You can skip this step if you call the AddClasses method in the constructor of the application’s main frame.
  3. In TestComplete, open your test project and add a TCUnitTest item to the project.

  4. Open the added test item in the TCUnitTest editor.

  5. In the Process text box, click the ellipsis button and select the process started by your application from the list of running processes.

    Specify the tested project
  6. In the editor, specify the needed tests. To load all tests defined in the application, click Load. To add tests manually, click Add Test and enter the test routine name on the Tests lists. If needed, unite tests into groups.

    TCUnit Test item settings
  7. Choose the test run mode:

    • Automatically load a list of tests and run all tests - Select this option to run all tests defined in the specified application.
    • Run selected tests only - Select this option to specify the list of test routines to be run.
    • Automatically load a list of tests and run without selected tests - Select this option to run all tests defined in the application except for the tests selected on the Tests list.
  8. Once you configure the test in your project, run it.

Important Notes:

  • When TestComplete runs a unit test, it first creates a new instance of the testing class that contains this routine and then calls the routine. TestComplete does not use existent instances of testing classes. It always creates new instances.

  • The testing class must have a constructor without any parameters. Otherwise, TestComplete will not be able to create a new class instance.

  • TestComplete can call a test routine only if it does not take any parameters because there is currently no way to supply test routines with parameters.

Alternatives to TestComplete Unit Tests

As an alternative, you can create unit tests for your Java applications by using the JUnit or TestNG framework and include the tests to your test project by using the JUnit or TestNG items correspondingly. See –

Running JUnit and TestNG Unit Tests and Selenium Tests Created With JUnit or TestNG

Running JUnit and TestNG Unit Tests and Selenium Tests Created With JUnit or TestNG

Samples

TestComplete includes a sample application that demonstrates unit testing of Java applications:

<TestComplete Samples>\Desktop\Unit Testing\Swing

TestComplete project suite:

<TestComplete Samples>\Desktop\Unit Testing\Swing\TCProject

To compile this example, run compile.bat kept in the example’s folder. To run the example, launch run.bat in this folder.

Note that to compile the example, you must have JDK v. 1.2 or later installed on your computer. Also, you may need to specify a path to javac.exe in the first line of compile.bat.

Note: If you do not have the sample, download the TestComplete Samples installation package from the support.smartbear.com/testcomplete/downloads/samples page of our website and run it.

See Also

Unit Testing

Highlight search results