As an aggregator, the Jtest Plugin form Maven must be executed only once during the build. We recommend configuring the jtest goal execution directly from command line or as a reporting plugin.

You do not need to build a project before running analysis, but we recommend compiling multi-module projects beforehand. Doing so enables Jtest to use artifacts from the local repository, reducing the amount of time necessary to test and analyze code.

Before you run analysis or collect coverage information with Maven, ensure that the Jtest license is properly configured (see Setting the License).

Running Static Analysis

To perform static analysis on your code:

  1. Ensure that the Jtest Plugin for Maven is set up (see Configuring the Jtest Plugin for Maven).
  2. Execute the jtest:jtest goal. Your command line may resemble the following:

    mvn jtest:jtest
  3. Review the analysis results (see Reviewing Results).

If Jtest reports compilation problems related to importing dependencies in the Maven test scope, try running the jtest:jtest goal with the mvn test command:

mvn test jtest:jtest

Executing Unit Tests

You can include unit test results in the Jtest report by running your tests with the jtest:jtest goal and the dedicated Unit Tests built-in test configuration:

  1. Ensure that the Jtest Plugin for Maven is set up (see Configuring the Jtest Plugin for Maven).
  2. Execute the jtest:jtest goal with Maven. Your command line may resemble the following:

    mvn clean test jtest:jtest -Djtest.config="builtin://Unit Tests"

Collecting Coverage for Unit Tests

You can collect coverage information during execution of unit tests by your tests with the jtest:jtestand jtest:agent goals, and the dedicated Unit Tests built-in test configuration:

  1. Ensure that the Jtest Plugin for Maven is set up (see Configuring the Jtest Plugin for Maven).
  2. Execute the jtest:agent and jtest:jtest goals Maven. Ensure that the tests and classes are compiled before the jtest:agent goal is run.  Your command line may resemble the following:

    mvn clean test-compile jtest:agent test jtest:jtest -Djtest.config="builtin://Unit Tests"

(info) Executing the jtest:agent goal before the classes are compiled, may result in empty coverage. If your build uses Tycho plugins, or any other plugins that are detached from the default lifecycle, invoke the dedicated (non-default) compiler to ensure that the classes are properly compiled and coverage information is collected. Your command line may resemble the following:

mvn clean tycho-compiler:compile jtest:agent verify jtest:jtest -Djtest.config="builtin://Unit Tests"

About the jtest:agent Goal

The jtest:agent goal generates the Jtest agent settings based on the build modules, and sets the javaagent VM agument in the properties specified with the agentPropertyNames parameter in the initialize phase.

By default, it tries to inject javaagent VmArg into maven-surefire-plugin and eclipse-test-plugin through specific properties.

Prerequisites for using the jtest:agent goal with the Maven test plugins:

  • tests must be executed in the forked process (for example, with the parameters forkCount or forkMode)
  • additional VmArgs must contain Jtest javaagent VmArgs (the argLine parameter)

The jtest:agent goal attempts to automatically configure the Maven test plugins (maven-surefire-plugin and tycho-surefire-plugin) by setting up the properties of their VmArgs parameters (by default through the argLine and tycho.testArgLine properties). You can customize the default values of these parameters if one of the following ways:

  • Through the default property. The jtest:agent goal will extend the properties with the Jtest javaagent VmArg :

    <project>
      <!-- ... -->
      <properties>
        <!-- argLine property which will be extended by jtest:agent goal --->
        <argLine>-Xmx=1024m -XX:MaxPermSize=256m</argLine>
      </properties>
      <!-- ... -->
      <build>
        <plugins><!-- or pluginManagement -->
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <forkCount>1</forkCount>
              <!-- do not configure argLine parameter here -->
            </configuration>
          </plugin>
        </plugin>
      </build>
    </project>
  • Through a parameter value with an injected custom property:

    <project>
      <!-- ... -->
      <build>
        <plugins>
          <plugin>
            <groupId>com.parasoft.jtest</groupId>
            <artifactId>jtest-maven-plugin</artifactId>
            <configuration>
              <!-- jtest:agent goal will set Jtest Java agent VmArg into properties below --> 
              <agentPropertyNames>jtest.argLine</agentPropertyNames>
              <!-- optional coverage block with parameters identical as in offline coverage -->
              <coverage>
                <!-- ... -->
              </coverage> 
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>${jtest.argLine} -Xmx=1024m -XX:MaxPermSize=256m</argLine>
            </configuration>
          </plugin>
        </plugin>
      </build>
    </project>

About the jtest:instrument Goal (deprecated)

You can collect coverage for unit tests by running the jtest:instrument goal, which instruments the .class files before test execution (in the process-test-classes phase). Your command line may resemble the following:

mvn clean test-compile jtest:instrument test jtest:jtest -Djtest.config="builtin://Unit Tests"

Ensure that the primary reporting scope is wider than the instrumentation scope for coverage to be collected. You can customize the reporting scope with the resource parameter. By default, everything is in scope.

<project>
  <!-- ... -->
  <build>
    <!-- ... -->
    <plugins>
      <!-- ... -->
      <plugin>
        <groupId>com.parasoft.jtest</groupId>
        <artifactId>jtest-maven-plugin</artifactId>
        <configuration>
          <includes><!-- reporting scope configuration -->
            <!-- report coverage for all files in specified package -->
            <include>**/my/package/*</include>
          <includes>
          <coverage>
            <excludes>
              <!-- classes instrumentatio excludes -->
              <exclude>**/AnnotationProcessor.class</exclude>
            </excludes>
            <testExcludes>
              <!-- test classes instrumentation excludes -->
              <exclude>**/*Util.class</exclude>
            </testExcludes>
          </coverage>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

We recommend using the jtest:agent goal for collecting coverage, as offline instrumentation with the jtest:instrument goal may lead to corrupted bytecode of build artifacts.

Configuration Guidelines

  • Disable incremental compilation by adding  -Dmaven.compiler.useIncrementalCompilation=false to your command line to prevent instrumented classes from being overridden during recompilation. This option is available only for Maven 3.1 or later, and requires older Maven versions to be upgraded. Instrumented classes are overridden during recompilation due to a known problem in the maven-compiler-plugin (version 3.0 and later).
    The maven-compiler-plugin (version 3.0 and later) has a known problem
  • If your build uses other plugins that instrument code (for example, aspectj), ensure that the Jtest Plugin for Maven is executed in the process-classes phase. To achieve this, explicitly configure the execution in the POM file:

    <build>
      <!-- ... -->
      <plugins>
        <!-- other plugin that instruments the code --> 
        <plugin>
          <groupId>com.parasoft.jtest</groupId>
          <artifactId>jtest-maven-plugin</artifactId>
          <executions>
            <execution>
              <goals>
                <goal>instrument</goal>
              </goals>
              <phase>process-classes</phase>
            </execution>
          </executions>
        </plugin>
        
      </plugins>
    </build> 
  • If your project includes a defined annotation processor that transforms compiled classes, ensure that the class (or module that contains the annotation processor) is excluded from instrumentation. See jtest:instrument for information on how to use the excludes option to exclude specific files from instrumentation (you can use the skip option to exclude the entire module).

Collecting Application Coverage

Jtest's coverage agent allows you to collect coverage data during manual or automated tests performed on a running application. See Application Coverage for information about collecting application coverage with Jtest.

  • No labels