In this section:

Introduction

The JUnit Executor tool enables you to execute JUnit tests in conjunction with the various other types of tests you can run with SOAtest. This enables you to design and execute a single test scenario that orchestrates JUnit tests as part of a broader sequence of events, such as setting up a test environment or test data, running unit tests via this tool, then executing mobile tests.

Requirements

  • JUnit 3.8.x and 4 are supported
  • This tool requires SOAtest 9.8.1 or later

Installation

You can install this tool from the UI or from the command line.

UI Installation

  1. Choose Parasoft> Preferences from the main menu.
  2. Choose the Syster Properties tab and click Add JARs.
  3. Brows for the junitexecutor.jar file and click Apply.
  4. Restart SOAtest.

Command Line Installation

Add the junitexecutor.jar file to the system.properties.classpath property in your settings.properties file. For example:

system.properties.classpath=<path to jar>/junitexecutor.jar 

Usage

The test's class folder or jar file must contain the JUnit that you want executed. Other dependencies can be included in the jar/folder, added to the system properties, or both. The tool checks the jar/folder first, then the system properties if necessary.

Adding the Tool

You can add the JUnit Executor as a standalone tool using the wizard.

  1. Right-click on a Test Suite folder and choose Add New> Test... 
  2. Browse for the JUnit Executor tool and click Finish.
  3. Configure the tool according to your scenario.

Running a Specific Test Method

  1. Specify the test's class folder or jar file, as well as the test class and test method you want to run.
  2. (Optional) If you want to dynamically change JUnit values from this tool, enter the desired key-value pairs, separated by a semicolon, in the Data field.

    These variables can be accessed from within your JUnit source code using Java's System Properties object, for example: 

    String lastName = System.getProperty("lastName")

Running all Test Methods in a Class

  1. Specify the test's class folder or jar file, as well as the test class. Leave the Method field empty.
  2. (Optional) If you want to dynamically change JUnit values from this tool, enter the desired key-value pairs, separated by a semicolon, in the Data field. These variables can be accessed from within your JUnit source code using Java's System Properties object, for example:  

    String lastName = System.getProperty("lastName")

Viewing Results

Execution details and results will be reported in the Console view. Additionally, any test failures detected will be reported in the Quality Tasks view:

Reporting Messages from JUnit Test to Console

Import the com.parasoft.api package into your project to access the Application Context, which enables SOAtest to report messages from your JUnit test execution to the console:

Application.showMessage("this displays in the console");

  Add <SOATEST_INSTALL>/plugins/com.parasoft.ptest.libs.web_<version>/root/com.parasoft.api.jar to your Java project classpath to import the resource.

Retrieving Data from a JUnit Test

You can attach a SOAtest output to the JUnit Executor to retrieve data from your JUnit test output so that the data can be used in other tests. You need to configure the JUnit to indicate which values you want stored, and then attach an appropriate tool to the JUnit Output of the executor tool. The data will be passed to the attached tool in XML format. For example, you could send the data to a Diff tool, XML Assertor, or XML Data Bank tool.

Configuring the JUnit Output

Your JUnit must access the application context and store the desired values in a standard Java Map under the custom_tool_junit_output key. The JUnit Executor tool will check the application context for the stored map and convert it to XML that gets passed to the tool's "JUnit Output" output. The map's keys and values must be strings.

Import the com.parasoft.api package into your JUnit as described in Reporting Messages from JUnit Test to Console to access the Application Context.

Sending the Data to Another Tool

To send the output data from your JUnit test to a tool that processes XML data:

  1. Right-click the JUnit Executor and choose Add Output.
  2. Choose a tool for receiving the output and click Finish.

Example

In this example, we will store the values of the multiplyResult and sumResult JUnit operations in an XML Data Bank so that the values can be used in a subsequent test. The following procedure will occur:

  1. The JUnit test constructs a map containing the values
  2. The map is placed into the application context.
  3. The values in the map will be converted into an XML document that is passed to any tools attached to the JUnit Output
@Test
public void testExample() throws Exception {
  int multiplyResult = 8 * 8;
  int sumResult = 8 + 8;

// Create map representing name-value pairs for XML 
  Map<String, String> map = new HashMap<String, String>(); 
  map.put("multiplyResult", String.valueOf(multiplyResult)); 
  map.put("sumResult", String.valueOf(sumResult));

// Get the Parasoft Scripting Context
  ScriptingContext context = Application.getContext();

// Place the map in the context with the custom_tool_junit_output 
  context.put("custom_tool_junit_output", map);
  }

Add a JUnit Executor tool configured to run this test and add an XML Data Bank tool to the JUnit output.

Specify which values to extract and use in other tools. The content of the map appears below the <root> element within the XML document that is passed to the tools attached to the JUnit output. In this example, a value assertion tool was added.

Third-party Content

This plug-in includes items that have been sourced from third parties as outlined below.

Additional license details are available in this plugin's licenses folder.


  • No labels