Skip to content

Commit

Permalink
Use failure with actual and expected message to improve IDE experience
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Oct 7, 2024
1 parent 80dfc76 commit 412fd9c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public SELF hasNameEqualTo(String name) {
isNotNull();
String actualName = this.actual.getName();
if (!Objects.equals(name, actualName)) {
failWithMessage("Observation should have name equal to <%s> but has <%s>", name, actualName);
failWithActualExpectedAndMessage(actualName, name,
"Observation should have name equal to <%s> but has <%s>", name, actualName);
}
return (SELF) this;
}
Expand All @@ -89,7 +90,8 @@ public SELF hasNameEqualToIgnoringCase(String name) {
isNotNull();
String actualName = this.actual.getName();
if (!name.equalsIgnoreCase(actualName)) {
failWithMessage("Observation should have name equal to ignoring case <%s> but has <%s>", name, actualName);
failWithActualExpectedAndMessage(actualName, name,
"Observation should have name equal to ignoring case <%s> but has <%s>", name, actualName);
}
return (SELF) this;
}
Expand All @@ -107,7 +109,8 @@ public SELF hasContextualNameEqualTo(String name) {
isNotNull();
String actualName = this.actual.getContextualName();
if (!Objects.equals(name, actualName)) {
failWithMessage("Observation should have contextual name equal to <%s> but has <%s>", name, actualName);
failWithActualExpectedAndMessage(actualName, name,
"Observation should have contextual name equal to <%s> but has <%s>", name, actualName);
}
return (SELF) this;
}
Expand All @@ -125,7 +128,8 @@ public SELF hasContextualNameEqualToIgnoringCase(String name) {
isNotNull();
String actualName = this.actual.getContextualName();
if (!name.equalsIgnoreCase(actualName)) {
failWithMessage("Observation should have contextual name equal to ignoring case <%s> but has <%s>", name,
failWithActualExpectedAndMessage(actualName, name,
"Observation should have contextual name equal to ignoring case <%s> but has <%s>", name,
actualName);
}
return (SELF) this;
Expand Down Expand Up @@ -251,7 +255,7 @@ public SELF hasLowCardinalityKeyValue(String key, String value) {
.get()
.getValue();
if (!Objects.equals(tagValue, value)) {
failWithMessage(
failWithActualExpectedAndMessage(tagValue, value,
"Observation should have a low cardinality tag with key <%s> and value <%s>. The key is correct but the value is <%s>",
key, value, tagValue);
}
Expand Down Expand Up @@ -328,7 +332,7 @@ public SELF hasHighCardinalityKeyValue(String key, String value) {
.get()
.getValue();
if (!Objects.equals(tagValue, value)) {
failWithMessage(
failWithActualExpectedAndMessage(tagValue, value,
"Observation should have a high cardinality tag with key <%s> and value <%s>. The key is correct but the value is <%s>",
key, value, tagValue);
}
Expand Down Expand Up @@ -462,7 +466,8 @@ public SELF hasParentObservationEqualTo(Observation expectedParent) {
failWithMessage("Observation should have parent <%s> but has none", expectedParent);
}
if (!realParent.equals(expectedParent)) {
failWithMessage("Observation should have parent <%s> but has <%s>", expectedParent, realParent);
failWithActualExpectedAndMessage(realParent, expectedParent,
"Observation should have parent <%s> but has <%s>", expectedParent, realParent);
}
return (SELF) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import io.micrometer.observation.ObservationRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import java.util.function.Supplier;

import static io.micrometer.observation.tck.ObservationContextAssert.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenNoException;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -63,7 +65,11 @@ void should_not_throw_exception_when_name_correct() {
void should_throw_exception_when_name_incorrect() {
context.setName("foo");

thenThrownBy(() -> assertThat(context).hasNameEqualTo("bar")).isInstanceOf(AssertionError.class);
thenThrownBy(() -> assertThat(context).hasNameEqualTo("bar")).isInstanceOfSatisfying(AssertionFailedError.class,
error -> {
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
});
}

@Test
Expand Down Expand Up @@ -91,7 +97,11 @@ void should_not_throw_exception_when_contextual_name_correct() {
void should_throw_exception_when_contextual_name_incorrect() {
context.setContextualName("foo");

thenThrownBy(() -> assertThat(context).hasContextualNameEqualTo("bar")).isInstanceOf(AssertionError.class);
thenThrownBy(() -> assertThat(context).hasContextualNameEqualTo("bar"))
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
});
}

@Test
Expand Down Expand Up @@ -120,7 +130,11 @@ void should_not_throw_exception_when_name_ignore_case_correct() {
void should_throw_exception_when_name_ignore_case_incorrect() {
context.setName("foo");

thenThrownBy(() -> assertThat(context).hasNameEqualToIgnoringCase("bar")).isInstanceOf(AssertionError.class);
thenThrownBy(() -> assertThat(context).hasNameEqualToIgnoringCase("bar"))
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
});
}

@Test
Expand Down Expand Up @@ -150,7 +164,10 @@ void should_throw_exception_when_contextual_name_ignore_case_incorrect() {
context.setContextualName("foo");

thenThrownBy(() -> assertThat(context).hasContextualNameEqualToIgnoringCase("bar"))
.isInstanceOf(AssertionError.class);
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
then(error.getActual().getStringRepresentation()).isEqualTo("foo");
then(error.getExpected().getStringRepresentation()).isEqualTo("bar");
});
}

@Test
Expand Down Expand Up @@ -248,7 +265,10 @@ void should_throw_exception_when_low_cardinality_key_value_missing() {
observation.lowCardinalityKeyValue("foo", "bar");

thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValue("foo", "baz"))
.isInstanceOf(AssertionError.class);
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
then(error.getActual().getStringRepresentation()).isEqualTo("bar");
then(error.getExpected().getStringRepresentation()).isEqualTo("baz");
});
thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValueWithKey("bar"))
.isInstanceOf(AssertionError.class);
}
Expand Down Expand Up @@ -284,7 +304,10 @@ void should_throw_exception_when_high_cardinality_key_value_missing() {
observation.highCardinalityKeyValue("foo", "bar");

thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValue("foo", "baz"))
.isInstanceOf(AssertionError.class);
.isInstanceOfSatisfying(AssertionFailedError.class, error -> {
then(error.getActual().getStringRepresentation()).isEqualTo("bar");
then(error.getExpected().getStringRepresentation()).isEqualTo("baz");
});
thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValueWithKey("bar"))
.isInstanceOf(AssertionError.class);
}
Expand Down

0 comments on commit 412fd9c

Please sign in to comment.