|
16 | 16 |
|
17 | 17 | package com.google.common.collect;
|
18 | 18 |
|
| 19 | +import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows; |
19 | 20 | import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
|
20 | 21 | import static com.google.common.truth.Truth.assertThat;
|
21 | 22 | import static com.google.common.truth.Truth.assertWithMessage;
|
@@ -132,20 +133,12 @@ public void testCopyOf_arrayOfOneElement() {
|
132 | 133 | }
|
133 | 134 |
|
134 | 135 | public void testCopyOf_nullArray() {
|
135 |
| - try { |
136 |
| - copyOf((String[]) null); |
137 |
| - fail(); |
138 |
| - } catch (NullPointerException expected) { |
139 |
| - } |
| 136 | + assertThrows(NullPointerException.class, () -> copyOf((String[]) null)); |
140 | 137 | }
|
141 | 138 |
|
142 | 139 | public void testCopyOf_arrayContainingOnlyNull() {
|
143 | 140 | @Nullable String[] array = new @Nullable String[] {null};
|
144 |
| - try { |
145 |
| - copyOf((String[]) array); |
146 |
| - fail(); |
147 |
| - } catch (NullPointerException expected) { |
148 |
| - } |
| 141 | + assertThrows(NullPointerException.class, () -> copyOf((String[]) array)); |
149 | 142 | }
|
150 | 143 |
|
151 | 144 | public void testCopyOf_collection_empty() {
|
@@ -178,11 +171,7 @@ public void testCopyOf_collection_general() {
|
178 | 171 |
|
179 | 172 | public void testCopyOf_collectionContainingNull() {
|
180 | 173 | Collection<@Nullable String> c = MinimalCollection.of("a", null, "b");
|
181 |
| - try { |
182 |
| - copyOf((Collection<String>) c); |
183 |
| - fail(); |
184 |
| - } catch (NullPointerException expected) { |
185 |
| - } |
| 174 | + assertThrows(NullPointerException.class, () -> copyOf((Collection<String>) c)); |
186 | 175 | }
|
187 | 176 |
|
188 | 177 | enum TestEnum {
|
@@ -228,11 +217,7 @@ public void testCopyOf_iterator_general() {
|
228 | 217 |
|
229 | 218 | public void testCopyOf_iteratorContainingNull() {
|
230 | 219 | Iterator<@Nullable String> c = Iterators.forArray("a", null, "b");
|
231 |
| - try { |
232 |
| - copyOf((Iterator<String>) c); |
233 |
| - fail(); |
234 |
| - } catch (NullPointerException expected) { |
235 |
| - } |
| 220 | + assertThrows(NullPointerException.class, () -> copyOf((Iterator<String>) c)); |
236 | 221 | }
|
237 | 222 |
|
238 | 223 | private static class CountingIterable implements Iterable<String> {
|
@@ -398,76 +383,59 @@ public void testComplexBuilder() {
|
398 | 383 | abstract int getComplexBuilderSetLastElement();
|
399 | 384 |
|
400 | 385 | public void testBuilderAddHandlesNullsCorrectly() {
|
| 386 | + { |
401 | 387 | ImmutableSet.Builder<String> builder = this.<String>builder();
|
402 |
| - try { |
403 |
| - builder.add((String) null); |
404 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
405 |
| - } catch (NullPointerException expected) { |
| 388 | + assertThrows(NullPointerException.class, () -> builder.add((String) null)); |
406 | 389 | }
|
407 | 390 |
|
408 |
| - builder = this.<String>builder(); |
409 |
| - try { |
410 |
| - builder.add((String[]) null); |
411 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
412 |
| - } catch (NullPointerException expected) { |
| 391 | + { |
| 392 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
| 393 | + assertThrows(NullPointerException.class, () -> builder.add((String[]) null)); |
413 | 394 | }
|
414 | 395 |
|
415 |
| - builder = this.<String>builder(); |
416 |
| - try { |
417 |
| - builder.add("a", (String) null); |
418 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
419 |
| - } catch (NullPointerException expected) { |
| 396 | + { |
| 397 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
| 398 | + assertThrows(NullPointerException.class, () -> builder.add("a", (String) null)); |
420 | 399 | }
|
421 | 400 |
|
422 |
| - builder = this.<String>builder(); |
423 |
| - try { |
424 |
| - builder.add("a", "b", (String) null); |
425 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
426 |
| - } catch (NullPointerException expected) { |
| 401 | + { |
| 402 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
| 403 | + assertThrows(NullPointerException.class, () -> builder.add("a", "b", (String) null)); |
427 | 404 | }
|
428 | 405 |
|
429 |
| - builder = this.<String>builder(); |
430 |
| - try { |
431 |
| - builder.add("a", "b", "c", null); |
432 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
433 |
| - } catch (NullPointerException expected) { |
| 406 | + { |
| 407 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
| 408 | + assertThrows(NullPointerException.class, () -> builder.add("a", "b", "c", null)); |
434 | 409 | }
|
435 | 410 |
|
436 |
| - builder = this.<String>builder(); |
437 |
| - try { |
438 |
| - builder.add("a", "b", null, "c"); |
439 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
440 |
| - } catch (NullPointerException expected) { |
| 411 | + { |
| 412 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
| 413 | + assertThrows(NullPointerException.class, () -> builder.add("a", "b", null, "c")); |
441 | 414 | }
|
442 | 415 | }
|
443 | 416 |
|
444 | 417 | public void testBuilderAddAllHandlesNullsCorrectly() {
|
| 418 | + { |
445 | 419 | ImmutableSet.Builder<String> builder = this.<String>builder();
|
446 |
| - try { |
447 |
| - builder.addAll((Iterable<String>) null); |
448 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
449 |
| - } catch (NullPointerException expected) { |
| 420 | + assertThrows(NullPointerException.class, () -> builder.addAll((Iterable<String>) null)); |
450 | 421 | }
|
451 | 422 |
|
452 |
| - try { |
453 |
| - builder.addAll((Iterator<String>) null); |
454 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
455 |
| - } catch (NullPointerException expected) { |
| 423 | + { |
| 424 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
| 425 | + assertThrows(NullPointerException.class, () -> builder.addAll((Iterator<String>) null)); |
456 | 426 | }
|
457 | 427 |
|
458 |
| - builder = this.<String>builder(); |
| 428 | + { |
| 429 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
459 | 430 | List<@Nullable String> listWithNulls = asList("a", null, "b");
|
460 |
| - try { |
461 |
| - builder.addAll((List<String>) listWithNulls); |
462 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
463 |
| - } catch (NullPointerException expected) { |
| 431 | + assertThrows(NullPointerException.class, () -> builder.addAll((List<String>) listWithNulls)); |
464 | 432 | }
|
465 | 433 |
|
| 434 | + { |
| 435 | + ImmutableSet.Builder<String> builder = this.<String>builder(); |
466 | 436 | Iterable<@Nullable String> iterableWithNulls = MinimalIterable.of("a", null, "b");
|
467 |
| - try { |
468 |
| - builder.addAll((Iterable<String>) iterableWithNulls); |
469 |
| - fail("expected NullPointerException"); // COV_NF_LINE |
470 |
| - } catch (NullPointerException expected) { |
| 437 | + assertThrows( |
| 438 | + NullPointerException.class, () -> builder.addAll((Iterable<String>) iterableWithNulls)); |
471 | 439 | }
|
472 | 440 | }
|
473 | 441 |
|
|
0 commit comments