Skip to content

Commit b3ebfcf

Browse files
authored
chore: use assertThrows with lambda instead of try-catch (#1139)
Fixes #1124
1 parent 699a426 commit b3ebfcf

Some content is hidden

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

41 files changed

+1121
-1761
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/AbstractStructReaderTypesTest.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static com.google.common.truth.Truth.assertThat;
2121
import static com.google.common.truth.Truth.assertWithMessage;
2222
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.fail;
23+
import static org.junit.Assert.assertThrows;
2424
import static org.junit.runners.Parameterized.Parameter;
2525

2626
import com.google.cloud.ByteArray;
@@ -408,46 +408,40 @@ public void getterForIncorrectType() {
408408
// Skip allowed getters.
409409
continue;
410410
}
411-
try {
412-
getterByIndex(method.getName(), columnIndex);
413-
fail("Expected " + IllegalStateException.class.getSimpleName() + " for " + method);
414-
} catch (IllegalStateException e) {
415-
assertWithMessage("Exception for " + method).that(e.getMessage()).contains("was " + type);
416-
assertWithMessage("Exception for " + method)
417-
.that(e.getMessage())
418-
.contains("Column " + columnIndex);
419-
}
420-
try {
421-
getterByName(method.getName(), "F1");
422-
fail("Expected ISE for " + method);
423-
} catch (IllegalStateException e) {
424-
assertWithMessage("Exception for " + method).that(e.getMessage()).contains("was " + type);
425-
assertWithMessage("Exception for " + method).that(e.getMessage()).contains("Column F1");
426-
}
411+
IllegalStateException getterByIndexException =
412+
assertThrows(
413+
IllegalStateException.class, () -> getterByIndex(method.getName(), columnIndex));
414+
assertWithMessage("Exception for " + method)
415+
.that(getterByIndexException.getMessage())
416+
.contains("was " + type);
417+
assertWithMessage("Exception for " + method)
418+
.that(getterByIndexException.getMessage())
419+
.contains("Column " + columnIndex);
420+
421+
IllegalStateException getterByNameException =
422+
assertThrows(IllegalStateException.class, () -> getterByName(method.getName(), "F1"));
423+
assertWithMessage("Exception for " + method)
424+
.that(getterByNameException.getMessage())
425+
.contains("was " + type);
426+
assertWithMessage("Exception for " + method)
427+
.that(getterByNameException.getMessage())
428+
.contains("Column F1");
427429
}
428430
}
429431

430432
@Test
431433
public void getterWhenNull() {
432434
Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type)));
433435
Mockito.when(reader.isNull(0)).thenReturn(true);
434-
try {
435-
getterByIndex(0);
436-
fail("Expected exception");
437-
} catch (NullPointerException ex) {
438-
assertNotNull(ex.getMessage());
439-
}
436+
NullPointerException ex = assertThrows(NullPointerException.class, () -> getterByIndex(0));
437+
assertNotNull(ex.getMessage());
440438
}
441439

442440
@Test
443441
public void getterByNameWhenNull() {
444442
Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type)));
445443
Mockito.when(reader.isNull(0)).thenReturn(true);
446-
try {
447-
getterByName("F1");
448-
fail("Expected exception");
449-
} catch (NullPointerException ex) {
450-
assertNotNull(ex.getMessage());
451-
}
444+
NullPointerException ex = assertThrows(NullPointerException.class, () -> getterByName("F1"));
445+
assertNotNull(ex.getMessage());
452446
}
453447
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncResultSetImplTest.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.cloud.spanner;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20-
import static org.junit.Assert.fail;
20+
import static org.junit.Assert.assertThrows;
2121
import static org.mockito.Mockito.mock;
2222
import static org.mockito.Mockito.when;
2323

