Skip to content

Commit 79a601d

Browse files
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 <[email protected]> * fix build Co-authored-by: jack-berg <[email protected]>
1 parent ea50096 commit 79a601d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

sdk/testing/src/main/java/io/opentelemetry/sdk/testing/assertj/AttributeAssertion.java

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ static AttributeAssertion create(
3737
// suppressing here.
3838
@SuppressWarnings("NullAway")
3939
static AbstractAssert<?, ?> attributeValueAssertion(AttributeKey<?> key, @Nullable Object value) {
40+
AbstractAssert<? extends AbstractAssert<?, ?>, ?> abstractAssert = makeAssertion(key, value);
41+
String description = "%s attribute '%s'";
42+
return abstractAssert.as(description, key.getType(), key.getKey());
43+
}
44+
45+
private static AbstractAssert<? extends AbstractAssert<?, ?>, ?> makeAssertion(
46+
AttributeKey<?> key, Object value) {
4047
switch (key.getType()) {
4148
case STRING:
4249
return assertThat((String) value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.testing.assertj;
7+
8+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
9+
10+
import io.opentelemetry.api.common.AttributeKey;
11+
import org.assertj.core.api.AbstractAssert;
12+
import org.junit.jupiter.api.Test;
13+
14+
class AttributeAssertionTest {
15+
16+
@Test
17+
void nullAttr_errorMessageContainsAttrName() {
18+
AttributeKey<String> key = AttributeKey.stringKey("flib");
19+
20+
assertThatThrownBy(
21+
() ->
22+
AttributeAssertion.create(key, AbstractAssert::isNotNull)
23+
.getAssertion()
24+
.accept(AttributeAssertion.attributeValueAssertion(key, null)))
25+
.isInstanceOf(AssertionError.class)
26+
.hasMessage("[STRING attribute 'flib'] \nExpecting actual not to be null");
27+
}
28+
}

0 commit comments

Comments
 (0)