Java Tests

Java Tests

Compatibility

Supported test frameworks:

  • JUnit >= 4.10 and >= 5.3
    • Also includes any test framework based on JUnit, such as Spock Framework and Cucumber-Junit
  • TestNG >= 6.4

Prerequisites

Install the Datadog Agent to collect tests data.

Installing the Java tracer

Add a new Maven profile in your root pom.xml configuring the Datadog Java tracer dependency and the javaagent arg property, replacing $VERSION with the latest version of the tracer accessible from the Maven Repository (without the preceding v): Maven Central

pom.xml

<profile>
  <id>dd-civisibility</id>
  <activation>
    <activeByDefault>false</activeByDefault>
  </activation>
  <properties>
    <dd.java.agent.arg>-javaagent:${settings.localRepository}/com/datadoghq/dd-java-agent/$VERSION/dd-java-agent-$VERSION.jar -Ddd.service=my-java-app -Ddd.prioritization.type=ENSURE_TRACE -Ddd.jmxfetch.enabled=false -Ddd.integrations.enabled=false -Ddd.integration.junit.enabled=true -Ddd.integration.testng.enabled=true</dd.java.agent.arg>
  </properties>
  <dependencies>
    <dependency>
        <groupId>com.datadoghq</groupId>
        <artifactId>dd-java-agent</artifactId>
        <version>$VERSION</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
</profile>

Add the ddTracerAgent entry to the configurations task block, and add the Datadog Java tracer dependency, replacing $VERSION with the latest version of the tracer available in the Maven Repository (without the preceding v): Maven Central

build.gradle

configurations {
    ddTracerAgent
}
dependencies {
    ddTracerAgent "com.datadoghq:dd-java-agent:$VERSION"
}

Instrumenting your tests

Configure the Maven Surefire Plugin or the Maven Failsafe Plugin (or both if you use both) to use Datadog Java agent, specifying the name of the service or library under test with the -Ddd.service property:

pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>${dd.java.agent.arg}</argLine>
  </configuration>
</plugin>

pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <configuration>
     <argLine>${dd.java.agent.arg}</argLine>
  </configuration>
  <executions>
      <execution>
        <goals>
           <goal>integration-test</goal>
           <goal>verify</goal>
        </goals>
      </execution>
  </executions>
</plugin>

Run your tests as you normally do, specifying the environment where tests are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) in the DD_ENV environment variable. For example:

DD_ENV=ci mvn clean verify -Pdd-civisibility

Configure the test Gradle task by adding to the jvmArgs attribute the -javaagent argument targeting the Datadog Java tracer based on the configurations.ddTracerAgent property, specifying the name of the service or library under test with the -Ddd.service property:

build.gradle

test {
  if(project.hasProperty("dd-civisibility")) {
    jvmArgs = ["-javaagent:${configurations.ddTracerAgent.asPath}", "-Ddd.service=my-java-app", "-Ddd.prioritization.type=ENSURE_TRACE", "-Ddd.jmxfetch.enabled=false", "-Ddd.integrations.enabled=false", "-Ddd.integration.junit.enabled=true", "-Ddd.integration.testng.enabled=true"]
  }
}

Run your tests as you normally do, specifying the environment where test are being run (for example, local when running tests on a developer workstation, or ci when running them on a CI provider) in the DD_ENV environment variable. For example:

DD_ENV=ci ./gradlew cleanTest test -Pdd-civisibility --rerun-tasks

Note: As Gradle builds can be customizable programmatically, you may need to adapt these steps to your specific build configuration.

Configuration settings

The following system properties set configuration options and have environment variable equivalents. If the same key type is set for both, the system property configuration takes priority. System properties can be set as JVM flags.

dd.service
Name of the service or library under test.
Environment variable: DD_SERVICE
Default: unnamed-java-app
Example: my-java-app
dd.env
Name of the environment where tests are being run.
Environment variable: DD_ENV
Default: none
Examples: local, ci
dd.trace.agent.url
Datadog Agent URL for trace collection in the form http://hostname:port.
Environment variable: DD_TRACE_AGENT_URL
Default: http://localhost:8126

All other Datadog Tracer configuration options can also be used.

Important: You may want to enable more integrations if you have integration tests. To enable a specific integration, use the Datadog Tracer Compatibility table to create your custom setup for your integration tests.

For example, to enable OkHttp3 client request integration, add -Ddd.integration.okhttp-3.enabled=true to your setup.

Further reading