@@ -68,24 +68,13 @@ public void close() {
6868
rs.close();
6969

7070
// The following methods are not allowed to call after closing the result set.
71-
try {
72-
rs.setCallback(mock(Executor.class), mock(ReadyCallback.class));
73-
fail("missing expected exception");
74-
} catch (IllegalStateException e) {
75-
// Expected exception
76-
}
77-
try {
78-
rs.toList(mock(Function.class));
79-
fail("missing expected exception");
80-
} catch (IllegalStateException e) {
81-
// Expected exception
82-
}
83-
try {
84-
rs.toListAsync(mock(Function.class), mock(Executor.class));
85-
fail("missing expected exception");
86-
} catch (IllegalStateException e) {
87-
// Expected exception
88-
}
71+
assertThrows(
72+
IllegalStateException.class,
73+
() -> rs.setCallback(mock(Executor.class), mock(ReadyCallback.class)));
74+
assertThrows(IllegalStateException.class, () -> rs.toList(mock(Function.class)));
75+
assertThrows(
76+
IllegalStateException.class,
77+
() -> rs.toListAsync(mock(Function.class), mock(Executor.class)));
8978

9079
// The following methods are allowed on a closed result set.
9180
AsyncResultSetImpl rs2 =
@@ -103,13 +92,8 @@ public void tryNextNotAllowed() {
10392
new AsyncResultSetImpl(
10493
mockedProvider, mock(ResultSet.class), AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
10594
rs.setCallback(mock(Executor.class), mock(ReadyCallback.class));
106-
try {
107-
rs.tryNext();
108-
fail("missing expected exception");
109-
} catch (IllegalStateException e) {
110-
assertThat(e.getMessage())
111-
.contains("tryNext may only be called from a DataReady callback.");
112-
}
95+
IllegalStateException e = assertThrows(IllegalStateException.class, () -> rs.tryNext());
96+
assertThat(e.getMessage()).contains("tryNext may only be called from a DataReady callback.");
11397
}
11498
}
11599

@@ -134,9 +118,8 @@ public void toListPropagatesError() {
134118
ErrorCode.INVALID_ARGUMENT, "invalid query"));
135119
try (AsyncResultSetImpl rs =
136120
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
137-
rs.toList(ignored -> new Object());
138-
fail("missing expected exception");
139-
} catch (SpannerException e) {
121+
SpannerException e =
122+
assertThrows(SpannerException.class, () -> rs.toList(ignored -> new Object()));
140123
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
141124
assertThat(e.getMessage()).contains("invalid query");
142125
}
@@ -166,9 +149,10 @@ public void toListAsyncPropagatesError() throws InterruptedException {
166149
ErrorCode.INVALID_ARGUMENT, "invalid query"));
167150
try (AsyncResultSetImpl rs =
168151
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
169-
rs.toListAsync(ignored -> new Object(), executor).get();
170-
fail("missing expected exception");
171-
} catch (ExecutionException e) {
152+
ExecutionException e =
153+
assertThrows(
154+
ExecutionException.class,
155+
() -> rs.toListAsync(ignored -> new Object(), executor).get());
172156
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
173157
SpannerException se = (SpannerException) e.getCause();
174158
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
@@ -386,9 +370,7 @@ public void callbackReturnsError() throws InterruptedException {
386370
callbackCounter.incrementAndGet();
387371
throw new RuntimeException("async test");
388372
});
389-
rs.getResult().get();
390-
fail("missing expected exception");
391-
} catch (ExecutionException e) {
373+
ExecutionException e = assertThrows(ExecutionException.class, () -> rs.getResult().get());
392374
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
393375
SpannerException se = (SpannerException) e.getCause();
394376
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.UNKNOWN);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncRunnerImplTest.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import static com.google.cloud.spanner.SpannerApiFutures.get;
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;
23-
import static org.junit.Assert.fail;
2424
import static org.mockito.Matchers.any;
2525
import static org.mockito.Mockito.mock;
2626
import static org.mockito.Mockito.when;
@@ -68,24 +68,18 @@ public void testAsyncRunReturnsResultAndCommitResponse() {
6868
public void testGetCommitTimestampReturnsErrorBeforeRun() {
6969
TransactionRunnerImpl delegate = mock(TransactionRunnerImpl.class);
7070
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
71-
try {
72-
runner.getCommitTimestamp();
73-
fail("missing expected exception");
74-
} catch (IllegalStateException e) {
75-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
76-
}
71+
IllegalStateException e =
72+
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
73+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
7774
}
7875

7976
@Test
8077
public void testGetCommitResponseReturnsErrorBeforeRun() {
8178
TransactionRunnerImpl delegate = mock(TransactionRunnerImpl.class);
8279
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
83-
try {
84-
runner.getCommitResponse();
85-
fail("missing expected exception");
86-
} catch (IllegalStateException e) {
87-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
88-
}
80+
IllegalStateException e =
81+
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
82+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
8983
}
9084

9185
@Test
@@ -99,12 +93,9 @@ public void testGetCommitResponseReturnsErrorIfRunFails() {
9993
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
10094
runner.runAsync(txn -> ApiFutures.immediateFailedFuture(expectedException), executor);
10195

102-
try {
103-
get(runner.getCommitResponse());
104-
fail("missing expected exception");
105-
} catch (SpannerException e) {
106-
assertSame(expectedException, e);
107-
}
96+
SpannerException e =
97+
assertThrows(SpannerException.class, () -> get(runner.getCommitResponse()));
98+
assertSame(expectedException, e);
10899
}
109100

110101
@SuppressWarnings("unchecked")
@@ -117,11 +108,10 @@ public void testRunAsyncFailsIfCalledMultipleTimes() {
117108
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
118109
runner.runAsync(txn -> ApiFutures.immediateFuture(result), executor);
119110

120-
try {
121-
runner.runAsync(txn -> ApiFutures.immediateFuture(null), executor);
122-
fail("missing expected exception");
123-
} catch (IllegalStateException e) {
124-
assertTrue(e.getMessage().contains("runAsync() can only be called once"));
125-
}
111+
IllegalStateException e =
112+
assertThrows(
113+
IllegalStateException.class,
114+
() -> runner.runAsync(txn -> ApiFutures.immediateFuture(null), executor));
115+
assertTrue(e.getMessage().contains("runAsync() can only be called once"));
126116
}
127117
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncRunnerTest.java

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import static com.google.cloud.spanner.MockSpannerTestUtil.*;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.assertTrue;
22-
import static org.junit.Assert.fail;
2323

2424
import com.google.api.core.ApiFuture;
2525
import com.google.api.core.ApiFutures;
@@ -54,23 +54,17 @@ public class AsyncRunnerTest extends AbstractAsyncTransactionTest {
5454
@Test
5555
public void testAsyncRunner_doesNotReturnCommitTimestampBeforeCommit() {
5656
AsyncRunner runner = client().runAsync();
57-
try {
58-
runner.getCommitTimestamp();
59-
fail("missing expected exception");
60-
} catch (IllegalStateException e) {
61-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
62-
}
57+
IllegalStateException e =
58+
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
59+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
6360
}
6461

6562
@Test
6663
public void testAsyncRunner_doesNotReturnCommitResponseBeforeCommit() {
6764
AsyncRunner runner = client().runAsync();
68-
try {
69-
runner.getCommitResponse();
70-
fail("missing expected exception");
71-
} catch (IllegalStateException e) {
72-
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
73-
}
65+
IllegalStateException e =
66+
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
67+
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
7468
}
7569

7670
@Test
@@ -103,15 +97,11 @@ public void asyncRunnerInvalidUpdate() throws Exception {
10397
AsyncRunner runner = client().runAsync();
10498
ApiFuture<Long> updateCount =
10599
runner.runAsync(txn -> txn.executeUpdateAsync(INVALID_UPDATE_STATEMENT), executor);
106-
try {
107-
updateCount.get();
108-
fail("missing expected exception");
109-
} catch (ExecutionException e) {
110-
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
111-
SpannerException se = (SpannerException) e.getCause();
112-
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
113-
assertThat(se.getMessage()).contains("invalid statement");
114-
}
100+
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
101+
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
102+
SpannerException se = (SpannerException) e.getCause();
103+
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
104+
assertThat(se.getMessage()).contains("invalid statement");
115105
}
116106

117107
@Test
@@ -233,15 +223,11 @@ public void asyncRunnerCommitFails() throws Exception {
233223
return txn.executeUpdateAsync(UPDATE_STATEMENT);
234224
},
235225
executor);
236-
try {
237-
updateCount.get();
238-
fail("missing expected exception");
239-
} catch (ExecutionException e) {
240-
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
241-
SpannerException se = (SpannerException) e.getCause();
242-
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
243-
assertThat(se.getMessage()).contains("mutation limit exceeded");
244-
}
226+
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
227+
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
228+
SpannerException se = (SpannerException) e.getCause();
229+
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
230+
assertThat(se.getMessage()).contains("mutation limit exceeded");
245231
}
246232

