Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit ceca5f7

Browse files
committed
chore: migrate tests to JUnit 5
1 parent 08e590a commit ceca5f7

46 files changed

Lines changed: 1406 additions & 1410 deletions

Some content is hidden

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

google-cloud-logging/pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,18 @@
109109

110110
<!-- Test dependencies -->
111111
<dependency>
112-
<groupId>junit</groupId>
113-
<artifactId>junit</artifactId>
112+
<groupId>org.junit.jupiter</groupId>
113+
<artifactId>junit-jupiter-api</artifactId>
114+
<scope>test</scope>
115+
</dependency>
116+
<dependency>
117+
<groupId>org.junit.jupiter</groupId>
118+
<artifactId>junit-jupiter-engine</artifactId>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.junit.jupiter</groupId>
123+
<artifactId>junit-jupiter-params</artifactId>
114124
<scope>test</scope>
115125
</dependency>
116126
<dependency>

google-cloud-logging/src/test/java/com/google/cloud/logging/AutoPopulateMetadataTests.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.easymock.EasyMock.expect;
2323
import static org.easymock.EasyMock.newCapture;
2424
import static org.easymock.EasyMock.replay;
25-
import static org.junit.Assert.*;
25+
import static org.junit.jupiter.api.Assertions.*;
2626

2727
import com.google.api.core.ApiFutures;
2828
import com.google.cloud.MonitoredResource;
@@ -34,14 +34,11 @@
3434
import com.google.logging.v2.WriteLogEntriesRequest;
3535
import com.google.logging.v2.WriteLogEntriesResponse;
3636
import org.easymock.Capture;
37-
import org.junit.After;
38-
import org.junit.Before;
39-
import org.junit.Test;
40-
import org.junit.runner.RunWith;
41-
import org.junit.runners.JUnit4;
37+
import org.junit.jupiter.api.AfterEach;
38+
import org.junit.jupiter.api.BeforeEach;
39+
import org.junit.jupiter.api.Test;
4240

