Skip to content

Commit 6dffbb8

Browse files
author
Anuraag Agrawal
authored
Migrate tests to JUnit5 (open-telemetry#1489)
* Add JUnit 5 dependency * Revert unintended
1 parent a024cca commit 6dffbb8

185 files changed

Lines changed: 2679 additions & 3097 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

all/src/test/java/io/opentelemetry/InternalApiProtectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
import com.tngtech.archunit.core.domain.JavaClasses;
2222
import com.tngtech.archunit.core.importer.ClassFileImporter;
2323
import com.tngtech.archunit.lang.syntax.elements.ClassesShouldConjunction;
24-
import org.junit.Test;
24+
import org.junit.jupiter.api.Test;
2525

26-
public class InternalApiProtectionTest {
26+
class InternalApiProtectionTest {
2727

2828
private static final String OTEL_BASE_PACKAGE = "io.opentelemetry";
2929
private static final JavaClasses ALL_OTEL_CLASSES =
3030
new ClassFileImporter().importPackages(OTEL_BASE_PACKAGE);
3131

3232
@Test
33-
public void contrib_should_not_use_internal_api() {
33+
void contrib_should_not_use_internal_api() {
3434
ClassesShouldConjunction contribRule =
3535
noClasses()
3636
.that()

api/src/test/java/io/opentelemetry/OpenTelemetryTest.java

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package io.opentelemetry;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static org.junit.Assert.assertTrue;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

2223
import io.opentelemetry.context.Scope;
2324
import io.opentelemetry.context.propagation.ContextPropagators;
@@ -54,34 +55,27 @@
5455
import java.io.Writer;
5556
import java.net.URL;
5657
import javax.annotation.Nullable;
57-
import org.junit.After;
58-
import org.junit.BeforeClass;
59-
import org.junit.Rule;
60-
import org.junit.Test;
61-
import org.junit.rules.ExpectedException;
62-
import org.junit.runner.RunWith;
63-
import org.junit.runners.JUnit4;
58+
import org.junit.jupiter.api.AfterEach;
59+
import org.junit.jupiter.api.BeforeAll;
60+
import org.junit.jupiter.api.Test;
6461

65-
@RunWith(JUnit4.class)
66-
public class OpenTelemetryTest {
62+
class OpenTelemetryTest {
6763

68-
@Rule public final ExpectedException thrown = ExpectedException.none();
69-
70-
@BeforeClass
71-
public static void beforeClass() {
64+
@BeforeAll
65+
static void beforeClass() {
7266
OpenTelemetry.reset();
7367
}
7468

75-
@After
76-
public void after() {
69+
@AfterEach
70+
void after() {
7771
OpenTelemetry.reset();
7872
System.clearProperty(TracerProviderFactory.class.getName());
7973
System.clearProperty(MeterProviderFactory.class.getName());
8074
System.clearProperty(CorrelationContextManagerFactory.class.getName());
8175
}
8276

8377
@Test
84-
public void testDefault() {
78+
void testDefault() {
8579
assertThat(OpenTelemetry.getTracerProvider()).isInstanceOf(DefaultTracerProvider.class);
8680
assertThat(OpenTelemetry.getTracerProvider())
8781
.isSameInstanceAs(OpenTelemetry.getTracerProvider());
@@ -96,7 +90,7 @@ public void testDefault() {
9690
}
9791

9892
@Test
99-
public void testTracerLoadArbitrary() throws IOException {
93+
void testTracerLoadArbitrary() throws IOException {
10094
File serviceFile =
10195
createService(
10296
TracerProviderFactory.class,
@@ -113,7 +107,7 @@ public void testTracerLoadArbitrary() throws IOException {
113107
}
114108

115109
@Test
116-
public void testTracerSystemProperty() throws IOException {
110+
void testTracerSystemProperty() throws IOException {
117111
File serviceFile =
118112
createService(
119113
TracerProviderFactory.class,
@@ -130,14 +124,13 @@ public void testTracerSystemProperty() throws IOException {
130124
}
131125

132126
@Test
133-
public void testTracerNotFound() {
127+
void testTracerNotFound() {
134128
System.setProperty(TracerProviderFactory.class.getName(), "io.does.not.exists");
135-
thrown.expect(IllegalStateException.class);
136-
OpenTelemetry.getTracer("testTracer");
129+
assertThrows(IllegalStateException.class, () -> OpenTelemetry.getTracer("testTracer"));
137130
}
138131

139132
@Test
140-
public void testMeterLoadArbitrary() throws IOException {
133+
void testMeterLoadArbitrary() throws IOException {
141134
File serviceFile =
142135
createService(
143136
MeterProviderFactory.class,
@@ -154,7 +147,7 @@ public void testMeterLoadArbitrary() throws IOException {
154147
}
155148

156149
@Test
157-
public void testMeterSystemProperty() throws IOException {
150+
void testMeterSystemProperty() throws IOException {
158151
File serviceFile =
159152
createService(
160153
MeterProviderFactory.class,
@@ -171,14 +164,13 @@ public void testMeterSystemProperty() throws IOException {
171164
}
172165

173166
@Test
174-
public void testMeterNotFound() {
167+
void testMeterNotFound() {
175168
System.setProperty(MeterProviderFactory.class.getName(), "io.does.not.exists");
176-
thrown.expect(IllegalStateException.class);
177-
OpenTelemetry.getMeterProvider();
169+
assertThrows(IllegalStateException.class, () -> OpenTelemetry.getMeterProvider());
178170
}
179171

180172
@Test
181-
public void testCorrelationContextManagerLoadArbitrary() throws IOException {
173+
void testCorrelationContextManagerLoadArbitrary() throws IOException {
182174
File serviceFile =
183175
createService(
184176
CorrelationContextManagerFactory.class,
@@ -197,7 +189,7 @@ public void testCorrelationContextManagerLoadArbitrary() throws IOException {
197189
}
198190

199191
@Test
200-
public void testCorrelationContextManagerSystemProperty() throws IOException {
192+
void testCorrelationContextManagerSystemProperty() throws IOException {
201193
File serviceFile =
202194
createService(
203195
CorrelationContextManagerFactory.class,
@@ -217,23 +209,21 @@ public void testCorrelationContextManagerSystemProperty() throws IOException {
217209
}
218210

219211
@Test
220-
public void testCorrelationContextManagerNotFound() {
212+
void testCorrelationContextManagerNotFound() {
221213
System.setProperty(CorrelationContextManagerFactory.class.getName(), "io.does.not.exists");
222-
thrown.expect(IllegalStateException.class);
223-
OpenTelemetry.getCorrelationContextManager();
214+
assertThrows(IllegalStateException.class, () -> OpenTelemetry.getCorrelationContextManager());
224215
}
225216

226217
@Test
227-
public void testPropagatorsSet() {
218+
void testPropagatorsSet() {
228219
ContextPropagators propagators = DefaultContextPropagators.builder().build();
229220
OpenTelemetry.setPropagators(propagators);
230221
assertThat(OpenTelemetry.getPropagators()).isEqualTo(propagators);
231222
}
232223

233224
@Test
234-
public void testPropagatorsSetNull() {
235-
thrown.expect(NullPointerException.class);
236-
OpenTelemetry.setPropagators(null);
225+
void testPropagatorsSetNull() {
226+
assertThrows(NullPointerException.class, () -> OpenTelemetry.setPropagators(null));
237227
}
238228

239229
private static File createService(Class<?> service, Class<?>... impls) throws IOException {

api/src/test/java/io/opentelemetry/common/AttributeValueTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020

2121
import com.google.common.testing.EqualsTester;
22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
24-
import org.junit.runners.JUnit4;
22+
import org.junit.jupiter.api.Test;
2523

26-
/** Unit tests for {@link AttributeValue}. */
27-
@RunWith(JUnit4.class)
28-
public class AttributeValueTest {
24+
class AttributeValueTest {
2925

3026
@Test
31-
public void attributeValue_EqualsAndHashCode() {
27+
void attributeValue_EqualsAndHashCode() {
3228
EqualsTester tester = new EqualsTester();
3329
tester.addEqualityGroup(
3430
AttributeValue.stringAttributeValue("MyStringAttributeValue"),
@@ -65,7 +61,7 @@ public void attributeValue_EqualsAndHashCode() {
6561
}
6662

6763
@Test
68-
public void doNotCrashOnNull() {
64+
void doNotCrashOnNull() {
6965
AttributeValue.stringAttributeValue(null);
7066
AttributeValue.arrayAttributeValue((String[]) null);
7167
AttributeValue.arrayAttributeValue((Boolean[]) null);
@@ -74,7 +70,7 @@ public void doNotCrashOnNull() {
7470
}
7571

7672
@Test
77-
public void attributeValue_ToString() {
73+
void attributeValue_ToString() {
7874
AttributeValue attribute = AttributeValue.stringAttributeValue("MyStringAttributeValue");
7975
assertThat(attribute.toString()).contains("MyStringAttributeValue");
8076
attribute = AttributeValue.booleanAttributeValue(true);
@@ -100,7 +96,7 @@ public void attributeValue_ToString() {
10096
}
10197

10298
@Test
103-
public void arrayAttributeValue_nullValuesWithinArray() {
99+
void arrayAttributeValue_nullValuesWithinArray() {
104100
AttributeValue attribute;
105101

106102
attribute = AttributeValue.arrayAttributeValue("string", null, "", "string");

api/src/test/java/io/opentelemetry/common/AttributesTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@
2626
import java.util.HashMap;
2727
import java.util.Map;
2828
import java.util.concurrent.atomic.AtomicBoolean;
29-
import org.junit.Rule;
30-
import org.junit.Test;
31-
import org.junit.rules.ExpectedException;
29+
import org.junit.jupiter.api.Test;
3230

3331
/** Unit tests for {@link Attributes}s. */
34-
public class AttributesTest {
35-
@Rule public final ExpectedException thrown = ExpectedException.none();
32+
class AttributesTest {
3633

3734
@Test
38-
public void forEach() {
35+
void forEach() {
3936
final Map<String, AttributeValue> entriesSeen = new HashMap<>();
4037

4138
Attributes attributes =
@@ -50,7 +47,7 @@ public void forEach() {
5047
}
5148

5249
@Test
53-
public void forEach_singleAttribute() {
50+
void forEach_singleAttribute() {
5451
final Map<String, AttributeValue> entriesSeen = new HashMap<>();
5552

5653
Attributes attributes = Attributes.of("key", stringAttributeValue("value"));
@@ -59,15 +56,15 @@ public void forEach_singleAttribute() {
5956
}
6057

6158
@Test
62-
public void forEach_empty() {
59+
void forEach_empty() {
6360
final AtomicBoolean sawSomething = new AtomicBoolean(false);
6461
Attributes emptyAttributes = Attributes.empty();
6562
emptyAttributes.forEach((key, value) -> sawSomething.set(true));
6663
assertThat(sawSomething.get()).isFalse();
6764
}
6865

6966
@Test
70-
public void orderIndependentEquality() {
67+
void orderIndependentEquality() {
7168
Attributes one =
7269
Attributes.of(
7370
"key1", stringAttributeValue("value1"),
@@ -103,7 +100,7 @@ public void orderIndependentEquality() {
103100
}
104101

105102
@Test
106-
public void deduplication() {
103+
void deduplication() {
107104
Attributes one =
108105
Attributes.of(
109106
"key1", stringAttributeValue("value1"),
@@ -114,15 +111,15 @@ public void deduplication() {
114111
}
115112

116113
@Test
117-
public void emptyAndNullKey() {
114+
void emptyAndNullKey() {
118115
Attributes noAttributes =
119116
Attributes.of("", stringAttributeValue("empty"), null, stringAttributeValue("null"));
120117

121118
assertThat(noAttributes.size()).isEqualTo(0);
122119
}
123120

124121
@Test
125-
public void builder() {
122+
void builder() {
126123
Attributes attributes =
127124
Attributes.newBuilder()
128125
.setAttribute("string", "value1")
@@ -142,7 +139,7 @@ public void builder() {
142139
}
143140

144141
@Test
145-
public void builder_arrayTypes() {
142+
void builder_arrayTypes() {
146143
Attributes attributes =
147144
Attributes.newBuilder()
148145
.setAttribute("string", "value1", "value2")
@@ -163,13 +160,13 @@ public void builder_arrayTypes() {
163160
}
164161

165162
@Test
166-
public void get_Null() {
163+
void get_Null() {
167164
assertThat(Attributes.empty().get("foo")).isNull();
168165
assertThat(Attributes.of("key", stringAttributeValue("value")).get("foo")).isNull();
169166
}
170167

171168
@Test
172-
public void get() {
169+
void get() {
173170
assertThat(Attributes.of("key", stringAttributeValue("value")).get("key"))
174171
.isEqualTo(stringAttributeValue("value"));
175172
assertThat(Attributes.of("key", stringAttributeValue("value")).get("value")).isNull();

api/src/test/java/io/opentelemetry/common/ImmutableKeyValuePairsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
import java.util.Arrays;
2323
import java.util.Collections;
2424
import java.util.List;
25-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
26+
27+
class ImmutableKeyValuePairsTest {
2628

27-
public class ImmutableKeyValuePairsTest {
2829
@Test
29-
public void toStringIsCorrect() {
30+
void toStringIsCorrect() {
3031
assertThat(new TestPairs(Collections.emptyList()).toString()).isEqualTo("{}");
3132
assertThat(new TestPairs(Arrays.asList("one", 55)).toString()).isEqualTo("{one=55}");
3233
assertThat(new TestPairs(Arrays.asList("one", 55, "two", "b")).toString())

0 commit comments

Comments
 (0)