247233
@Test
@@ -295,15 +281,11 @@ public void asyncRunnerInvalidBatchUpdate() throws Exception {
295281
txn ->
296282
txn.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, INVALID_UPDATE_STATEMENT)),
297283
executor);
298-
try {
299-
updateCount.get();
300-
fail("missing expected exception");
301-
} catch (ExecutionException e) {
302-
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
303-
SpannerException se = (SpannerException) e.getCause();
304-
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
305-
assertThat(se.getMessage()).contains("invalid statement");
306-
}
284+
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
285+
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
286+
SpannerException se = (SpannerException) e.getCause();
287+
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
288+
assertThat(se.getMessage()).contains("invalid statement");
307289
}
308290

309291
@Test
@@ -423,15 +405,11 @@ public void asyncRunnerWithBatchUpdateCommitFails() throws Exception {
423405
return txn.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT));
424406
},
425407
executor);
426-
try {
427-
updateCount.get();
428-
fail("missing expected exception");
429-
} catch (ExecutionException e) {
430-
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
431-
SpannerException se = (SpannerException) e.getCause();
432-
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
433-
assertThat(se.getMessage()).contains("mutation limit exceeded");
434-
}
408+
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
409+
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
410+
SpannerException se = (SpannerException) e.getCause();
411+
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
412+
assertThat(se.getMessage()).contains("mutation limit exceeded");
435413
}
436414

437415
@Test

0 commit comments

Comments
 (0)