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:
1
<repositories>
2
<repository>
3
<id>thundra-releases</id>
4
<name>Thundra Releases</name>
5
<url>https://repo.thundra.io/content/repositories/thundra-releases</url>
6
</repository>
7
</repositories>
Copied!
Step 2: Install Thundra Java Library
Add Thundra dependency as test scoped dependency:
1
<dependency>
2
<groupId>io.thundra.agent</groupId>
3
<artifactId>thundra-agent-broker-tracepoint</artifactId>
4
<scope>test</scope>
5
<version>${thundra.version}</version>
6
</dependency>
Copied!
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:
1
export THUNDRA_APIKEY=<YOUR-THUNDRA-API-KEY>
2
export THUNDRA_AGENT_APPLICATION_NAME=MyServiceTest
3
export THUNDRA_AGENT_BROKER_CLIENT=<YOUR-THUNDRA-LOGIN-EMAIL>
Copied!
or through YAML file can be specified as following:
1
thundra:
2
apiKey: <YOUR-THUNDRA-API-KEY>
3
agent:
4
application:
5
name: MyServiceTest
6
broker:
7
client: <YOUR-THUNDRA-LOGIN-EMAIL>
Copied!

Test Rule

A sample test rule can be defined like the following example:
1
import io.thundra.agent.broker.tracepoint.test.junit4.TracePointSnapshotTestRule;
2
3
@Rule
4
public TracePointSnapshotTestRule tracePointSnapshotTestRule =
5
TracePointSnapshotTestRule
6
.builder()
7
.addTracePoint(
8
TracePointSnapshotTestRule.TracePoint
9
.builder()
10
.className("com.mycompany.MyService")
11
.lineNo(10)
12
.build())
13
.addTracePoint(
14
TracePointSnapshotTestRule.TracePoint
15
.builder()
16
.className("com.mycompany.MyAnotherService")
17
.lineNo(20)
18
.build())
19
// Other tracepoints ...
20
.build();
Copied!
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:
1
import io.thundra.agent.broker.tracepoint.test.junit4.TracePointSnapshotTestRule;
2
3
@Rule
4
public TracePointSnapshotTestRule tracePointSnapshotTestRule =
5
TracePointSnapshotTestRule
6
.builder()
7
.takeSnapshotOnFail(true)
8
.addTracePoint(
9
TracePointSnapshotTestRule.TracePoint
10
.builder()
11
.className("com.mycompany.MyService")
12
.lineNo(10)
13
.build())
14
.addTracePoint(
15
TracePointSnapshotTestRule.TracePoint
16
.builder()
17
.className("com.mycompany.MyAnotherService")
18
.lineNo(20)
19
.build())
20
// Other tracepoints ...
21
.build();
Copied!
Last modified 7mo ago