Skip to content

Commit 4a29322

Browse files
authored
Use failure with actual and expected message to improve IDE experience (#5550)
1 parent ceb213a commit 4a29322

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationContextAssert.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public SELF hasNameEqualTo(String name) {
7171
isNotNull();
7272
String actualName = this.actual.getName();
7373
if (!Objects.equals(name, actualName)) {
74-
failWithMessage("Observation should have name equal to <%s> but has <%s>", name, actualName);
74+
failWithActualExpectedAndMessage(actualName, name,
75+
"Observation should have name equal to <%s> but has <%s>", name, actualName);
7576
}
7677
return (SELF) this;
7778
}
@@ -89,7 +90,8 @@ public SELF hasNameEqualToIgnoringCase(String name) {
8990
isNotNull();
9091
String actualName = this.actual.getName();
9192
if (!name.equalsIgnoreCase(actualName)) {
92-
failWithMessage("Observation should have name equal to ignoring case <%s> but has <%s>", name, actualName);
93+
failWithActualExpectedAndMessage(actualName, name,
94+
"Observation should have name equal to ignoring case <%s> but has <%s>", name, actualName);
9395
}
9496
return (SELF) this;
9597
}
@@ -107,7 +109,8 @@ public SELF hasContextualNameEqualTo(String name) {
107109
isNotNull();
108110
String actualName = this.actual.getContextualName();
109111
if (!Objects.equals(name, actualName)) {
110-
failWithMessage("Observation should have contextual name equal to <%s> but has <%s>", name, actualName);
112+
failWithActualExpectedAndMessage(actualName, name,
113+
"Observation should have contextual name equal to <%s> but has <%s>", name, actualName);
111114
}
112115
return (SELF) this;
113116
}
@@ -125,7 +128,8 @@ public SELF hasContextualNameEqualToIgnoringCase(String name) {
125128
isNotNull();
126129
String actualName = this.actual.getContextualName();
127130
if (!name.equalsIgnoreCase(actualName)) {
128-
failWithMessage("Observation should have contextual name equal to ignoring case <%s> but has <%s>", name,
131+
failWithActualExpectedAndMessage(actualName, name,
132+
"Observation should have contextual name equal to ignoring case <%s> but has <%s>", name,
129133
actualName);
130134
}
131135
return (SELF) this;
@@ -251,7 +255,7 @@ public SELF hasLowCardinalityKeyValue(String key, String value) {
251255
.get()
252256
.getValue();
253257
if (!Objects.equals(tagValue, value)) {
254-
failWithMessage(
258+
failWithActualExpectedAndMessage(tagValue, value,
255259
"Observation should have a low cardinality tag with key <%s> and value <%s>. The key is correct but the value is <%s>",
256260
key, value, tagValue);
257261
}
@@ -328,7 +332,7 @@ public SELF hasHighCardinalityKeyValue(String key, String value) {
328332
.get()
329333
.getValue();
330334
if (!Objects.equals(tagValue, value)) {
331-
failWithMessage(
335+
failWithActualExpectedAndMessage(tagValue, value,
332336
"Observation should have a high cardinality tag with key <%s> and value <%s>. The key is correct but the value is <%s>",
333337
key, value, tagValue);
334338
}
@@ -462,7 +466,8 @@ public SELF hasParentObservationEqualTo(Observation expectedParent) {
462466
failWithMessage("Observation should have parent <%s> but has none", expectedParent);
463467
}
464468
if (!realParent.equals(expectedParent)) {
465-
failWithMessage("Observation should have parent <%s> but has <%s>", expectedParent, realParent);
469+
failWithActualExpectedAndMessage(realParent, expectedParent,
470+
"Observation should have parent <%s> but has <%s>", expectedParent, realParent);
466471
}
467472
return (SELF) this;
468473
}

micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationContextAssertTests.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import io.micrometer.observation.ObservationRegistry;
2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
24+
import org.opentest4j.AssertionFailedError;
2425

2526
import java.util.function.Supplier;
2627

2728
import static io.micrometer.observation.tck.ObservationContextAssert.assertThat;
29+
import static org.assertj.core.api.BDDAssertions.then;
2830
import static org.assertj.core.api.BDDAssertions.thenNoException;
2931
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
3032
import static org.mockito.Mockito.spy;
@@ -63,7 +65,11 @@ void should_not_throw_exception_when_name_correct() {
6365
void should_throw_exception_when_name_incorrect() {
6466
context.setName("foo");
6567

66-
thenThrownBy(() -> assertThat(context).hasNameEqualTo("bar")).isInstanceOf(AssertionError.class);
68+
thenThrownBy(() -> assertThat(context).hasNameEqualTo("bar")).isInstanceOfSatisfying(AssertionFailedError.class,
69+
error -> {
70+
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
71+
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
72+
});
6773
}
6874

6975
@Test
@@ -91,7 +97,11 @@ void should_not_throw_exception_when_contextual_name_correct() {
9197
void should_throw_exception_when_contextual_name_incorrect() {
9298
context.setContextualName("foo");
9399

94-
thenThrownBy(() -> assertThat(context).hasContextualNameEqualTo("bar")).isInstanceOf(AssertionError.class);
100+
thenThrownBy(() -> assertThat(context).hasContextualNameEqualTo("bar"))
101+
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
102+
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
103+
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
104+
});
95105
}
96106

97107
@Test
@@ -120,7 +130,11 @@ void should_not_throw_exception_when_name_ignore_case_correct() {
120130
void should_throw_exception_when_name_ignore_case_incorrect() {
121131
context.setName("foo");
122132

123-
thenThrownBy(() -> assertThat(context).hasNameEqualToIgnoringCase("bar")).isInstanceOf(AssertionError.class);
133+
thenThrownBy(() -> assertThat(context).hasNameEqualToIgnoringCase("bar"))
134+
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
135+
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
136+
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
137+
});
124138
}
125139

126140
@Test
@@ -150,7 +164,10 @@ void should_throw_exception_when_contextual_name_ignore_case_incorrect() {
150164
context.setContextualName("foo");
151165

152166
thenThrownBy(() -> assertThat(context).hasContextualNameEqualToIgnoringCase("bar"))
153-
.isInstanceOf(AssertionError.class);
167+
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
168+
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
169+
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
170+
});
154171
}
155172

156173
@Test
@@ -248,7 +265,10 @@ void should_throw_exception_when_low_cardinality_key_value_missing() {
248265
observation.lowCardinalityKeyValue("foo", "bar");
249266

250267
thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValue("foo", "baz"))
251-
.isInstanceOf(AssertionError.class);
268+
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
269+
then(error.getActual().getStringRepresentation()).isEqualTo("bar");
270+
then(error.getExpected().getStringRepresentation()).isEqualTo("baz");
271+
});
252272
thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValueWithKey("bar"))
253273
.isInstanceOf(AssertionError.class);
254274
}
@@ -284,7 +304,10 @@ void should_throw_exception_when_high_cardinality_key_value_missing() {
284304
observation.highCardinalityKeyValue("foo", "bar");
285305

286306
thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValue("foo", "baz"))
287-
.isInstanceOf(AssertionError.class);
307+
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
308+
then(error.getActual().getStringRepresentation()).isEqualTo("bar");
309+
then(error.getExpected().getStringRepresentation()).isEqualTo("baz");
310+
});
288311
thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValueWithKey("bar"))
289312
.isInstanceOf(AssertionError.class);
290313
}

0 commit comments

Comments
 (0)