Skip to content

Commit 44847bf

Browse files
authored
Upgrade JUnit to 4.13.2
ExpectedException is deprecated, so I fixed the new warnings. However, we are still using ExpectedException many places and had previously supressed the warning. See #7467 . I did not fix those existing instances that had suppressed the warning, since it is unrelated to the upgrade and we have been free to fix them at any time since we dropped Java 7.
1 parent 5a2c94b commit 44847bf

File tree

11 files changed

+35
-44
lines changed

11 files changed

+35
-44
lines changed

core/src/test/java/io/grpc/internal/ReadableBuffersTest.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Charsets.UTF_8;
2020
import static org.junit.Assert.assertArrayEquals;
2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThrows;
2223
import static org.junit.Assert.assertTrue;
2324
import static org.mockito.ArgumentMatchers.anyInt;
2425
import static org.mockito.Mockito.mock;
@@ -32,9 +33,7 @@
3233
import java.io.IOException;
3334
import java.io.InputStream;
3435
import java.nio.InvalidMarkException;
35-
import org.junit.Rule;
3636
import org.junit.Test;
37-
import org.junit.rules.ExpectedException;
3837
import org.junit.runner.RunWith;
3938
import org.junit.runners.JUnit4;
4039

@@ -46,9 +45,6 @@
4645
public class ReadableBuffersTest {
4746
private static final byte[] MSG_BYTES = "hello".getBytes(UTF_8);
4847

49-
@Rule
50-
public final ExpectedException thrown = ExpectedException.none();
51-
5248
@Test
5349
public void empty_returnsEmptyBuffer() {
5450
ReadableBuffer buffer = ReadableBuffers.empty();
@@ -216,8 +212,7 @@ public void bufferInputStream_markDiscardedAfterDetached() throws IOException {
216212
InputStream inputStream = ReadableBuffers.openStream(buffer, true);
217213
inputStream.mark(5);
218214
((Detachable) inputStream).detach();
219-
thrown.expect(InvalidMarkException.class);
220-
inputStream.reset();
215+
assertThrows(InvalidMarkException.class, () -> inputStream.reset());
221216
}
222217

223218
@Test

examples/android/clientcache/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ dependencies {
5959
implementation 'io.grpc:grpc-stub:1.53.0-SNAPSHOT' // CURRENT_GRPC_VERSION
6060
implementation 'org.apache.tomcat:annotations-api:6.0.53'
6161

62-
testImplementation 'junit:junit:4.12'
62+
testImplementation 'junit:junit:4.13.2'
6363
testImplementation 'com.google.truth:truth:1.0.1'
6464
testImplementation 'io.grpc:grpc-testing:1.53.0-SNAPSHOT' // CURRENT_GRPC_VERSION
6565
}

examples/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies {
3636
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
3737

3838
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
39-
testImplementation "junit:junit:4.12"
39+
testImplementation "junit:junit:4.13.2"
4040
testImplementation "org.mockito:mockito-core:3.4.0"
4141
}
4242

examples/example-gauth/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<dependency>
7474
<groupId>junit</groupId>
7575
<artifactId>junit</artifactId>
76-
<version>4.12</version>
76+
<version>4.13.2</version>
7777
<scope>test</scope>
7878
</dependency>
7979
<dependency>

examples/example-hostname/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
compileOnly "org.apache.tomcat:annotations-api:6.0.53"
3232
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
3333

34-
testImplementation 'junit:junit:4.12'
34+
testImplementation 'junit:junit:4.13.2'
3535
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
3636
}
3737

examples/example-hostname/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<dependency>
5959
<groupId>junit</groupId>
6060
<artifactId>junit</artifactId>
61-
<version>4.12</version>
61+
<version>4.13.2</version>
6262
<scope>test</scope>
6363
</dependency>
6464
<dependency>

examples/example-jwt-auth/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies {
3737
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
3838

3939
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
40-
testImplementation "junit:junit:4.12"
40+
testImplementation "junit:junit:4.13.2"
4141
testImplementation "org.mockito:mockito-core:3.4.0"
4242
}
4343

examples/example-jwt-auth/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<dependency>
7272
<groupId>junit</groupId>
7373
<artifactId>junit</artifactId>
74-
<version>4.12</version>
74+
<version>4.13.2</version>
7575
<scope>test</scope>
7676
</dependency>
7777
<dependency>

examples/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<dependency>
7171
<groupId>junit</groupId>
7272
<artifactId>junit</artifactId>
73-
<version>4.12</version>
73+
<version>4.13.2</version>
7474
<scope>test</scope>
7575
</dependency>
7676
<dependency>

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ hdrhistogram = "org.hdrhistogram:HdrHistogram:2.1.12"
3939
javax-annotation = "org.apache.tomcat:annotations-api:6.0.53"
4040
jetty-alpn-agent = "org.mortbay.jetty.alpn:jetty-alpn-agent:2.0.10"
4141
jsr305 = "com.google.code.findbugs:jsr305:3.0.2"
42-
junit = "junit:junit:4.12"
42+
junit = "junit:junit:4.13.2"
4343
mockito-android = "org.mockito:mockito-android:3.8.0"
4444
mockito-core = "org.mockito:mockito-core:3.3.3"
4545
netty-codec-http2 = { module = "io.netty:netty-codec-http2", version.ref = "netty" }

netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java

+24-28
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package io.grpc.netty;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.Assert.assertArrayEquals;
2021
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThrows;
2123
import static org.junit.Assert.fail;
2224

2325
import com.google.common.util.concurrent.MoreExecutors;
@@ -56,9 +58,7 @@
5658
import javax.net.ssl.SSLEngine;
5759
import org.junit.After;
5860
import org.junit.Before;
59-
import org.junit.Rule;
6061
import org.junit.Test;
61-
import org.junit.rules.ExpectedException;
6262
import org.junit.runner.RunWith;
6363
import org.junit.runners.JUnit4;
6464

@@ -89,9 +89,6 @@ public class AdvancedTlsTest {
8989
private PrivateKey serverKeyBad;
9090
private X509Certificate[] serverCertBad;
9191

92-
@Rule
93-
public ExpectedException exceptionRule = ExpectedException.none();
94-
9592
@Before
9693
public void setUp()
9794
throws NoSuchAlgorithmException, IOException, CertificateException, InvalidKeySpecException {
@@ -427,24 +424,22 @@ public void onFileLoadingKeyManagerTrustManagerTest() throws Exception {
427424

428425
@Test
429426
public void onFileReloadingKeyManagerBadInitialContentTest() throws Exception {
430-
exceptionRule.expect(GeneralSecurityException.class);
431427
AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager();
432428
// We swap the order of key and certificates to intentionally create an exception.
433-
Closeable keyShutdown = keyManager.updateIdentityCredentialsFromFile(serverCert0File,
434-
serverKey0File, 100, TimeUnit.MILLISECONDS, executor);
435-
keyShutdown.close();
429+
assertThrows(GeneralSecurityException.class,
430+
() -> keyManager.updateIdentityCredentialsFromFile(serverCert0File,
431+
serverKey0File, 100, TimeUnit.MILLISECONDS, executor));
436432
}
437433

438434
@Test
439435
public void onFileReloadingTrustManagerBadInitialContentTest() throws Exception {
440-
exceptionRule.expect(GeneralSecurityException.class);
441436
AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder()
442437
.setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION)
443438
.build();
444439
// We pass in a key as the trust certificates to intentionally create an exception.
445-
Closeable trustShutdown = trustManager.updateTrustCredentialsFromFile(serverKey0File,
446-
100, TimeUnit.MILLISECONDS, executor);
447-
trustShutdown.close();
440+
assertThrows(GeneralSecurityException.class,
441+
() -> trustManager.updateTrustCredentialsFromFile(serverKey0File,
442+
100, TimeUnit.MILLISECONDS, executor));
448443
}
449444

450445
@Test
@@ -472,40 +467,38 @@ public void trustManagerCheckTrustedWithSocketTest() throws Exception {
472467

473468
@Test
474469
public void trustManagerCheckClientTrustedWithoutParameterTest() throws Exception {
475-
exceptionRule.expect(CertificateException.class);
476-
exceptionRule.expectMessage(
477-
"Not enough information to validate peer. SSLEngine or Socket required.");
478470
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
479471
.setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).build();
480-
tm.checkClientTrusted(serverCert0, "RSA");
472+
CertificateException ex =
473+
assertThrows(CertificateException.class, () -> tm.checkClientTrusted(serverCert0, "RSA"));
474+
assertThat(ex).hasMessageThat()
475+
.isEqualTo("Not enough information to validate peer. SSLEngine or Socket required.");
481476
}
482477

483478
@Test
484479
public void trustManagerCheckServerTrustedWithoutParameterTest() throws Exception {
485-
exceptionRule.expect(CertificateException.class);
486-
exceptionRule.expectMessage(
487-
"Not enough information to validate peer. SSLEngine or Socket required.");
488480
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
489481
.setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).build();
490-
tm.checkServerTrusted(serverCert0, "RSA");
482+
CertificateException ex =
483+
assertThrows(CertificateException.class, () -> tm.checkServerTrusted(serverCert0, "RSA"));
484+
assertThat(ex).hasMessageThat()
485+
.isEqualTo("Not enough information to validate peer. SSLEngine or Socket required.");
491486
}
492487

493488
@Test
494489
public void trustManagerEmptyChainTest() throws Exception {
495-
exceptionRule.expect(IllegalArgumentException.class);
496-
exceptionRule.expectMessage(
497-
"Want certificate verification but got null or empty certificates");
498490
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
499491
.setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION)
500492
.build();
501493
tm.updateTrustCredentials(caCert);
502-
tm.checkClientTrusted(null, "RSA", (SSLEngine) null);
494+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
495+
() -> tm.checkClientTrusted(null, "RSA", (SSLEngine) null));
496+
assertThat(ex).hasMessageThat()
497+
.isEqualTo("Want certificate verification but got null or empty certificates");
503498
}
504499

505500
@Test
506501
public void trustManagerBadCustomVerificationTest() throws Exception {
507-
exceptionRule.expect(CertificateException.class);
508-
exceptionRule.expectMessage("Bad Custom Verification");
509502
AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder()
510503
.setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION)
511504
.setSslSocketAndEnginePeerVerifier(
@@ -523,7 +516,10 @@ public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authTy
523516
}
524517
}).build();
525518
tm.updateTrustCredentials(caCert);
526-
tm.checkClientTrusted(serverCert0, "RSA", new Socket());
519+
CertificateException ex = assertThrows(
520+
CertificateException.class,
521+
() -> tm.checkClientTrusted(serverCert0, "RSA", new Socket()));
522+
assertThat(ex).hasMessageThat().isEqualTo("Bad Custom Verification");
527523
}
528524

529525
private static class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {

0 commit comments

Comments
 (0)