Skip to content

Commit 4cfbb8c

Browse files
committed
replace ExpectedException in grpc-core
1 parent bbfff41 commit 4cfbb8c

13 files changed

+133
-224
lines changed

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.assertFalse;
2424
import static org.junit.Assert.assertNull;
2525
import static org.junit.Assert.assertSame;
26+
import static org.junit.Assert.assertThrows;
2627
import static org.junit.Assert.assertTrue;
2728
import static org.junit.Assert.fail;
2829
import static org.mockito.AdditionalAnswers.delegatesTo;
@@ -57,7 +58,6 @@
5758
import org.junit.Before;
5859
import org.junit.Rule;
5960
import org.junit.Test;
60-
import org.junit.rules.ExpectedException;
6161
import org.junit.runner.RunWith;
6262
import org.junit.runners.JUnit4;
6363
import org.mockito.ArgumentCaptor;
@@ -76,8 +76,6 @@
7676
public class AbstractClientStreamTest {
7777

7878
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
79-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
80-
@Rule public final ExpectedException thrown = ExpectedException.none();
8179

8280
private final StatsTraceContext statsTraceCtx = StatsTraceContext.NOOP;
8381
private final TransportTracer transportTracer = new TransportTracer();
@@ -136,9 +134,7 @@ public void cancel_failsOnNull() {
136134
AbstractClientStream stream =
137135
new BaseAbstractClientStream(allocator, statsTraceCtx, transportTracer);
138136
stream.start(listener);
139-
thrown.expect(NullPointerException.class);
140-
141-
stream.cancel(null);
137+
assertThrows(NullPointerException.class, () -> stream.cancel(null));
142138
}
143139

144140
@Test
@@ -164,19 +160,15 @@ public void startFailsOnNullListener() {
164160
AbstractClientStream stream =
165161
new BaseAbstractClientStream(allocator, statsTraceCtx, transportTracer);
166162

167-
thrown.expect(NullPointerException.class);
168-
169-
stream.start(null);
163+
assertThrows(NullPointerException.class, () -> stream.start(null));
170164
}
171165

172166
@Test
173167
public void cantCallStartTwice() {
174168
AbstractClientStream stream =
175169
new BaseAbstractClientStream(allocator, statsTraceCtx, transportTracer);
176170
stream.start(mockListener);
177-
thrown.expect(IllegalStateException.class);
178-
179-
stream.start(mockListener);
171+
assertThrows(IllegalStateException.class, () -> stream.start(mockListener));
180172
}
181173

182174
@Test
@@ -188,8 +180,7 @@ public void inboundDataReceived_failsOnNullFrame() {
188180

189181
TransportState state = stream.transportState();
190182

191-
thrown.expect(NullPointerException.class);
192-
state.inboundDataReceived(null);
183+
assertThrows(NullPointerException.class, () -> state.inboundDataReceived(null));
193184
}
194185

195186
@Test
@@ -212,8 +203,8 @@ public void inboundHeadersReceived_failsIfStatusReported() {
212203

213204
TransportState state = stream.transportState();
214205

215-
thrown.expect(IllegalStateException.class);
216-
state.inboundHeadersReceived(new Metadata());
206+
Metadata headers = new Metadata();
207+
assertThrows(IllegalStateException.class, () -> state.inboundHeadersReceived(headers));
217208
}
218209

219210
@Test

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.mockito.AdditionalAnswers.delegatesTo;
2324
import static org.mockito.ArgumentMatchers.any;
@@ -45,9 +46,7 @@
4546
import java.util.Queue;
4647
import java.util.concurrent.TimeUnit;
4748
import org.junit.Before;
48-
import org.junit.Rule;
4949
import org.junit.Test;
50-
import org.junit.rules.ExpectedException;
5150
import org.junit.runner.RunWith;
5251
import org.junit.runners.JUnit4;
5352
import org.mockito.ArgumentCaptor;
@@ -60,9 +59,6 @@ public class AbstractServerStreamTest {
6059
private static final int TIMEOUT_MS = 1000;
6160
private static final int MAX_MESSAGE_SIZE = 100;
6261

63-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
64-
@Rule public final ExpectedException thrown = ExpectedException.none();
65-
6662
private final WritableBufferAllocator allocator = new WritableBufferAllocator() {
6763
@Override
6864
public WritableBuffer allocate(int capacityHint) {
@@ -226,9 +222,9 @@ public void completeWithoutClose() {
226222
public void setListener_setOnlyOnce() {
227223
TransportState state = stream.transportState();
228224
state.setListener(new ServerStreamListenerBase());
229-
thrown.expect(IllegalStateException.class);
230225

231-
state.setListener(new ServerStreamListenerBase());
226+
ServerStreamListenerBase listener2 = new ServerStreamListenerBase();
227+
assertThrows(IllegalStateException.class, () -> state.setListener(listener2));
232228
}
233229

234230
@Test
@@ -238,8 +234,7 @@ public void listenerReady_onlyOnce() {
238234

239235
TransportState state = stream.transportState();
240236

241-
thrown.expect(IllegalStateException.class);
242-
state.onStreamAllocated();
237+
assertThrows(IllegalStateException.class, state::onStreamAllocated);
243238
}
244239

245240
@Test
@@ -255,8 +250,7 @@ public void listenerReady_readyCalled() {
255250
public void setListener_failsOnNull() {
256251
TransportState state = stream.transportState();
257252

258-
thrown.expect(NullPointerException.class);
259-
state.setListener(null);
253+
assertThrows(NullPointerException.class, () -> state.setListener(null));
260254
}
261255

262256
// TODO(ericgribkoff) This test is only valid if deframeInTransportThread=true, as otherwise the
@@ -284,9 +278,7 @@ public void messagesAvailable(MessageProducer producer) {
284278

285279
@Test
286280
public void writeHeaders_failsOnNullHeaders() {
287-
thrown.expect(NullPointerException.class);
288-
289-
stream.writeHeaders(null, true);
281+
assertThrows(NullPointerException.class, () -> stream.writeHeaders(null, true));
290282
}
291283

292284
@Test
@@ -336,16 +328,13 @@ public void writeMessage_closesStream() throws Exception {
336328

337329
@Test
338330
public void close_failsOnNullStatus() {
339-
thrown.expect(NullPointerException.class);
340-
341-
stream.close(null, new Metadata());
331+
Metadata trailers = new Metadata();
332+
assertThrows(NullPointerException.class, () -> stream.close(null, trailers));
342333
}
343334

344335
@Test
345336
public void close_failsOnNullMetadata() {
346-
thrown.expect(NullPointerException.class);
347-
348-
stream.close(Status.INTERNAL, null);
337+
assertThrows(NullPointerException.class, () -> stream.close(Status.INTERNAL, null));
349338
}
350339

351340
@Test
@@ -451,4 +440,3 @@ public int streamId() {
451440
}
452441
}
453442
}
454-

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import io.grpc.ConnectivityState;
2828
import java.util.LinkedList;
2929
import java.util.concurrent.Executor;
30-
import org.junit.Rule;
3130
import org.junit.Test;
32-
import org.junit.rules.ExpectedException;
3331
import org.junit.runner.RunWith;
3432
import org.junit.runners.JUnit4;
3533

@@ -38,10 +36,6 @@
3836
*/
3937
@RunWith(JUnit4.class)
4038
public class ConnectivityStateManagerTest {
41-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
42-
@Rule
43-
public final ExpectedException thrown = ExpectedException.none();
44-
4539
private final FakeClock executor = new FakeClock();
4640
private final ConnectivityStateManager state = new ConnectivityStateManager();
4741
private final LinkedList<ConnectivityState> sink = new LinkedList<>();
@@ -75,7 +69,7 @@ public void run() {
7569
assertEquals(1, sink.size());
7670
assertEquals(TRANSIENT_FAILURE, sink.poll());
7771
}
78-
72+
7973
@Test
8074
public void registerCallbackAfterStateChanged() {
8175
state.gotoState(CONNECTING);

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertNull;
2424
import static org.junit.Assert.assertSame;
25+
import static org.junit.Assert.assertThrows;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.junit.Assert.fail;
2728
import static org.mockito.ArgumentMatchers.any;
@@ -35,6 +36,7 @@
3536
import static org.mockito.Mockito.when;
3637

3738
import com.google.common.base.Stopwatch;
39+
import com.google.common.base.VerifyException;
3840
import com.google.common.collect.ImmutableMap;
3941
import com.google.common.collect.Iterables;
4042
import com.google.common.net.InetAddresses;
@@ -82,7 +84,6 @@
8284
import org.junit.Rule;
8385
import org.junit.Test;
8486
import org.junit.rules.DisableOnDebug;
85-
import org.junit.rules.ExpectedException;
8687
import org.junit.rules.TestRule;
8788
import org.junit.rules.Timeout;
8889
import org.junit.runner.RunWith;
@@ -99,8 +100,6 @@ public class DnsNameResolverTest {
99100

100101
@Rule public final TestRule globalTimeout = new DisableOnDebug(Timeout.seconds(10));
101102
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
102-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
103-
@Rule public final ExpectedException thrown = ExpectedException.none();
104103

105104
private final Map<String, ?> serviceConfig = new LinkedHashMap<>();
106105

@@ -914,9 +913,10 @@ public HttpConnectProxiedSocketAddress proxyFor(SocketAddress targetAddress) {
914913
public void maybeChooseServiceConfig_failsOnMisspelling() {
915914
Map<String, Object> bad = new LinkedHashMap<>();
916915
bad.put("parcentage", 1.0);
917-
thrown.expectMessage("Bad key");
918-
919-
DnsNameResolver.maybeChooseServiceConfig(bad, new Random(), "host");
916+
Random random = new Random();
917+
VerifyException e = assertThrows(VerifyException.class,
918+
() -> DnsNameResolver.maybeChooseServiceConfig(bad, random, "host"));
919+
assertThat(e).hasMessageThat().contains("Bad key");
920920
}
921921

922922
@Test
@@ -1155,25 +1155,25 @@ public void parseTxtResults_misspelledName() throws Exception {
11551155
}
11561156

11571157
@Test
1158-
public void parseTxtResults_badTypeFails() throws Exception {
1158+
public void parseTxtResults_badTypeFails() {
11591159
List<String> txtRecords = new ArrayList<>();
11601160
txtRecords.add("some_record");
11611161
txtRecords.add("grpc_config={}");
11621162

1163-
thrown.expect(ClassCastException.class);
1164-
thrown.expectMessage("wrong type");
1165-
DnsNameResolver.parseTxtResults(txtRecords);
1163+
ClassCastException e = assertThrows(ClassCastException.class,
1164+
() -> DnsNameResolver.parseTxtResults(txtRecords));
1165+
assertThat(e).hasMessageThat().contains("wrong type");
11661166
}
11671167

11681168
@Test
1169-
public void parseTxtResults_badInnerTypeFails() throws Exception {
1169+
public void parseTxtResults_badInnerTypeFails() {
11701170
List<String> txtRecords = new ArrayList<>();
11711171
txtRecords.add("some_record");
11721172
txtRecords.add("grpc_config=[\"bogus\"]");
11731173

1174-
thrown.expect(ClassCastException.class);
1175-
thrown.expectMessage("not object");
1176-
DnsNameResolver.parseTxtResults(txtRecords);
1174+
ClassCastException e = assertThrows(ClassCastException.class,
1175+
() -> DnsNameResolver.parseTxtResults(txtRecords));
1176+
assertThat(e).hasMessageThat().contains("not object");
11771177
}
11781178

11791179
@Test

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertNull;
2424
import static org.junit.Assert.assertSame;
25+
import static org.junit.Assert.assertThrows;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.mockito.ArgumentMatchers.any;
2728
import static org.mockito.ArgumentMatchers.eq;
@@ -41,7 +42,6 @@
4142
import java.util.ArrayList;
4243
import org.junit.Rule;
4344
import org.junit.Test;
44-
import org.junit.rules.ExpectedException;
4545
import org.junit.runner.RunWith;
4646
import org.junit.runners.JUnit4;
4747
import org.mockito.ArgumentCaptor;
@@ -57,8 +57,6 @@ public class GrpcUtilTest {
5757
new ClientStreamTracer() {}
5858
};
5959

60-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
61-
@Rule public final ExpectedException thrown = ExpectedException.none();
6260
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
6361

6462
@Captor
@@ -201,9 +199,7 @@ public void urlAuthorityEscape_unicodeAreNotEncoded() {
201199

202200
@Test
203201
public void checkAuthority_failsOnNull() {
204-
thrown.expect(NullPointerException.class);
205-
206-
GrpcUtil.checkAuthority(null);
202+
assertThrows(NullPointerException.class, () -> GrpcUtil.checkAuthority(null));
207203
}
208204

209205
@Test
@@ -229,19 +225,17 @@ public void checkAuthority_succeedsOnIpV6() {
229225

230226
@Test
231227
public void checkAuthority_failsOnInvalidAuthority() {
232-
thrown.expect(IllegalArgumentException.class);
233-
thrown.expectMessage("Invalid authority");
234-
235-
GrpcUtil.checkAuthority("[ : : 1]");
228+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
229+
() -> GrpcUtil.checkAuthority("[ : : 1]"));
230+
assertThat(e).hasMessageThat().contains("Invalid authority");
236231
}
237232

238233

239234
@Test
240235
public void checkAuthority_userInfoNotAllowed() {
241-
thrown.expect(IllegalArgumentException.class);
242-
thrown.expectMessage("Userinfo");
243-
244-
GrpcUtil.checkAuthority("foo@valid");
236+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
237+
() -> GrpcUtil.checkAuthority("foo@valid"));
238+
assertThat(e).hasMessageThat().contains("Userinfo");
245239
}
246240

247241
@Test

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.junit.Assert.assertNotNull;
2828
import static org.junit.Assert.assertNull;
2929
import static org.junit.Assert.assertSame;
30+
import static org.junit.Assert.assertThrows;
3031
import static org.junit.Assert.assertTrue;
3132
import static org.mockito.ArgumentMatchers.any;
3233
import static org.mockito.ArgumentMatchers.eq;
@@ -65,7 +66,6 @@
6566
import org.junit.Before;
6667
import org.junit.Rule;
6768
import org.junit.Test;
68-
import org.junit.rules.ExpectedException;
6969
import org.junit.runner.RunWith;
7070
import org.junit.runners.JUnit4;
7171
import org.mockito.Mock;
@@ -79,9 +79,6 @@
7979
public class InternalSubchannelTest {
8080
@Rule
8181
public final MockitoRule mocks = MockitoJUnit.rule();
82-
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
83-
@Rule
84-
public final ExpectedException thrown = ExpectedException.none();
8582

8683
private static final String AUTHORITY = "fakeauthority";
8784
private static final String USER_AGENT = "mosaic";
@@ -544,17 +541,17 @@ public void constructor_eagListWithNull_throws() {
544541
public void updateAddresses_emptyEagList_throws() {
545542
SocketAddress addr = new FakeSocketAddress();
546543
createInternalSubchannel(addr);
547-
thrown.expect(IllegalArgumentException.class);
548-
internalSubchannel.updateAddresses(Arrays.<EquivalentAddressGroup>asList());
544+
List<EquivalentAddressGroup> newAddressGroups = Collections.emptyList();
545+
assertThrows(IllegalArgumentException.class,
546+
() -> internalSubchannel.updateAddresses(newAddressGroups));
549547
}
550548

551549
@Test
552550
public void updateAddresses_eagListWithNull_throws() {
553551
SocketAddress addr = new FakeSocketAddress();
554552
createInternalSubchannel(addr);
555553
List<EquivalentAddressGroup> eags = Arrays.asList((EquivalentAddressGroup) null);
556-
thrown.expect(NullPointerException.class);
557-
internalSubchannel.updateAddresses(eags);
554+
assertThrows(NullPointerException.class, () -> internalSubchannel.updateAddresses(eags));
558555
}
559556

560557
@Test public void updateAddresses_intersecting_ready() {

0 commit comments

Comments
 (0)