Skip to content

Commit

Permalink
Add hasAnObservationWithAKeyValue() variant with KeyValue (#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye authored Aug 21, 2023
1 parent a2ad7e1 commit a56b968
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.observation.tck;

import io.micrometer.common.KeyValue;
import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
import org.assertj.core.api.ThrowingConsumer;
Expand Down Expand Up @@ -345,6 +346,25 @@ public TestObservationRegistryAssert hasAnObservationWithAKeyValue(String key, S
return this;
}

/**
* Verifies that there is an Observation with a key value.
* <p>
* Examples: <pre><code class='java'>
* // assertions succeed
* assertThat(testObservationRegistry).hasAnObservationWithAKeyValue(KeyValue.of("foo", "bar"));
*
* // assertions fail - assuming that there is no such a key value in any observation
* assertThat(testObservationRegistry).hasAnObservationWithAKeyValue(KeyValue.of("foo", "bar"));</code></pre>
* @param keyValue expected key value
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if there is no Observation with given key name and value
* @since 1.12.0
*/
public TestObservationRegistryAssert hasAnObservationWithAKeyValue(KeyValue keyValue) {
return hasAnObservationWithAKeyValue(keyValue.getKey(), keyValue.getValue());
}

/**
* Verifies that there is an Observation with a key name.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.observation.tck;

import io.micrometer.common.KeyValue;
import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
Expand Down Expand Up @@ -306,6 +307,9 @@ void should_fail_when_key_value_not_matched() {
thenThrownBy(
() -> TestObservationRegistryAssert.assertThat(registry).hasAnObservationWithAKeyValue("key", "value"))
.isInstanceOf(AssertionError.class);

thenThrownBy(() -> TestObservationRegistryAssert.assertThat(registry)
.hasAnObservationWithAKeyValue(KeyValue.of("key", "value"))).isInstanceOf(AssertionError.class);
}

@Test
Expand All @@ -314,6 +318,9 @@ void should_not_fail_when_key_value_matched() {

thenNoException().isThrownBy(
() -> TestObservationRegistryAssert.assertThat(registry).hasAnObservationWithAKeyValue("foo", "bar"));

thenNoException().isThrownBy(() -> TestObservationRegistryAssert.assertThat(registry)
.hasAnObservationWithAKeyValue(KeyValue.of("foo", "bar")));
}

@Test
Expand Down

0 comments on commit a56b968

Please sign in to comment.