Skip to content

Commit d60e6fc

Browse files
authored
Replace usages of deprecated ExpectedException in grpc-api and grpc-core (#11962)
1 parent d2d72cd commit d60e6fc

19 files changed

+185
-303
lines changed

api/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
testImplementation project(':grpc-core')
4848
testImplementation project(':grpc-testing')
4949
testImplementation libraries.guava.testlib
50+
testImplementation libraries.truth
5051

5152
signature (libraries.signature.java) {
5253
artifact {

api/src/test/java/io/grpc/MetadataTest.java

+14-21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.grpc;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static java.nio.charset.StandardCharsets.US_ASCII;
2021
import static java.nio.charset.StandardCharsets.UTF_8;
2122
import static org.junit.Assert.assertArrayEquals;
@@ -24,6 +25,7 @@
2425
import static org.junit.Assert.assertNotSame;
2526
import static org.junit.Assert.assertNull;
2627
import static org.junit.Assert.assertSame;
28+
import static org.junit.Assert.assertThrows;
2729
import static org.junit.Assert.assertTrue;
2830
import static org.junit.Assert.fail;
2931

@@ -37,9 +39,7 @@
3739
import java.util.Arrays;
3840
import java.util.Iterator;
3941
import java.util.Locale;
40-
import org.junit.Rule;
4142
import org.junit.Test;
42-
import org.junit.rules.ExpectedException;
4343
import org.junit.runner.RunWith;
4444
import org.junit.runners.JUnit4;
4545

@@ -49,9 +49,6 @@
4949
@RunWith(JUnit4.class)
5050
public class MetadataTest {
5151

52-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
53-
@Rule public final ExpectedException thrown = ExpectedException.none();
54-
5552
private static final Metadata.BinaryMarshaller<Fish> FISH_MARSHALLER =
5653
new Metadata.BinaryMarshaller<Fish>() {
5754
@Override
@@ -65,7 +62,7 @@ public Fish parseBytes(byte[] serialized) {
6562
}
6663
};
6764

68-
private static class FishStreamMarsaller implements Metadata.BinaryStreamMarshaller<Fish> {
65+
private static class FishStreamMarshaller implements Metadata.BinaryStreamMarshaller<Fish> {
6966
@Override
7067
public InputStream toStream(Fish fish) {
7168
return new ByteArrayInputStream(FISH_MARSHALLER.toBytes(fish));
@@ -82,7 +79,7 @@ public Fish parseStream(InputStream stream) {
8279
}
8380

8481
private static final Metadata.BinaryStreamMarshaller<Fish> FISH_STREAM_MARSHALLER =
85-
new FishStreamMarsaller();
82+
new FishStreamMarshaller();
8683

8784
/** A pattern commonly used to avoid unnecessary serialization of immutable objects. */
8885
private static final class FakeFishStream extends InputStream {
@@ -121,10 +118,9 @@ public Fish parseStream(InputStream stream) {
121118

122119
@Test
123120
public void noPseudoHeaders() {
124-
thrown.expect(IllegalArgumentException.class);
125-
thrown.expectMessage("Invalid character");
126-
127-
Metadata.Key.of(":test-bin", FISH_MARSHALLER);
121+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
122+
() -> Metadata.Key.of(":test-bin", FISH_MARSHALLER));
123+
assertThat(e).hasMessageThat().isEqualTo("Invalid character ':' in key name ':test-bin'");
128124
}
129125

130126
@Test
@@ -186,8 +182,7 @@ public void testGetAllNoRemove() {
186182
Iterator<Fish> i = metadata.getAll(KEY).iterator();
187183
assertEquals(lance, i.next());
188184

189-
thrown.expect(UnsupportedOperationException.class);
190-
i.remove();
185+
assertThrows(UnsupportedOperationException.class, i::remove);
191186
}
192187

193188
@Test
@@ -271,17 +266,15 @@ public void mergeExpands() {
271266

272267
@Test
273268
public void shortBinaryKeyName() {
274-
thrown.expect(IllegalArgumentException.class);
275-
276-
Metadata.Key.of("-bin", FISH_MARSHALLER);
269+
assertThrows(IllegalArgumentException.class, () -> Metadata.Key.of("-bin", FISH_MARSHALLER));
277270
}
278271

279272
@Test
280273
public void invalidSuffixBinaryKeyName() {
281-
thrown.expect(IllegalArgumentException.class);
282-
thrown.expectMessage("Binary header is named");
283-
284-
Metadata.Key.of("nonbinary", FISH_MARSHALLER);
274+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
275+
() -> Metadata.Key.of("nonbinary", FISH_MARSHALLER));
276+
assertThat(e).hasMessageThat()
277+
.isEqualTo("Binary header is named nonbinary. It must end with -bin");
285278
}
286279

287280
@Test
@@ -415,7 +408,7 @@ public void streamedValueDifferentMarshaller() {
415408
h.put(KEY_STREAMED, salmon);
416409

417410
// Get using a different marshaller instance.
418-
Fish fish = h.get(copyKey(KEY_STREAMED, new FishStreamMarsaller()));
411+
Fish fish = h.get(copyKey(KEY_STREAMED, new FishStreamMarshaller()));
419412
assertEquals(salmon, fish);
420413
}
421414

api/src/test/java/io/grpc/MethodDescriptorTest.java

-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import io.grpc.MethodDescriptor.Marshaller;
2727
import io.grpc.MethodDescriptor.MethodType;
2828
import io.grpc.testing.TestMethodDescriptors;
29-
import org.junit.Rule;
3029
import org.junit.Test;
31-
import org.junit.rules.ExpectedException;
3230
import org.junit.runner.RunWith;
3331
import org.junit.runners.JUnit4;
3432

@@ -37,10 +35,6 @@
3735
*/
3836
@RunWith(JUnit4.class)
3937
public class MethodDescriptorTest {
40-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
41-
@Rule
42-
public final ExpectedException thrown = ExpectedException.none();
43-
4438
@Test
4539
public void createMethodDescriptor() {
4640
MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()

api/src/test/java/io/grpc/ServerInterceptorsTest.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.collect.Iterables.getOnlyElement;
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertSame;
22+
import static org.junit.Assert.assertThrows;
2223
import static org.junit.Assert.assertTrue;
2324
import static org.mockito.AdditionalAnswers.delegatesTo;
2425
import static org.mockito.ArgumentMatchers.same;
@@ -40,7 +41,6 @@
4041
import org.junit.Before;
4142
import org.junit.Rule;
4243
import org.junit.Test;
43-
import org.junit.rules.ExpectedException;
4444
import org.junit.runner.RunWith;
4545
import org.junit.runners.JUnit4;
4646
import org.mockito.ArgumentMatchers;
@@ -55,10 +55,6 @@ public class ServerInterceptorsTest {
5555
@Rule
5656
public final MockitoRule mocks = MockitoJUnit.rule();
5757

58-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
59-
@Rule
60-
public final ExpectedException thrown = ExpectedException.none();
61-
6258
@Mock
6359
private Marshaller<String> requestMarshaller;
6460

@@ -111,21 +107,21 @@ public void makeSureExpectedMocksUnused() {
111107
public void npeForNullServiceDefinition() {
112108
ServerServiceDefinition serviceDef = null;
113109
List<ServerInterceptor> interceptors = Arrays.asList();
114-
thrown.expect(NullPointerException.class);
115-
ServerInterceptors.intercept(serviceDef, interceptors);
110+
assertThrows(NullPointerException.class,
111+
() -> ServerInterceptors.intercept(serviceDef, interceptors));
116112
}
117113

118114
@Test
119115
public void npeForNullInterceptorList() {
120-
thrown.expect(NullPointerException.class);
121-
ServerInterceptors.intercept(serviceDefinition, (List<ServerInterceptor>) null);
116+
assertThrows(NullPointerException.class,
117+
() -> ServerInterceptors.intercept(serviceDefinition, (List<ServerInterceptor>) null));
122118
}
123119

124120
@Test
125121
public void npeForNullInterceptor() {
126122
List<ServerInterceptor> interceptors = Arrays.asList((ServerInterceptor) null);
127-
thrown.expect(NullPointerException.class);
128-
ServerInterceptors.intercept(serviceDefinition, interceptors);
123+
assertThrows(NullPointerException.class,
124+
() -> ServerInterceptors.intercept(serviceDefinition, interceptors));
129125
}
130126

131127
@Test

api/src/test/java/io/grpc/ServerServiceDefinitionTest.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertSame;
21+
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.fail;
2223

2324
import java.util.ArrayList;
2425
import java.util.Collections;
2526
import java.util.HashSet;
26-
import org.junit.Rule;
2727
import org.junit.Test;
28-
import org.junit.rules.ExpectedException;
2928
import org.junit.runner.RunWith;
3029
import org.junit.runners.JUnit4;
3130

@@ -52,9 +51,6 @@ public class ServerServiceDefinitionTest {
5251
= ServerMethodDefinition.create(method1, methodHandler1);
5352
private ServerMethodDefinition<String, Integer> methodDef2
5453
= ServerMethodDefinition.create(method2, methodHandler2);
55-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
56-
@Rule
57-
public ExpectedException thrown = ExpectedException.none();
5854

5955
@Test
6056
public void noMethods() {
@@ -91,35 +87,30 @@ public void addMethod_duplicateName() {
9187
ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1);
9288
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd)
9389
.addMethod(method1, methodHandler1);
94-
thrown.expect(IllegalStateException.class);
95-
ssd.addMethod(diffMethod1, methodHandler2)
96-
.build();
90+
assertThrows(IllegalStateException.class, () -> ssd.addMethod(diffMethod1, methodHandler2));
9791
}
9892

9993
@Test
10094
public void buildMisaligned_extraMethod() {
10195
ServiceDescriptor sd = new ServiceDescriptor(serviceName);
10296
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd)
10397
.addMethod(methodDef1);
104-
thrown.expect(IllegalStateException.class);
105-
ssd.build();
98+
assertThrows(IllegalStateException.class, ssd::build);
10699
}
107100

108101
@Test
109102
public void buildMisaligned_diffMethodInstance() {
110103
ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1);
111104
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd)
112105
.addMethod(diffMethod1, methodHandler1);
113-
thrown.expect(IllegalStateException.class);
114-
ssd.build();
106+
assertThrows(IllegalStateException.class, ssd::build);
115107
}
116108

117109
@Test
118110
public void buildMisaligned_missingMethod() {
119111
ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1);
120112
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd);
121-
thrown.expect(IllegalStateException.class);
122-
ssd.build();
113+
assertThrows(IllegalStateException.class, ssd::build);
123114
}
124115

125116
@Test

api/src/test/java/io/grpc/ServiceDescriptorTest.java

+24-27
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
package io.grpc;
1818

19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertThrows;
1921
import static org.junit.Assert.assertTrue;
2022

23+
import com.google.common.truth.StringSubject;
2124
import io.grpc.MethodDescriptor.MethodType;
2225
import io.grpc.testing.TestMethodDescriptors;
2326
import java.util.Arrays;
2427
import java.util.Collection;
2528
import java.util.Collections;
2629
import java.util.List;
27-
import org.junit.Rule;
2830
import org.junit.Test;
29-
import org.junit.rules.ExpectedException;
3031
import org.junit.runner.RunWith;
3132
import org.junit.runners.JUnit4;
3233

@@ -36,48 +37,45 @@
3637
@RunWith(JUnit4.class)
3738
public class ServiceDescriptorTest {
3839

39-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
40-
@Rule
41-
public final ExpectedException thrown = ExpectedException.none();
42-
4340
@Test
4441
public void failsOnNullName() {
45-
thrown.expect(NullPointerException.class);
46-
thrown.expectMessage("name");
47-
48-
new ServiceDescriptor(null, Collections.<MethodDescriptor<?, ?>>emptyList());
42+
List<MethodDescriptor<?, ?>> methods = Collections.emptyList();
43+
NullPointerException e = assertThrows(NullPointerException.class,
44+
() -> new ServiceDescriptor(null, methods));
45+
assertThat(e).hasMessageThat().isEqualTo("name");
4946
}
5047

5148
@Test
5249
public void failsOnNullMethods() {
53-
thrown.expect(NullPointerException.class);
54-
thrown.expectMessage("methods");
55-
56-
new ServiceDescriptor("name", (Collection<MethodDescriptor<?, ?>>) null);
50+
NullPointerException e = assertThrows(NullPointerException.class,
51+
() -> new ServiceDescriptor("name", (Collection<MethodDescriptor<?, ?>>) null));
52+
assertThat(e).hasMessageThat().isEqualTo("methods");
5753
}
5854

5955
@Test
6056
public void failsOnNullMethod() {
61-
thrown.expect(NullPointerException.class);
62-
thrown.expectMessage("method");
63-
64-
new ServiceDescriptor("name", Collections.<MethodDescriptor<?, ?>>singletonList(null));
57+
List<MethodDescriptor<?, ?>> methods = Collections.singletonList(null);
58+
NullPointerException e = assertThrows(NullPointerException.class,
59+
() -> new ServiceDescriptor("name", methods));
60+
assertThat(e).hasMessageThat().isEqualTo("method");
6561
}
6662

6763
@Test
6864
public void failsOnNonMatchingNames() {
6965
List<MethodDescriptor<?, ?>> descriptors = Collections.<MethodDescriptor<?, ?>>singletonList(
7066
MethodDescriptor.<Void, Void>newBuilder()
7167
.setType(MethodType.UNARY)
72-
.setFullMethodName(MethodDescriptor.generateFullMethodName("wrongservice", "method"))
68+
.setFullMethodName(MethodDescriptor.generateFullMethodName("wrongService", "method"))
7369
.setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
7470
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
7571
.build());
7672

77-
thrown.expect(IllegalArgumentException.class);
78-
thrown.expectMessage("service names");
79-
80-
new ServiceDescriptor("name", descriptors);
73+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
74+
() -> new ServiceDescriptor("fooService", descriptors));
75+
StringSubject error = assertThat(e).hasMessageThat();
76+
error.contains("service names");
77+
error.contains("fooService");
78+
error.contains("wrongService");
8179
}
8280

8381
@Test
@@ -96,10 +94,9 @@ public void failsOnNonDuplicateNames() {
9694
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
9795
.build());
9896

99-
thrown.expect(IllegalArgumentException.class);
100-
thrown.expectMessage("duplicate");
101-
102-
new ServiceDescriptor("name", descriptors);
97+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
98+
() -> new ServiceDescriptor("name", descriptors));
99+
assertThat(e).hasMessageThat().isEqualTo("duplicate name name/method");
103100
}
104101

105102
@Test

0 commit comments

Comments
 (0)