|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.
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:
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
UnitTestingclass defined in the file.
Open your Java project in your development tool.
Add a call to the
com.AutomatedQA.testcomplete.UnitTesting.AddClassesmethod in your application:
AddClassesmethod uses one parameter - an array of testing classes. In our example, the array only holds one element, which refers to the class
- Recompile your application.
Calling Test Routines From TestComplete
After you compile your application, you can call test routines from TestComplete:
Launch your Java application.
In the application, call the
AddClassesmethod to make the test classes and their methods available to TestComplete.
Note: You can skip this step if you call the
AddClassesmethod in the constructor of the application’s main frame.
In TestComplete, open your test project and add a TCUnitTest item to the project.
Open the added test item in the TCUnitTest editor.
In the Process text box, click the ellipsis button and select the process started by your application from the list of running processes.
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.
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.
Once you configure the test in your project, run it.
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 –
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.
|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.