43-
@RunWith(JUnit4.class)
44-
public class AutoPopulateMetadataTests {
41+
class AutoPopulateMetadataTests {
4542

4643
private static final String LOG_NAME = "test-log";
4744
private static final String RESOURCE_PROJECT_ID = "env-project-id";
@@ -81,8 +78,8 @@ public class AutoPopulateMetadataTests {
8178
private final Capture<WriteLogEntriesRequest> rpcWriteArgument = newCapture();
8279
private ResourceTypeEnvironmentGetter mockedEnvGetter;
8380

84-
@Before
85-
public void setup() {
81+
@BeforeEach
82+
void setup() {
8683
mockedEnvGetter = createMock(ResourceTypeEnvironmentGetter.class);
8784
mockedRpcFactory = createMock(LoggingRpcFactory.class);
8885
mockedRpc = createMock(LoggingRpc.class);
@@ -106,8 +103,8 @@ public void setup() {
106103
logging = options.getService();
107104
}
108105

109-
@After
110-
public void teardown() {
106+
@AfterEach
107+
void teardown() {
111108
new ContextHandler().removeCurrentContext();
112109
}
113110

@@ -124,7 +121,7 @@ private void mockCurrentContext(
124121
}
125122

126123
@Test
127-
public void testAutoPopulationEnabledInLoggingOptions() {
124+
void testAutoPopulationEnabledInLoggingOptions() {
128125
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
129126

130127
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY));
@@ -138,7 +135,7 @@ public void testAutoPopulationEnabledInLoggingOptions() {
138135
}
139136

140137
@Test
141-
public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions() {
138+
void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions() {
142139
// redefine logging option to opt out auto-populating
143140
LoggingOptions options =
144141
logging.getOptions().toBuilder().setAutoPopulateMetadata(false).build();
@@ -156,7 +153,7 @@ public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions()
156153
}
157154

158155
@Test
159-
public void testAutoPopulationDisabledInWriteOptions() {
156+
void testAutoPopulationDisabledInWriteOptions() {
160157
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
161158

162159
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(false));
@@ -170,7 +167,7 @@ public void testAutoPopulationDisabledInWriteOptions() {
170167
}
171168

172169
@Test
173-
public void testSourceLocationPopulation() {
170+
void testSourceLocationPopulation() {
174171
SourceLocation expected = SourceLocation.fromCurrentContext();
175172
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY_WITH_DEBUG));
176173

@@ -182,7 +179,7 @@ public void testSourceLocationPopulation() {
182179
}
183180

184181
@Test
185-
public void testNotFormattedTraceId() {
182+
void testNotFormattedTraceId() {
186183
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
187184

188185
final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build();
@@ -194,7 +191,7 @@ public void testNotFormattedTraceId() {
194191
}
195192

196193
@Test
197-
public void testMonitoredResourcePopulationInWriteOptions() {
194+
void testMonitoredResourcePopulationInWriteOptions() {
198195
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
199196

200197
final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build();

google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,30 @@
2626
import java.time.format.DateTimeFormatter;
2727
import java.util.Calendar;
2828
import java.util.Iterator;
29-
import org.junit.AfterClass;
30-
import org.junit.BeforeClass;
31-
import org.junit.Rule;
32-
import org.junit.rules.Timeout;
29+
import java.util.concurrent.TimeUnit;
30+
import org.junit.jupiter.api.AfterAll;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Timeout;
3333

3434
/**
3535
* A base class for system tests. This class can be extended to run system tests in different
3636
* environments (e.g. local emulator or remote Logging service).
3737
*/
38+
@Timeout(value = 600, unit = TimeUnit.SECONDS)
3839
public class BaseSystemTest {
3940

40-
@Rule public Timeout globalTimeout = Timeout.seconds(600);
41-
4241
private static final DateTimeFormatter RFC_3339 =
4342
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
4443

4544
protected static Logging logging;
4645

47-
@BeforeClass
46+
@BeforeAll
4847
public static void beforeClass() {
4948
RemoteLoggingHelper helper = RemoteLoggingHelper.create();
5049
logging = helper.getOptions().getService();
5150
}
5251

53-
@AfterClass
52+
@AfterAll
5453
public static void afterClass() throws Exception {
5554
logging.close();
5655
}

google-cloud-logging/src/test/java/com/google/cloud/logging/ContextHandlerTest.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717
package com.google.cloud.logging;
1818

19-
import static org.junit.Assert.*;
19+
import static org.junit.jupiter.api.Assertions.*;
2020

2121
import com.google.cloud.logging.ContextHandler.ContextPriority;
2222
import com.google.cloud.logging.HttpRequest.RequestMethod;
23-
import org.junit.After;
24-
import org.junit.Test;
25-
import org.junit.runner.RunWith;
26-
import org.junit.runners.JUnit4;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.Test;
2725

28-
@RunWith(JUnit4.class)
29-
public class ContextHandlerTest {
26+
class ContextHandlerTest {
3027
private static final HttpRequest OLD_HTTP_REQUEST =
3128
HttpRequest.newBuilder()
3229
.setRequestMethod(RequestMethod.POST)
@@ -46,14 +43,14 @@ public class ContextHandlerTest {
4643
private static final String SPAN_ID = "1";
4744
private static final boolean TRACE_SAMPLED = true;
4845

49-
@After
50-
public void teardown() {
46+
@AfterEach
47+
void teardown() {
5148
new ContextHandler().removeCurrentContext();
5249
new ContextHandler().removeCurrentContextPriority();
5350
}
5451

5552
@Test
56-
public void testDefaultSetContext() {
53+
void testDefaultSetContext() {
5754
Context newContext =
5855
Context.newBuilder()
5956
.setRequest(HTTP_REQUEST)
@@ -71,7 +68,7 @@ public void testDefaultSetContext() {
7168
}
7269

7370
@Test
74-
public void testSetContextWithPriorityFromNoInput() {
71+
void testSetContextWithPriorityFromNoInput() {
7572
Context newContext =
7673
Context.newBuilder()
7774
.setRequest(HTTP_REQUEST)
@@ -89,7 +86,7 @@ public void testSetContextWithPriorityFromNoInput() {
8986
}
9087

9188
@Test
92-
public void testSetContextWithPriorityFromW3CHeader() {
89+
void testSetContextWithPriorityFromW3CHeader() {
9390
Context newContext =
9491
Context.newBuilder()
9592
.setRequest(HTTP_REQUEST)
@@ -107,7 +104,7 @@ public void testSetContextWithPriorityFromW3CHeader() {
107104
}
108105

109106
@Test
110-
public void testSetContextFromXCloudHeader() {
107+
void testSetContextFromXCloudHeader() {
111108
Context newContext =
112109
Context.newBuilder()
113110
.setRequest(HTTP_REQUEST)
@@ -125,7 +122,7 @@ public void testSetContextFromXCloudHeader() {
125122
}
126123

127124
@Test
128-
public void testSetContextFromOpenTelemetry() {
125+
void testSetContextFromOpenTelemetry() {
129126
Context newContext =
130127
Context.newBuilder()
131128
.setRequest(HTTP_REQUEST)
@@ -143,7 +140,7 @@ public void testSetContextFromOpenTelemetry() {
143140
}
144141

145142
@Test
146-
public void testOverrideW3CContextFromOpenTelemetry() {
143+
void testOverrideW3CContextFromOpenTelemetry() {
147144
Context oldContext =
148145
Context.newBuilder()
149146
.setRequest(OLD_HTTP_REQUEST)
@@ -170,7 +167,7 @@ public void testOverrideW3CContextFromOpenTelemetry() {
170167
}
171168

172169
@Test
173-
public void testOverrideXCTCContextFromOpenTelemetry() {
170+
void testOverrideXCTCContextFromOpenTelemetry() {
174171
Context oldContext =
175172
Context.newBuilder()
176173
.setRequest(OLD_HTTP_REQUEST)
@@ -198,7 +195,7 @@ public void testOverrideXCTCContextFromOpenTelemetry() {
198195
}
199196

200197
@Test
201-
public void testOverrideOtelContextFromDefaultSetContext() {
198+
void testOverrideOtelContextFromDefaultSetContext() {
202199
Context oldContext =
203200
Context.newBuilder()
204201
.setRequest(OLD_HTTP_REQUEST)
@@ -225,7 +222,7 @@ public void testOverrideOtelContextFromDefaultSetContext() {
225222
}
226223

227224
@Test
228-
public void testOverrideOtelContextFromW3C() {
225+
void testOverrideOtelContextFromW3C() {
229226
Context oldContext =
230227
Context.newBuilder()
231228
.setRequest(OLD_HTTP_REQUEST)
@@ -252,7 +249,7 @@ public void testOverrideOtelContextFromW3C() {
252249
}
253250

254251
@Test
255-
public void testOverrideOtelContextFromXCTC() {
252+
void testOverrideOtelContextFromXCTC() {
256253
Context oldContext =
257254
Context.newBuilder()
258255
.setRequest(OLD_HTTP_REQUEST)

google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.google.cloud.logging;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotEquals;
22-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
2323

2424
import io.opentelemetry.api.trace.*;
2525
import io.opentelemetry.api.trace.Span;
@@ -30,12 +30,9 @@
3030
import io.opentelemetry.sdk.trace.SpanProcessor;
3131
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
3232
import java.time.Duration;
33-
import org.junit.Test;
34-
import org.junit.runner.RunWith;
35-
import org.junit.runners.JUnit4;
33+
import org.junit.jupiter.api.Test;
3634

37-
@RunWith(JUnit4.class)
38-
public class ContextTest {
35+
class ContextTest {
3936

4037
private static final HttpRequest.RequestMethod REQUEST_METHOD = HttpRequest.RequestMethod.GET;
4138
private static final String REQUEST_URL = "https://test.domain?arg=val";
@@ -81,7 +78,7 @@ public class ContextTest {
8178
.build();
8279

8380
@Test
84-
public void testCompareContexts() {
81+
void testCompareContexts() {
8582
Context context1 =
8683
Context.newBuilder()
8784
.setRequest(REQUEST)
@@ -107,7 +104,7 @@ public void testCompareContexts() {
107104
}
108105

109106
@Test
110-
public void testContextBuilder() {
107+
void testContextBuilder() {
111108
Context emptyContext = Context.newBuilder().build();
112109
Context anotherContext = TEST_CONTEXT.toBuilder().build();
113110

@@ -123,7 +120,7 @@ public void testContextBuilder() {
123120
}
124121

125122
@Test
126-
public void testParsingCloudTraceContext() {
123+
void testParsingCloudTraceContext() {
127124
final String X_CLOUD_TRACE_NO_TRACE = "/SPAN_ID;o=TRACE_TRUE";
128125
final String X_CLOUD_TRACE_ONLY = TEST_TRACE_ID;
129126
final String X_CLOUD_TRACE_WITH_SPAN = TEST_TRACE_ID + "/" + TEST_SPAN_ID;
@@ -146,7 +143,7 @@ public void testParsingCloudTraceContext() {
146143
}
147144

148145
@Test
149-
public void testParsingW3CTraceParent() {
146+
void testParsingW3CTraceParent() {
150147
final String W3C_TEST_TRACE_ID = "12345678901234567890123456789012";
151148
final String W3C_TEST_SPAN_ID = "1234567890123456";
152149
final String W3C_TEST_TRACE_SAMPLED = "0f";
@@ -162,7 +159,7 @@ public void testParsingW3CTraceParent() {
162159
}
163160

164161
@Test
165-
public void testParsingOpenTelemetryContext() {
162+
void testParsingOpenTelemetryContext() {
166163
InMemorySpanExporter testExporter = InMemorySpanExporter.create();
167164
SpanProcessor inMemorySpanProcessor = SimpleSpanProcessor.create(testExporter);
168165
OpenTelemetrySdk openTelemetrySdk =

0 commit comments

Comments
 (0)