Skip to content

Commit be5a7c1

Browse files
dconeybejeremyjiang-dev
authored andcommitted
Firestore: Migrate unit tests to assertThrows() (#3562)
1 parent 4e42f9f commit be5a7c1

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

firebase-firestore/src/test/java/com/google/firebase/firestore/FieldPathTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertNotEquals;
19+
import static org.junit.Assert.assertThrows;
1920

2021
import org.junit.Test;
2122
import org.junit.runner.RunWith;
@@ -32,24 +33,24 @@ public void pathWithArray() {
3233
assertEquals("a.b.c", fieldPath.toString());
3334
}
3435

35-
@Test(expected = IllegalArgumentException.class)
36+
@Test
3637
public void emptyPathIsInvalid() {
37-
FieldPath.fromDotSeparatedPath("");
38+
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath(""));
3839
}
3940

40-
@Test(expected = IllegalArgumentException.class)
41+
@Test
4142
public void emptyFirstSegmentIsInvalid() {
42-
FieldPath.fromDotSeparatedPath(".a");
43+
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath(".a"));
4344
}
4445

45-
@Test(expected = IllegalArgumentException.class)
46+
@Test
4647
public void emptyLastSegmentIsInvalid() {
47-
FieldPath.fromDotSeparatedPath("a.");
48+
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath("a."));
4849
}
4950

50-
@Test(expected = IllegalArgumentException.class)
51+
@Test
5152
public void emptyMiddleSegmentIsInvalid() {
52-
FieldPath.fromDotSeparatedPath("a..b");
53+
assertThrows(IllegalArgumentException.class, () -> FieldPath.fromDotSeparatedPath("a..b"));
5354
}
5455

5556
@Test

firebase-firestore/src/test/java/com/google/firebase/firestore/bundle/BundleReaderTest.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.firebase.firestore.testutil.TestUtil.version;
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.assertThrows;
2425

2526
import com.google.firebase.firestore.core.Query;
2627
import com.google.firebase.firestore.core.Target;
@@ -219,55 +220,59 @@ public void testReadsWithoutDocumentOrQuery() throws IOException, JSONException
219220
verifyAllElements(bundleReader);
220221
}
221222

222-
@Test(expected = IllegalArgumentException.class)
223+
@Test
223224
public void testThrowsWithoutLengthPrefix() throws IOException, JSONException {
224225
String bundle = "{metadata: 'no length prefix' }";
225226

226227
BundleReader bundleReader =
227228
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
228229

229-
bundleReader.getBundleMetadata();
230+
assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
230231
}
231232

232-
@Test(expected = IllegalArgumentException.class)
233+
@Test
233234
public void testThrowsWithMissingBrackets() throws IOException, JSONException {
234235
String bundle = "3abc";
235236

236237
BundleReader bundleReader =
237238
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
238-
bundleReader.getBundleMetadata();
239+
240+
assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
239241
}
240242

241-
@Test(expected = JSONException.class)
243+
@Test
242244
public void testThrowsWithInvalidJSON() throws IOException, JSONException {
243245
String bundle = "3{abc}";
244246

245247
BundleReader bundleReader =
246248
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
247-
bundleReader.getBundleMetadata();
249+
250+
assertThrows(JSONException.class, () -> bundleReader.getBundleMetadata());
248251
}
249252

250-
@Test(expected = IllegalArgumentException.class)
253+
@Test
251254
public void testThrowsWhenSecondElementIsMissing() throws IOException, JSONException {
252255
TestBundleBuilder bundleBuilder = new TestBundleBuilder(TEST_PROJECT);
253256
String bundle =
254257
bundleBuilder.build("bundle-1", /* createTimeMicros= */ 6000000L, /* version= */ 1) + "foo";
255258

256259
BundleReader bundleReader =
257260
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
258-
bundleReader.getNextElement();
261+
262+
assertThrows(IllegalArgumentException.class, () -> bundleReader.getNextElement());
259263
}
260264

