Skip to content

Commit 229264c

Browse files
authored
Convert JUnit assertions to AssertJ assertions (#2172)
1 parent 8ef63a6 commit 229264c

File tree

15 files changed

+194
-204
lines changed

15 files changed

+194
-204
lines changed

disk-buffering/src/test/java/io/opentelemetry/contrib/disk/buffering/internal/storage/files/utils/FileStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void truncateTop() throws IOException {
6161

6262
// Truncate all available data
6363
stream.truncateTop(3);
64-
assertThat(stream.size()).isEqualTo(0);
64+
assertThat(stream).isEmpty();
6565
assertThat(readString(temporaryFile)).isEqualTo("");
6666

6767
stream.close();

gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_NAME;
5353
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_TYPE;
5454
import static io.opentelemetry.semconv.incubating.K8sIncubatingAttributes.K8S_CLUSTER_NAME;
55-
import static org.junit.jupiter.api.Assertions.fail;
55+
import static org.assertj.core.api.Assertions.fail;
5656
import static org.mockito.Mockito.verify;
5757

5858
import com.google.cloud.opentelemetry.detection.DetectedPlatform;

jfr-connection/src/test/java/io/opentelemetry/contrib/jfr/connection/FlightRecorderDiagnosticCommandConnectionTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
package io.opentelemetry.contrib.jfr.connection;
77

8-
import static org.junit.jupiter.api.Assertions.assertEquals;
9-
import static org.junit.jupiter.api.Assertions.assertThrows;
10-
import static org.junit.jupiter.api.Assertions.fail;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
10+
import static org.assertj.core.api.Assertions.fail;
1111
import static org.mockito.ArgumentMatchers.any;
1212
import static org.mockito.ArgumentMatchers.anyString;
1313
import static org.mockito.Mockito.mock;
@@ -33,32 +33,32 @@ void assertCommercialFeaturesUnlocked() throws Exception {
3333

3434
@Test
3535
void assertCommercialFeaturesLockedThrows() throws Exception {
36-
assertThrows(
37-
JfrConnectionException.class,
38-
() -> {
39-
ObjectName objectName = mock(ObjectName.class);
40-
MBeanServerConnection mBeanServerConnection = mockMbeanServer(objectName, "locked");
41-
FlightRecorderDiagnosticCommandConnection.assertCommercialFeaturesUnlocked(
42-
mBeanServerConnection, objectName);
43-
});
36+
assertThatThrownBy(
37+
() -> {
38+
ObjectName objectName = mock(ObjectName.class);
39+
MBeanServerConnection mBeanServerConnection = mockMbeanServer(objectName, "locked");
40+
FlightRecorderDiagnosticCommandConnection.assertCommercialFeaturesUnlocked(
41+
mBeanServerConnection, objectName);
42+
})
43+
.isInstanceOf(JfrConnectionException.class);
4444
}
4545

4646
@Test
4747
void closeRecording() throws Exception {
48-
assertThrows(UnsupportedOperationException.class, () -> createconnection().closeRecording(1));
48+
assertThatThrownBy(() -> createconnection().closeRecording(1))
49+
.isInstanceOf(UnsupportedOperationException.class);
4950
}
5051

5152
@Test
5253
void testGetStream() throws Exception {
53-
assertThrows(
54-
UnsupportedOperationException.class,
55-
() -> createconnection().getStream(1L, null, null, 0L));
54+
assertThatThrownBy(() -> createconnection().getStream(1L, null, null, 0L))
55+
.isInstanceOf(UnsupportedOperationException.class);
5656
}
5757

5858
@Test
5959
void testCloneRecording() throws Exception {
60-
assertThrows(
61-
UnsupportedOperationException.class, () -> createconnection().cloneRecording(1, false));
60+
assertThatThrownBy(() -> createconnection().cloneRecording(1, false))
61+
.isInstanceOf(UnsupportedOperationException.class);
6262
}
6363

6464
@Test
@@ -73,7 +73,7 @@ void startRecordingParsesIdCorrectly() throws Exception {
7373
long id =
7474
connection.startRecording(
7575
new RecordingOptions.Builder().build(), RecordingConfiguration.PROFILE_CONFIGURATION);
76-
assertEquals(id, 99);
76+
assertThat(id).isEqualTo(99);
7777
}
7878

7979
@Test

jfr-connection/src/test/java/io/opentelemetry/contrib/jfr/connection/OpenDataUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.opentelemetry.contrib.jfr.connection;
77

8-
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.assertj.core.api.Assertions.assertThat;
99

1010
import java.lang.management.ManagementFactory;
1111
import java.util.HashMap;
@@ -49,6 +49,6 @@ void makeOpenData() throws Exception {
4949
mBeanServerConnection.invoke(
5050
objectInstance.getObjectName(), "getRecordingSettings", args, argTypes);
5151

52-
assertEquals(expected, actual);
52+
assertThat(actual).isEqualTo(expected);
5353
}
5454
}

jfr-connection/src/test/java/io/opentelemetry/contrib/jfr/connection/RecordingConfigurationTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
package io.opentelemetry.contrib.jfr.connection;
77

8-
import static org.junit.jupiter.api.Assertions.assertNotNull;
9-
import static org.junit.jupiter.api.Assertions.assertThrows;
10-
import static org.junit.jupiter.api.Assertions.assertTrue;
11-
import static org.junit.jupiter.api.Assertions.fail;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
10+
import static org.assertj.core.api.Assertions.fail;
1211

1312
import java.io.IOException;
1413
import java.nio.file.Files;
@@ -44,18 +43,20 @@ void tearDown() {
4443

4544
@Test
4645
void nullConfigThrows() {
47-
assertThrows(IllegalArgumentException.class, () -> new JfcFileConfiguration(null));
46+
assertThatThrownBy(() -> new JfcFileConfiguration(null))
47+
.isInstanceOf(IllegalArgumentException.class);
4848
}
4949

5050
@Test
5151
void brokenJfcConfigFileThrowsError() {
52-
assertThrows(RuntimeMBeanException.class, () -> executeRecording("brokenJfcFile.jfc"));
52+
assertThatThrownBy(() -> executeRecording("brokenJfcFile.jfc"))
53+
.isInstanceOf(RuntimeMBeanException.class);
5354
}
5455

5556
@Test
5657
void jfcFileFromInputStreamCanBeRead() {
5758
IItemCollection recordingContent = executeRecording("sampleJfcFile.jfc");
58-
assertTrue(containsEvent(recordingContent, "jdk.ThreadAllocationStatistics"));
59+
assertThat(containsEvent(recordingContent, "jdk.ThreadAllocationStatistics")).isTrue();
5960
}
6061

6162
@Test
@@ -68,9 +69,9 @@ void mapConfiguration() {
6869
RecordingConfiguration recordingConfiguration = new MapConfiguration(recordingConfigAsMap);
6970

7071
IItemCollection recordingContent = excecuteRecordingWithConfig(recordingConfiguration);
71-
assertNotNull(recordingContent, "excecuteRecordingWithConfig returned null");
72-
assertTrue(containsEvent(recordingContent, "jdk.ObjectAllocationInNewTLAB"));
73-
assertTrue(containsEvent(recordingContent, "jdk.ObjectAllocationOutsideTLAB"));
72+
assertThat(recordingContent).isNotNull();
73+
assertThat(containsEvent(recordingContent, "jdk.ObjectAllocationInNewTLAB")).isTrue();
74+
assertThat(containsEvent(recordingContent, "jdk.ObjectAllocationOutsideTLAB")).isTrue();
7475
}
7576

7677
private static boolean containsEvent(IItemCollection recordingContent, String eventName) {
@@ -110,7 +111,7 @@ private IItemCollection excecuteRecordingWithConfig(RecordingConfiguration confi
110111
}
111112
recording.stop();
112113
recording.dump(dumpFile.toString());
113-
assertTrue(Files.exists(dumpFile));
114+
assertThat(dumpFile).exists();
114115

115116
try {
116117
return JfrLoaderToolkit.loadEvents(dumpFile.toFile());

jfr-connection/src/test/java/io/opentelemetry/contrib/jfr/connection/RecordingOptionsTest.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
package io.opentelemetry.contrib.jfr.connection;
77

8-
import static org.junit.jupiter.api.Assertions.assertEquals;
9-
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1010

1111
import com.google.errorprone.annotations.Keep;
1212
import java.util.HashMap;
@@ -33,14 +33,14 @@ private static Stream<Arguments> testGetName() {
3333
@MethodSource
3434
void testGetName(String testValue, String expected) {
3535
RecordingOptions opts = new RecordingOptions.Builder().name(testValue).build();
36-
assertEquals(expected, opts.getName());
36+
assertThat(opts.getName()).isEqualTo(expected);
3737
}
3838

3939
@Test
4040
void testGetNameDefault() {
4141
String expected = "";
4242
RecordingOptions opts = new RecordingOptions.Builder().build();
43-
assertEquals(expected, opts.getName());
43+
assertThat(opts.getName()).isEqualTo(expected);
4444
}
4545

4646
@Keep
@@ -64,14 +64,14 @@ static Stream<Arguments> testGetMaxAge() {
6464
@MethodSource
6565
void testGetMaxAge(String testValue, String expected) {
6666
RecordingOptions opts = new RecordingOptions.Builder().maxAge(testValue).build();
67-
assertEquals(expected, opts.getMaxAge());
67+
assertThat(opts.getMaxAge()).isEqualTo(expected);
6868
}
6969

7070
@Test
7171
void testGetMaxAgeDefault() {
7272
String expected = "0";
7373
RecordingOptions opts = new RecordingOptions.Builder().build();
74-
assertEquals(expected, opts.getMaxAge());
74+
assertThat(opts.getMaxAge()).isEqualTo(expected);
7575
}
7676

7777
@Keep
@@ -91,9 +91,8 @@ private static Stream<Arguments> testGetMaxAgeNegative() {
9191
@ParameterizedTest
9292
@MethodSource
9393
void testGetMaxAgeNegative(String badValue) {
94-
assertThrows(
95-
IllegalArgumentException.class,
96-
() -> new RecordingOptions.Builder().maxAge(badValue).build());
94+
assertThatThrownBy(() -> new RecordingOptions.Builder().maxAge(badValue).build())
95+
.isInstanceOf(IllegalArgumentException.class);
9796
}
9897

9998
@Keep
@@ -113,14 +112,14 @@ private static Stream<Arguments> testGetMaxSize() {
113112
@MethodSource
114113
void testGetMaxSize(String testValue, String expected) {
115114
RecordingOptions opts = new RecordingOptions.Builder().maxSize(testValue).build();
116-
assertEquals(expected, opts.getMaxSize());
115+
assertThat(opts.getMaxSize()).isEqualTo(expected);
117116
}
118117

119118
@Test
120119
void testGetMaxSizeDefault() {
121120
String expected = "0";
122121
RecordingOptions opts = new RecordingOptions.Builder().build();
123-
assertEquals(expected, opts.getMaxSize());
122+
assertThat(opts.getMaxSize()).isEqualTo(expected);
124123
}
125124

126125
@Keep
@@ -135,30 +134,29 @@ private static Stream<Arguments> testGetMaxSizeNegative() {
135134
@ParameterizedTest
136135
@MethodSource
137136
void testGetMaxSizeNegative(String badValue) {
138-
assertThrows(
139-
IllegalArgumentException.class,
140-
() -> new RecordingOptions.Builder().maxSize(badValue).build());
137+
assertThatThrownBy(() -> new RecordingOptions.Builder().maxSize(badValue).build())
138+
.isInstanceOf(IllegalArgumentException.class);
141139
}
142140

143141
@Test
144142
void testGetDumpOnExit() {
145143
String expected = "true";
146144
RecordingOptions opts = new RecordingOptions.Builder().dumpOnExit(expected).build();
147-
assertEquals(expected, opts.getDumpOnExit());
145+
assertThat(opts.getDumpOnExit()).isEqualTo(expected);
148146
}
149147

150148
@Test
151149
void testGetDumpOnExitDefault() {
152150
String expected = "false";
153151
RecordingOptions opts = new RecordingOptions.Builder().build();
154-
assertEquals(expected, opts.getDumpOnExit());
152+
assertThat(opts.getDumpOnExit()).isEqualTo(expected);
155153
}
156154

157155
@Test
158156
void testGetDumpOnExitBadValue() {
159157
String expected = "false";
160158
RecordingOptions opts = new RecordingOptions.Builder().dumpOnExit("BAD_VALUE").build();
161-
assertEquals(expected, opts.getDumpOnExit());
159+
assertThat(opts.getDumpOnExit()).isEqualTo(expected);
162160
}
163161

164162
@Keep
@@ -175,35 +173,35 @@ private static Stream<Arguments> testGetDestination() {
175173
@MethodSource
176174
void testGetDestination(String testValue, String expected) {
177175
RecordingOptions opts = new RecordingOptions.Builder().destination(testValue).build();
178-
assertEquals(expected, opts.getDestination());
176+
assertThat(opts.getDestination()).isEqualTo(expected);
179177
}
180178

181179
@Test
182180
void testGetDestinationDefault() {
183181
String expected = "";
184182
RecordingOptions opts = new RecordingOptions.Builder().build();
185-
assertEquals(expected, opts.getDestination());
183+
assertThat(opts.getDestination()).isEqualTo(expected);
186184
}
187185

188186
@Test
189187
void testGetDisk() {
190188
String expected = "true";
191189
RecordingOptions opts = new RecordingOptions.Builder().disk(expected).build();
192-
assertEquals(expected, opts.getDisk());
190+
assertThat(opts.getDisk()).isEqualTo(expected);
193191
}
194192

195193
@Test
196194
void testGetDiskDefault() {
197195
String expected = "false";
198196
RecordingOptions opts = new RecordingOptions.Builder().build();
199-
assertEquals(expected, opts.getDisk());
197+
assertThat(opts.getDisk()).isEqualTo(expected);
200198
}
201199

202200
@Test
203201
void testGetDiskBadValue() {
204202
String expected = "false";
205203
RecordingOptions opts = new RecordingOptions.Builder().disk("BAD_VALUE").build();
206-
assertEquals(expected, opts.getDisk());
204+
assertThat(opts.getDisk()).isEqualTo(expected);
207205
}
208206

209207
@Keep
@@ -227,14 +225,14 @@ private static Stream<Arguments> testGetDuration() {
227225
@MethodSource
228226
void testGetDuration(String testValue, String expected) {
229227
RecordingOptions opts = new RecordingOptions.Builder().duration(testValue).build();
230-
assertEquals(expected, opts.getDuration());
228+
assertThat(opts.getDuration()).isEqualTo(expected);
231229
}
232230

233231
@Test
234232
void testGetDurationDefault() {
235233
String expected = "0";
236234
RecordingOptions opts = new RecordingOptions.Builder().build();
237-
assertEquals(expected, opts.getDuration());
235+
assertThat(opts.getDuration()).isEqualTo(expected);
238236
}
239237

240238
@Keep
@@ -254,9 +252,8 @@ private static Stream<Arguments> testGetDurationNegative() {
254252
@ParameterizedTest
255253
@MethodSource
256254
void testGetDurationNegative(String badValue) {
257-
assertThrows(
258-
IllegalArgumentException.class,
259-
() -> new RecordingOptions.Builder().duration(badValue).build());
255+
assertThatThrownBy(() -> new RecordingOptions.Builder().duration(badValue).build())
256+
.isInstanceOf(IllegalArgumentException.class);
260257
}
261258

262259
@Test
@@ -279,7 +276,7 @@ void testGetRecordingOptions() {
279276
.disk("true")
280277
.duration("120 s")
281278
.build();
282-
assertEquals(expected, opts.getRecordingOptions());
279+
assertThat(opts.getRecordingOptions()).isEqualTo(expected);
283280
}
284281

285282
@Test
@@ -289,6 +286,6 @@ void testGetRecordingOptionsDefaults() {
289286
// to insure consistent behaviour.
290287
expected.put("disk", "false");
291288
RecordingOptions opts = new RecordingOptions.Builder().build();
292-
assertEquals(expected, opts.getRecordingOptions());
289+
assertThat(opts.getRecordingOptions()).isEqualTo(expected);
293290
}
294291
}

0 commit comments

Comments
 (0)