JUNIT 4

Thundra provides out-of-the-box JUnit rule to take snapshots of defined tracepoints hit automatically during test execution. Those snapshots can be useful to debug failing tests and understand cause of the fail for flaky tests.

In order to get tracepoint snapshot events produced during test execution, first you need to login Thundra via IntelliJ IDEA plugin as explained here before running tests.

Thundra Dependency

Step 1: Add Thundra Repo

Add Thundra repo (repo.thundra.io) to your repositories:

<repositories>
<repository>
<id>thundra-releases</id>
<name>Thundra Releases</name>
<url>https://repo.thundra.io/content/repositories/thundra-releases</url>
</repository>
</repositories>

Step 2: Install Thundra Java Library

Add Thundra dependency as test scoped dependency:

<dependency>
<groupId>io.thundra.agent</groupId>
<artifactId>thundra-agent-broker-tracepoint</artifactId>
<scope>test</scope>
<version>${thundra.version}</version>
</dependency>

You can use the version shown below instead of ${thundra.version}.

Configuration

  • Application name and Thundra API key must be configured as explained here

  • Your Thundra login e-mail address must be specified

    • by setting THUNDRA_AGENT_BROKER_CLIENT environment variable

    • or by setting thundra.agent.broker.client YAML key in the thundra-config.yml file

A sample test configuration through environment variables will look like this:

export THUNDRA_APIKEY=<YOUR-THUNDRA-API-KEY>
export THUNDRA_AGENT_APPLICATION_NAME=MyServiceTest
export THUNDRA_AGENT_BROKER_CLIENT=<YOUR-THUNDRA-LOGIN-EMAIL>

or through YAML file can be specified as following:

thundra:
apiKey: <YOUR-THUNDRA-API-KEY>
agent:
application:
name: MyServiceTest
broker:
client: <YOUR-THUNDRA-LOGIN-EMAIL>

Test Rule

A sample test rule can be defined like the following example:

import io.thundra.agent.broker.tracepoint.test.junit4.TracePointSnapshotTestRule;
@Rule
public TracePointSnapshotTestRule tracePointSnapshotTestRule =
TracePointSnapshotTestRule
.builder()
.addTracePoint(
TracePointSnapshotTestRule.TracePoint
.builder()
.className("com.mycompany.MyService")
.lineNo(10)
.build())
.addTracePoint(
TracePointSnapshotTestRule.TracePoint
.builder()
.className("com.mycompany.MyAnotherService")
.lineNo(20)
.build())
// Other tracepoints ...
.build();

By default, tracepoint snapshots from any test execution are captured and sent. But Thundra test rule can be configured to filter out snapshots for successful tests and send for only failing tests. To do this, takeSnapshotOnFail attribute of the rule can be set to true.

Such sample test rule can be defined like the following example:

import io.thundra.agent.broker.tracepoint.test.junit4.TracePointSnapshotTestRule;
@Rule
public TracePointSnapshotTestRule tracePointSnapshotTestRule =
TracePointSnapshotTestRule
.builder()
.takeSnapshotOnFail(true)
.addTracePoint(
TracePointSnapshotTestRule.TracePoint
.builder()
.className("com.mycompany.MyService")
.lineNo(10)
.build())
.addTracePoint(
TracePointSnapshotTestRule.TracePoint
.builder()
.className("com.mycompany.MyAnotherService")
.lineNo(20)
.build())
// Other tracepoints ...
.build();