Skip to content

Commit 867dd33

Browse files
authored
chore: remove usage of deprecated ExpectedException none() method (#247)
1 parent 00eadd3 commit 867dd33

Some content is hidden

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

48 files changed

+1312
-762
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.cloud.spanner.Type.StructField;
2020
import static com.google.common.truth.Truth.assertThat;
2121
import static com.google.common.truth.Truth.assertWithMessage;
22+
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.fail;
2324
import static org.junit.runners.Parameterized.Parameter;
2425

@@ -33,9 +34,7 @@
3334
import java.util.List;
3435
import javax.annotation.Nullable;
3536
import org.junit.Before;
36-
import org.junit.Rule;
3737
import org.junit.Test;
38-
import org.junit.rules.ExpectedException;
3938
import org.junit.runner.RunWith;
4039
import org.junit.runners.Parameterized;
4140
import org.mockito.Mockito;
@@ -274,7 +273,6 @@ public static Collection<Object[]> parameters() {
274273
@Nullable
275274
public List<String> otherAllowedGetters;
276275

277-
@Rule public ExpectedException expectedException = ExpectedException.none();
278276
private TestStructReader reader;
279277

280278
@Before
@@ -372,15 +370,23 @@ public void getterForIncorrectType() throws Exception {
372370
public void getterWhenNull() throws Exception {
373371
Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type)));
374372
Mockito.when(reader.isNull(0)).thenReturn(true);
375-
expectedException.expect(NullPointerException.class);
376-
getterByIndex(0);
373+
try {
374+
getterByIndex(0);
375+
fail("Expected exception");
376+
} catch (NullPointerException ex) {
377+
assertNotNull(ex.getMessage());
378+
}
377379
}
378380

379381
@Test
380382
public void getterByNameWhenNull() throws Exception {
381383
Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type)));
382384
Mockito.when(reader.isNull(0)).thenReturn(true);
383-
expectedException.expect(NullPointerException.class);
384-
getterByName("F1");
385+
try {
386+
getterByName("F1");
387+
fail("Expected exception");
388+
} catch (NullPointerException ex) {
389+
assertNotNull(ex.getMessage());
390+
}
385391
}
386392
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.junit.Assert.fail;
2121

22-
import org.junit.Rule;
2322
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2523
import org.junit.runner.RunWith;
2624
import org.junit.runners.JUnit4;
2725

