Skip to content

Commit fdfbed1

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Migrate some tests and testing infrastructure from AssertionFailedError to AssertionError.
This is a baby step away from JUnit 3, but my actual motivation was to further take advantage of `AssertionError(String, Throwable)`, which we can now use because it was [added in API Level 19](https://developer.android.com/reference/java/lang/AssertionError#AssertionError(java.lang.String,%20java.lang.Throwable)). RELNOTES=`testing`: Some test libraries now throw `AssertionError` (instead of the more specific `AssertionFailedError`) in some cases. PiperOrigin-RevId: 642289963
1 parent c2bbd73 commit fdfbed1

File tree

22 files changed

+121
-187
lines changed

22 files changed

+121
-187
lines changed

android/guava-testlib/src/com/google/common/collect/testing/AbstractIteratorTester.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.NoSuchElementException;
3131
import java.util.Set;
3232
import java.util.Stack;
33-
import junit.framework.AssertionFailedError;
3433
import org.checkerframework.checker.nullness.qual.Nullable;
3534

3635
/**
@@ -99,7 +98,7 @@ void assertPermitted(Exception exception) {
9998
+ exception.getClass().getSimpleName()
10099
+ " was thrown; expected "
101100
+ getMessage();
102-
Helpers.fail(exception, message);
101+
throw new AssertionError(message, exception);
103102
}
104103
}
105104

@@ -355,8 +354,8 @@ private void compareResultsForThisListOfStimuli() {
355354
try {
356355
stimuli[i].executeAndCompare(reference, target);
357356
verify(reference.getElements());
358-
} catch (AssertionFailedError cause) {
359-
Helpers.fail(cause, "failed with stimuli " + subListCopy(stimuli, i + 1));
357+
} catch (AssertionError cause) {
358+
throw new AssertionError("failed with stimuli " + subListCopy(stimuli, i + 1), cause);
360359
}
361360
}
362361
}
@@ -425,12 +424,12 @@ private <T extends Iterator<E>> void internalExecuteAndCompare(
425424
} catch (PermittedMetaException e) {
426425
referenceException = e;
427426
} catch (UnknownElementException e) {
428-
Helpers.fail(e, e.getMessage());
427+
throw new AssertionError(e);
429428
}
430429

431430
if (referenceException == null) {
432431
if (targetException != null) {
433-
Helpers.fail(targetException, "Target threw exception when reference did not");
432+
throw new AssertionError("Target threw exception when reference did not", targetException);
434433
}
435434

436435
/*

android/guava-testlib/src/com/google/common/collect/testing/Helpers.java

+8-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static junit.framework.Assert.assertEquals;
2121
import static junit.framework.Assert.assertFalse;
2222
import static junit.framework.Assert.assertTrue;
23+
import static junit.framework.Assert.fail;
2324

2425
import com.google.common.annotations.GwtCompatible;
2526
import com.google.common.annotations.GwtIncompatible;
@@ -40,8 +41,6 @@
4041
import java.util.Map;
4142
import java.util.Map.Entry;
4243
import java.util.Set;
43-
import junit.framework.Assert;
44-
import junit.framework.AssertionFailedError;
4544
import org.checkerframework.checker.nullness.qual.Nullable;
4645

4746
@GwtCompatible(emulated = true)
@@ -88,13 +87,13 @@ private static boolean isEmpty(Iterable<?> iterable) {
8887

8988
public static void assertEmpty(Iterable<?> iterable) {
9089
if (!isEmpty(iterable)) {
91-
Assert.fail("Not true that " + iterable + " is empty");
90+
fail("Not true that " + iterable + " is empty");
9291
}
9392
}
9493

9594
public static void assertEmpty(Map<?, ?> map) {
9695
if (!map.isEmpty()) {
97-
Assert.fail("Not true that " + map + " is empty");
96+
fail("Not true that " + map + " is empty");
9897
}
9998
}
10099

@@ -104,7 +103,7 @@ public static void assertEqualInOrder(Iterable<?> expected, Iterable<?> actual)
104103

105104
while (expectedIter.hasNext() && actualIter.hasNext()) {
106105
if (!equal(expectedIter.next(), actualIter.next())) {
107-
Assert.fail(
106+
fail(
108107
"contents were not equal and in the same order: "
109108
+ "expected = "
110109
+ expected
@@ -115,7 +114,7 @@ public static void assertEqualInOrder(Iterable<?> expected, Iterable<?> actual)
115114

116115
if (expectedIter.hasNext() || actualIter.hasNext()) {
117116
// actual either had too few or too many elements
118-
Assert.fail(
117+
fail(
119118
"contents were not equal and in the same order: "
120119
+ "expected = "
121120
+ expected
@@ -139,7 +138,7 @@ public static void assertEqualIgnoringOrder(Iterable<?> expected, Iterable<?> ac
139138
// Yeah it's n^2.
140139
for (Object object : exp) {
141140
if (!act.remove(object)) {
142-
Assert.fail(
141+
fail(
143142
"did not contain expected element "
144143
+ object
145144
+ ", "
@@ -170,7 +169,7 @@ public static void assertContains(Iterable<?> actual, Object expected) {
170169
}
171170

172171
if (!contained) {
173-
Assert.fail("Not true that " + actual + " contains " + expected);
172+
fail("Not true that " + actual + " contains " + expected);
174173
}
175174
}
176175

@@ -182,7 +181,7 @@ public static void assertContainsAllOf(Iterable<?> actual, Object... expected) {
182181
}
183182

184183
if (!expectedList.isEmpty()) {
185-
Assert.fail("Not true that " + actual + " contains all of " + Arrays.asList(expected));
184+
fail("Not true that " + actual + " contains all of " + Arrays.asList(expected));
186185
}
187186
}
188187

@@ -252,12 +251,6 @@ public void remove() {
252251
return iterator.next();
253252
}
254253

255-
static void fail(Throwable cause, Object message) {
256-
AssertionFailedError assertionFailedError = new AssertionFailedError(String.valueOf(message));
257-
assertionFailedError.initCause(cause);
258-
throw assertionFailedError;
259-
}
260-
261254
private static class EntryComparator<K extends @Nullable Object, V extends @Nullable Object>
262255
implements Comparator<Entry<K, V>> {
263256
final @Nullable Comparator<? super K> keyComparator;

android/guava-testlib/src/com/google/common/collect/testing/ReserializingTestCollectionGenerator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ static <T> T reserialize(T object) {
5959
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
6060
return (T) in.readObject();
6161
} catch (IOException | ClassNotFoundException e) {
62-
Helpers.fail(e, e.getMessage());
62+
throw new AssertionError(e);
6363
}
64-
throw new AssertionError("not reachable");
6564
}
6665

6766
@Override

android/guava-testlib/src/com/google/common/testing/AbstractPackageSanityTests.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.TreeMap;
4444
import java.util.logging.Level;
4545
import java.util.logging.Logger;
46-
import junit.framework.AssertionFailedError;
4746
import junit.framework.TestCase;
4847
import org.junit.Test;
4948

@@ -307,7 +306,7 @@ protected final void ignoreClasses(Predicate<? super Class<?>> condition) {
307306
this.classFilter = and(this.classFilter, not(condition));
308307
}
309308

310-
private static AssertionFailedError sanityError(
309+
private static AssertionError sanityError(
311310
Class<?> cls, List<String> explicitTestNames, String description, Throwable e) {
312311
String message =
313312
String.format(
@@ -318,9 +317,7 @@ private static AssertionFailedError sanityError(
318317
cls,
319318
explicitTestNames.get(0),
320319
cls.getName());
321-
AssertionFailedError error = new AssertionFailedError(message);
322-
error.initCause(e);
323-
return error;
320+
return new AssertionError(message, e);
324321
}
325322

326323
/**

android/guava-testlib/src/com/google/common/testing/ClassSanityTester.java

+7-17
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,7 @@ public FactoryMethodReturnValueTester testNulls() throws Exception {
456456
try {
457457
nullPointerTester.testAllPublicInstanceMethods(instance);
458458
} catch (AssertionError e) {
459-
AssertionError error =
460-
new AssertionFailedError("Null check failed on return value of " + factory);
461-
error.initCause(e);
462-
throw error;
459+
throw new AssertionError("Null check failed on return value of " + factory, e);
463460
}
464461
}
465462
}
@@ -505,10 +502,8 @@ public FactoryMethodReturnValueTester testSerializable() throws Exception {
505502
try {
506503
SerializableTester.reserialize(instance);
507504
} catch (Exception e) { // sneaky checked exception
508-
AssertionError error =
509-
new AssertionFailedError("Serialization failed on return value of " + factory);
510-
error.initCause(e.getCause());
511-
throw error;
505+
throw new AssertionError(
506+
"Serialization failed on return value of " + factory, e.getCause());
512507
}
513508
}
514509
}
@@ -537,16 +532,11 @@ public FactoryMethodReturnValueTester testEqualsAndSerializable() throws Excepti
537532
try {
538533
SerializableTester.reserializeAndAssert(instance);
539534
} catch (Exception e) { // sneaky checked exception
540-
AssertionError error =
541-
new AssertionFailedError("Serialization failed on return value of " + factory);
542-
error.initCause(e.getCause());
543-
throw error;
535+
throw new AssertionError(
536+
"Serialization failed on return value of " + factory, e.getCause());
544537
} catch (AssertionFailedError e) {
545-
AssertionError error =
546-
new AssertionFailedError(
547-
"Return value of " + factory + " reserialized to an unequal value");
548-
error.initCause(e);
549-
throw error;
538+
throw new AssertionError(
539+
"Return value of " + factory + " reserialized to an unequal value", e);
550540
}
551541
}
552542
}

android/guava-testlib/src/com/google/common/testing/NullPointerTester.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.List;
4747
import java.util.concurrent.ConcurrentMap;
4848
import junit.framework.Assert;
49-
import junit.framework.AssertionFailedError;
5049
import org.checkerframework.checker.nullness.qual.Nullable;
5150

5251
/**
@@ -377,19 +376,17 @@ private void testParameter(
377376
if (policy.isExpectedType(cause)) {
378377
return;
379378
}
380-
AssertionFailedError error =
381-
new AssertionFailedError(
382-
String.format(
383-
"wrong exception thrown from %s when passing null to %s parameter at index %s.%n"
384-
+ "Full parameters: %s%n"
385-
+ "Actual exception message: %s",
386-
invokable,
387-
invokable.getParameters().get(paramIndex).getType(),
388-
paramIndex,
389-
Arrays.toString(params),
390-
cause));
391-
error.initCause(cause);
392-
throw error;
379+
throw new AssertionError(
380+
String.format(
381+
"wrong exception thrown from %s when passing null to %s parameter at index %s.%n"
382+
+ "Full parameters: %s%n"
383+
+ "Actual exception message: %s",
384+
invokable,
385+
invokable.getParameters().get(paramIndex).getType(),
386+
paramIndex,
387+
Arrays.toString(params),
388+
cause),
389+
cause);
393390
} catch (IllegalAccessException e) {
394391
throw new RuntimeException(e);
395392
}

android/guava-testlib/test/com/google/common/collect/testing/IteratorTesterTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Iterator;
2727
import java.util.List;
2828
import java.util.NoSuchElementException;
29-
import junit.framework.AssertionFailedError;
3029
import junit.framework.TestCase;
3130

3231
/**
@@ -149,7 +148,7 @@ protected Iterator<Integer> newTargetIterator() {
149148
return new IteratorWithSunJavaBug6529795<>(iterator);
150149
}
151150
}.test();
152-
} catch (AssertionFailedError e) {
151+
} catch (AssertionError e) {
153152
return;
154153
}
155154
fail("Should have caught jdk6 bug in target iterator");
@@ -201,18 +200,18 @@ protected Iterator<Integer> newTargetIterator() {
201200

202201
@Override
203202
protected void verify(List<Integer> elements) {
204-
throw new AssertionFailedError(message);
203+
throw new AssertionError(message);
205204
}
206205
};
207-
AssertionFailedError actual = null;
206+
AssertionError actual = null;
208207
try {
209208
tester.test();
210-
} catch (AssertionFailedError e) {
209+
} catch (AssertionError e) {
211210
actual = e;
212211
}
213212
assertNotNull("verify() should be able to cause test failure", actual);
214213
assertTrue(
215-
"AssertionFailedError should have info about why test failed",
214+
"AssertionError should have info about why test failed",
216215
actual.getCause().getMessage().contains(message));
217216
}
218217

@@ -323,7 +322,7 @@ public boolean hasNext() {
323322
private static void assertFailure(IteratorTester<?> tester) {
324323
try {
325324
tester.test();
326-
} catch (AssertionFailedError expected) {
325+
} catch (AssertionError expected) {
327326
return;
328327
}
329328
fail();

android/guava-tests/test/com/google/common/graph/PackageSanityTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121

2222
import com.google.common.testing.AbstractPackageSanityTests;
23-
import junit.framework.AssertionFailedError;
2423

2524
/**
2625
* Covers basic sanity checks for the entire package.
@@ -68,7 +67,7 @@ public PackageSanityTests() {
6867
public void testNulls() throws Exception {
6968
try {
7069
super.testNulls();
71-
} catch (AssertionFailedError e) {
70+
} catch (AssertionError e) {
7271
assertWithMessage("Method did not throw null pointer OR element not in graph exception.")
7372
.that(e)
7473
.hasCauseThat()

android/guava-tests/test/com/google/common/util/concurrent/AbstractFutureTest.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -1208,11 +1208,8 @@ private static int findStackFrame(ExecutionException e, String clazz, String met
12081208
return i;
12091209
}
12101210
}
1211-
AssertionFailedError failure =
1212-
new AssertionFailedError(
1213-
"Expected element " + clazz + "." + method + " not found in stack trace");
1214-
failure.initCause(e);
1215-
throw failure;
1211+
throw new AssertionError(
1212+
"Expected element " + clazz + "." + method + " not found in stack trace", e);
12161213
}
12171214

12181215
private ExecutionException getExpectingExecutionException(AbstractFuture<String> future)

android/guava-tests/test/com/google/common/util/concurrent/TestThread.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ public void tearDown() throws Exception {
7777
join();
7878

7979
if (uncaughtThrowable != null) {
80-
throw (AssertionFailedError)
81-
new AssertionFailedError("Uncaught throwable in " + getName())
82-
.initCause(uncaughtThrowable);
80+
throw new AssertionError("Uncaught throwable in " + getName(), uncaughtThrowable);
8381
}
8482
}
8583

@@ -283,7 +281,7 @@ private static class Response {
283281

284282
Object getResult() {
285283
if (throwable != null) {
286-
throw (AssertionFailedError) new AssertionFailedError().initCause(throwable);
284+
throw new AssertionError(throwable);
287285
}
288286
return result;
289287
}

0 commit comments

Comments
 (0)