Skip to content

Commit f396ab2

Browse files
committed
feat(testing): Improve error messages
1 parent 1e72a88 commit f396ab2

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/Matchers.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package datadog.trace.agent.test.assertions;
22

3+
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
4+
35
import java.util.Optional;
46
import java.util.function.Predicate;
57
import java.util.regex.Pattern;
6-
import org.opentest4j.AssertionFailedError;
78

89
/** This class is a utility class to create generic matchers. */
910
public final class Matchers {
@@ -103,12 +104,11 @@ public static <T> Matcher<T> any() {
103104
static <T> void assertValue(Matcher<T> matcher, T value, String message) {
104105
if (matcher != null && !matcher.test(value)) {
105106
Optional<T> expected = matcher.expected();
106-
if (expected.isPresent()) {
107-
throw new AssertionFailedError(
108-
message + ". " + matcher.failureReason(), expected.get(), value);
109-
} else {
110-
throw new AssertionFailedError(message + ": " + value + ". " + matcher.failureReason());
111-
}
107+
assertionFailure()
108+
.message(message + ". " + matcher.failureReason())
109+
.expected(expected.orElse(null))
110+
.actual(value)
111+
.buildAndThrow();
112112
}
113113
}
114114
}

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/SpanMatcher.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static datadog.trace.agent.test.assertions.Matchers.validates;
1111
import static datadog.trace.core.DDSpanAccessor.spanLinks;
1212
import static java.time.Duration.ofNanos;
13+
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
1314

1415
import datadog.trace.api.TagMap;
1516
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
@@ -322,7 +323,7 @@ private void assertSpanTags(TagMap tags) {
322323
if (matcher == null) {
323324
uncheckedTagNames.add(key);
324325
} else {
325-
assertValue(matcher, value, "Unexpected " + key + " tag value.");
326+
assertValue(matcher, value, "Unexpected " + key + " tag value");
326327
}
327328
});
328329
// Remove matchers that accept missing tags
@@ -345,9 +346,13 @@ private void assertSpanTags(TagMap tags) {
345346
*/
346347
private void assertSpanLinks(List<AgentSpanLink> links) {
347348
int linkCount = links == null ? 0 : links.size();
348-
int expectedLinkCount = this.linkMatchers == null ? 0 : this.linkMatchers.length;
349+
int expectedLinkCount = this.linkMatchers.length;
349350
if (linkCount != expectedLinkCount) {
350-
throw new AssertionFailedError("Unexpected span link count", expectedLinkCount, linkCount);
351+
assertionFailure()
352+
.message("Unexpected span link count")
353+
.expected(expectedLinkCount)
354+
.actual(linkCount)
355+
.buildAndThrow();
351356
}
352357
for (int i = 0; i < expectedLinkCount; i++) {
353358
SpanLinkMatcher linkMatcher = this.linkMatchers[expectedLinkCount];

dd-java-agent/instrumentation-testing/src/main/java/datadog/trace/agent/test/assertions/TraceAssertions.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package datadog.trace.agent.test.assertions;
22

33
import static java.util.function.Function.identity;
4+
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;
45

56
import datadog.trace.core.DDSpan;
67
import java.util.Comparator;
78
import java.util.List;
89
import java.util.function.Function;
9-
import org.opentest4j.AssertionFailedError;
1010

1111
/**
1212
* This class is a helper class to verify traces structure.
@@ -87,11 +87,19 @@ public static void assertTraces(
8787
int traceCount = traces.size();
8888
if (opts.ignoredAdditionalTraces) {
8989
if (traceCount < expectedTraceCount) {
90-
throw new AssertionFailedError("Not enough of traces", expectedTraceCount, traceCount);
90+
assertionFailure()
91+
.message("Not enough of traces")
92+
.expected(expectedTraceCount)
93+
.actual(traceCount)
94+
.buildAndThrow();
9195
}
9296
} else {
9397
if (traceCount != expectedTraceCount) {
94-
throw new AssertionFailedError("Invalid number of traces", expectedTraceCount, traceCount);
98+
assertionFailure()
99+
.message("Invalid number of traces")
100+
.expected(expectedTraceCount)
101+
.actual(traceCount)
102+
.buildAndThrow();
95103
}
96104
}
97105
if (opts.sorter != null) {

0 commit comments

Comments
 (0)