2826
/** Unit tests for {@link com.google.cloud.spanner.BackupId}. */
2927
@RunWith(JUnit4.class)
3028
public class BackupIdTest {
31-
@Rule public ExpectedException expectedException = ExpectedException.none();
3229

3330
@Test
3431
public void basics() {

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

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

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
22+
import static org.junit.Assert.fail;
2023
import static org.mockito.Mockito.verify;
2124
import static org.mockito.Mockito.when;
2225
import static org.mockito.MockitoAnnotations.initMocks;
@@ -29,9 +32,7 @@
2932
import com.google.cloud.spanner.BackupInfo.State;
3033
import java.util.Arrays;
3134
import org.junit.Before;
32-
import org.junit.Rule;
3335
import org.junit.Test;
34-
import org.junit.rules.ExpectedException;
3536
import org.junit.runner.RunWith;
3637
import org.junit.runners.JUnit4;
3738
import org.mockito.Mock;
@@ -46,7 +47,6 @@ public class BackupTest {
4647
private static final String DB = "projects/test-project/instances/test-instance/databases/db-1";
4748
private static final Timestamp EXP_TIME = Timestamp.ofTimeSecondsAndNanos(1000L, 1000);
4849

49-
@Rule public ExpectedException expectedException = ExpectedException.none();
5050
@Mock DatabaseAdminClient dbClient;
5151

5252
@Before
@@ -102,8 +102,12 @@ public void createWithoutSource() {
102102
.newBackupBuilder(BackupId.of("test-project", "dest-instance", "backup-id"))
103103
.setExpireTime(expireTime)
104104
.build();
105-
expectedException.expect(IllegalStateException.class);
106-
backup.create();
105+
try {
106+
backup.create();
107+
fail("Expected exception");
108+
} catch (IllegalStateException e) {
109+
assertNotNull(e.getMessage());
110+
}
107111
}
108112

109113
@Test
@@ -113,8 +117,12 @@ public void createWithoutExpireTime() {
113117
.newBackupBuilder(BackupId.of("test-project", "instance-id", "backup-id"))
114118
.setDatabase(DatabaseId.of("test-project", "instance-id", "src-database"))
115119
.build();
116-
expectedException.expect(IllegalStateException.class);
117-
backup.create();
120+
try {
121+
backup.create();
122+
fail("Expected exception");
123+
} catch (IllegalStateException e) {
124+
assertNotNull(e.getMessage());
125+
}
118126
}
119127

120128
@Test
@@ -207,8 +215,12 @@ public void updateExpireTimeWithoutExpireTime() {
207215
dbClient
208216
.newBackupBuilder(BackupId.of("test-project", "test-instance", "test-backup"))
209217
.build();
210-
expectedException.expect(IllegalStateException.class);
211-
backup.updateExpireTime();
218+
try {
219+
backup.updateExpireTime();
220+
fail("Expected exception");
221+
} catch (IllegalStateException e) {
222+
assertNotNull(e.getMessage());
223+
}
212224
}
213225

214226
@Test
@@ -228,8 +240,12 @@ public void restoreWithoutDestination() {
228240
dbClient
229241
.newBackupBuilder(BackupId.of("test-project", "test-instance", "test-backup"))
230242
.build();
231-
expectedException.expect(NullPointerException.class);
232-
backup.restore(null);
243+
try {
244+
backup.restore(null);
245+
fail("Expected exception");
246+
} catch (NullPointerException e) {
247+
assertNull(e.getMessage());
248+
}
233249
}
234250

235251
@Test

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

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

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.fail;
2021

21-
import org.junit.Rule;
2222
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
2423
import org.junit.runner.RunWith;
2524
import org.junit.runners.JUnit4;
2625

2726
/** Unit tests for {@link com.google.cloud.spanner.DatabaseId}. */
2827
@RunWith(JUnit4.class)
2928
public class DatabaseIdTest {
30-
@Rule public ExpectedException expectedException = ExpectedException.none();
3129

3230
@Test
3331
public void basics() {
@@ -44,8 +42,11 @@ public void basics() {
4442

4543
@Test
4644
public void badName() {
47-
expectedException.expect(IllegalArgumentException.class);
48-
expectedException.expectMessage("projects"); // Expect conforming pattern in output.
49-
DatabaseId.of("bad name");
45+
try {
46+
DatabaseId.of("bad name");
47+
fail("Expected exception");
48+
} catch (IllegalArgumentException ex) {
49+
assertThat(ex.getMessage()).contains("projects");
50+
}
5051
}
5152
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
import com.google.cloud.spanner.DatabaseInfo.State;
2929
import java.util.Arrays;
3030
import org.junit.Before;
31-
import org.junit.Rule;
3231
import org.junit.Test;
33-
import org.junit.rules.ExpectedException;
3432
import org.junit.runner.RunWith;
3533
import org.junit.runners.JUnit4;
3634
import org.mockito.Mock;
@@ -44,7 +42,6 @@ public class DatabaseTest {
4442
private static final String NAME =
4543
"projects/test-project/instances/test-instance/databases/database-1";
4644

47-
@Rule public ExpectedException expectedException = ExpectedException.none();
4845
@Mock DatabaseAdminClient dbClient;
4946

5047
@Before

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

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

1717
package com.google.cloud.spanner;
1818

19-
import static com.google.cloud.spanner.SpannerMatchers.isSpannerException;
2019
import static com.google.common.testing.SerializableTester.reserialize;
2120
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.fail;
2222

2323
import com.google.cloud.ByteArray;
2424
import com.google.cloud.Date;
@@ -40,16 +40,13 @@
4040
import java.util.Map;
4141
import javax.annotation.Nullable;
4242
import org.junit.Before;
43-
import org.junit.Rule;
4443
import org.junit.Test;
45-
import org.junit.rules.ExpectedException;
4644
import org.junit.runner.RunWith;
4745
import org.junit.runners.JUnit4;
4846

4947
/** Unit tests for {@link com.google.cloud.spanner.SpannerImpl.GrpcResultSet}. */
5048
@RunWith(JUnit4.class)
5149
public class GrpcResultSetTest {
52-
@Rule public ExpectedException expectedException = ExpectedException.none();
5350

5451
private AbstractResultSet.GrpcResultSet resultSet;
5552
private SpannerRpc.ResultStreamConsumer consumer;
@@ -107,16 +104,24 @@ public void metadataFailure() {
107104
SpannerException t =
108105
SpannerExceptionFactory.newSpannerException(ErrorCode.DEADLINE_EXCEEDED, "outatime");
109106
consumer.onError(t);
110-
expectedException.expect(isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
111-
expectedException.expectMessage("outatime");
112-
resultSet.next();
107+
try {
108+
resultSet.next();
109+
fail("Expected exception");
110+
} catch (SpannerException ex) {
111+
assertThat(ex.getErrorCode()).isEqualTo(ErrorCode.DEADLINE_EXCEEDED);
112+
assertThat(ex.getMessage()).contains("outatime");
113+
}
113114
}
114115

115116
@Test
116117
public void noMetadata() {
117118
consumer.onCompleted();
118-
expectedException.expect(isSpannerException(ErrorCode.INTERNAL));
119-
resultSet.next();
119+
try {
120+
resultSet.next();
121+
fail("Expected exception");
122+
} catch (SpannerException ex) {
123+
assertThat(ex.getErrorCode()).isEqualTo(ErrorCode.INTERNAL);
124+
}
120125
}
121126

122127
@Test
@@ -195,8 +200,12 @@ public void multiResponseChunkingStreamClosed() {
195200
.setChunkedValue(true)
196201
.build());
197202
consumer.onCompleted();
198-
expectedException.expect(isSpannerException(ErrorCode.INTERNAL));
199-
resultSet.next();
203+
try {
204+
resultSet.next();
205+
fail("Expected exception");
206+
} catch (SpannerException ex) {
207+
assertThat(ex.getErrorCode()).isEqualTo(ErrorCode.INTERNAL);
208+
}
200209
}
201210

202211
@Test

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

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

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.fail;
2022

21-
import org.junit.Rule;
2223
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
2424
import org.junit.runner.RunWith;
2525
import org.junit.runners.JUnit4;
2626

2727
/** Unit tests for {@link InstanceConfigId}. */
2828
@RunWith(JUnit4.class)
2929
public class InstanceConfigIdTest {
30-
@Rule public ExpectedException expectedException = ExpectedException.none();
3130

3231
@Test
3332
public void basic() {
@@ -43,7 +42,11 @@ public void basic() {
4342

4443
@Test
4544
public void badName() {
46-
expectedException.expect(IllegalArgumentException.class);
47-
InstanceConfigId.of("bad name");
45+
try {
46+
InstanceConfigId.of("bad name");
47+
fail("Expected exception");
48+
} catch (IllegalArgumentException e) {
49+
assertNotNull(e.getMessage());
50+
}
4851
}
4952
}

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

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

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
2021

21-
import org.junit.Rule;
22+
import org.junit.Assert;
2223
import org.junit.Test;
23-
import org.junit.rules.ExpectedException;
2424
import org.junit.runner.RunWith;
2525
import org.junit.runners.JUnit4;
2626

2727
/** Unit tests for {@link InstanceId}. */
2828
@RunWith(JUnit4.class)
2929
public class InstanceIdTest {
30-
@Rule public ExpectedException expectedException = ExpectedException.none();
3130

3231
@Test
3332
public void basic() {
@@ -43,7 +42,11 @@ public void basic() {
4342

4443
@Test
4544
public void badName() {
46-
expectedException.expect(IllegalArgumentException.class);
47-
InstanceId.of("bad name");
45+
try {
46+
InstanceId.of("bad name");
47+
Assert.fail("Expected exception");
48+
} catch (IllegalArgumentException e) {
49+
assertNotNull(e.getMessage());
50+
}
4851
}
4952
}

0 commit comments

Comments
 (0)