261-
@Test(expected = IllegalArgumentException.class)
265+
@Test
262266
public void testThrowsWhenBundleDoesNotContainEnoughData() throws IOException, JSONException {
263267
String bundle = "3{}";
264268

265269
BundleReader bundleReader =
266270
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
267-
bundleReader.getBundleMetadata();
271+
272+
assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
268273
}
269274

270-
@Test(expected = IllegalArgumentException.class)
275+
@Test
271276
public void testWhenFirstElementIsNotBundleMetadata() throws IOException, JSONException {
272277
String json =
273278
String.format(
@@ -283,7 +288,7 @@ public void testWhenFirstElementIsNotBundleMetadata() throws IOException, JSONEx
283288
BundleReader bundleReader =
284289
new BundleReader(SERIALIZER, new ByteArrayInputStream(bundle.getBytes(UTF8_CHARSET)));
285290

286-
bundleReader.getBundleMetadata();
291+
assertThrows(IllegalArgumentException.class, () -> bundleReader.getBundleMetadata());
287292
}
288293

289294
@Test

firebase-firestore/src/test/java/com/google/firebase/firestore/bundle/BundleSerializerTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.firebase.firestore.testutil.TestUtil.orderBy;
2121
import static java.util.Arrays.asList;
2222
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThrows;
2324

2425
import com.google.firebase.firestore.core.Query;
2526
import com.google.firebase.firestore.core.Target;
@@ -596,32 +597,32 @@ public void testDecodesEndBeforeQuery() throws JSONException {
596597
assertDecodesNamedQuery(json, query);
597598
}
598599

599-
@Test(expected = IllegalArgumentException.class)
600+
@Test
600601
public void testDoesNotDecodeOffset() throws JSONException {
601602
String json = "{ from: [ { collectionId: 'coll' } ], offset: 5 }";
602603
Query query = TestUtil.query("coll");
603-
assertDecodesNamedQuery(json, query);
604+
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
604605
}
605606

606-
@Test(expected = IllegalArgumentException.class)
607+
@Test
607608
public void testDoesNotDecodeSelect() throws JSONException {
608609
String json = "{ from: [ { collectionId: 'coll' } ], select: [] }";
609610
Query query = TestUtil.query("coll");
610-
assertDecodesNamedQuery(json, query);
611+
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
611612
}
612613

613-
@Test(expected = IllegalArgumentException.class)
614+
@Test
614615
public void testDoesNotDecodeMissingCollection() throws JSONException {
615616
String json = "{ from: [ ] }";
616617
Query query = TestUtil.query("coll");
617-
assertDecodesNamedQuery(json, query);
618+
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
618619
}
619620

620-
@Test(expected = IllegalArgumentException.class)
621+
@Test
621622
public void testDoesNotDecodeMultipleCollections() throws JSONException {
622623
String json = "{ from: [ { collectionId: 'c1' }, { collectionId: 'c2' } ] }";
623624
Query query = TestUtil.query("coll");
624-
assertDecodesNamedQuery(json, query);
625+
assertThrows(IllegalArgumentException.class, () -> assertDecodesNamedQuery(json, query));
625626
}
626627

627628
// BundleMetadata tests

firebase-firestore/src/test/java/com/google/firebase/firestore/model/DocumentKeyTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
import static java.util.Arrays.asList;
1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertNotEquals;
20+
import static org.junit.Assert.assertThrows;
2021

2122
import com.google.firebase.firestore.testutil.ComparatorTester;
2223
import java.util.Collections;
24+
import java.util.List;
2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
2527
import org.robolectric.RobolectricTestRunner;
@@ -57,8 +59,9 @@ public void testComparison() {
5759
.testCompare();
5860
}
5961

60-
@Test(expected = Throwable.class)
62+
@Test
6163
public void testUnevenNumberOfSegmentsAreRejected() {
62-
DocumentKey.fromSegments(Collections.singletonList("a"));
64+
List<String> segments = Collections.singletonList("a");
65+
assertThrows(Throwable.class, () -> DocumentKey.fromSegments(segments));
6366
}
6467
}

0 commit comments

Comments
 (0)