Attribute assertions should always contain the attr key (#5027)

* improve attribute assertions to alwyas contain the attr key

* spotless

* change assertion

* Update sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/AttributeAssertionTest.java

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>

* fix build

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>
This commit is contained in:
jason plumb 2022-12-09 10:51:07 -08:00 committed by GitHub
parent ea500962d6
commit 79a601d7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -37,6 +37,13 @@ public abstract class AttributeAssertion {
// suppressing here.
@SuppressWarnings("NullAway")
static AbstractAssert<?, ?> attributeValueAssertion(AttributeKey<?> key, @Nullable Object value) {
AbstractAssert<? extends AbstractAssert<?, ?>, ?> abstractAssert = makeAssertion(key, value);
String description = "%s attribute '%s'";
return abstractAssert.as(description, key.getType(), key.getKey());
}
private static AbstractAssert<? extends AbstractAssert<?, ?>, ?> makeAssertion(
AttributeKey<?> key, Object value) {
switch (key.getType()) {
case STRING:
return assertThat((String) value);

View File

@ -0,0 +1,28 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.testing.assertj;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import io.opentelemetry.api.common.AttributeKey;
import org.assertj.core.api.AbstractAssert;
import org.junit.jupiter.api.Test;
class AttributeAssertionTest {
@Test
void nullAttr_errorMessageContainsAttrName() {
AttributeKey<String> key = AttributeKey.stringKey("flib");
assertThatThrownBy(
() ->
AttributeAssertion.create(key, AbstractAssert::isNotNull)
.getAssertion()
.accept(AttributeAssertion.attributeValueAssertion(key, null)))
.isInstanceOf(AssertionError.class)
.hasMessage("[STRING attribute 'flib'] \nExpecting actual not to be null");
}
}