public final class Verify extends Assert
Assert
class, which adds useful additional "assert" methods.
You can import this class instead of Assert, and use it thus, e.g.:
Verify.assertEquals("fred", name); // from original Assert class Verify.assertContains("fred", nameList); // from new extensions Verify.assertBefore("fred", "jim", orderedNamesList); // from new extensions
Modifier and Type | Method and Description |
---|---|
static <T> void |
assertAllSatisfy(Iterable<T> iterable,
Predicate<? super T> predicate) |
static <K,V> void |
assertAllSatisfy(Map<K,V> map,
Predicate<? super V> predicate) |
static <T> void |
assertAllSatisfy(String message,
Iterable<T> iterable,
Predicate<? super T> predicate) |
static <T> void |
assertAnySatisfy(Iterable<T> iterable,
Predicate<? super T> predicate) |
static <K,V> void |
assertAnySatisfy(Map<K,V> map,
Predicate<? super V> predicate) |
static <T> void |
assertAnySatisfy(String message,
Iterable<T> iterable,
Predicate<? super T> predicate) |
static void |
assertBagsEqual(Bag<?> expectedBag,
Bag<?> actualBag) |
static void |
assertBagsEqual(String bagName,
Bag<?> expectedBag,
Bag<?> actualBag) |
static void |
assertBefore(Object formerItem,
Object latterItem,
List<?> actualList)
Assert that the formerItem appears before the latterItem in the given
Collection . |
static void |
assertBefore(String listName,
Object formerItem,
Object latterItem,
List<?> actualList)
Assert that the formerItem appears before the latterItem in the given
Collection . |
static <T> void |
assertClassNonInstantiable(Class<T> aClass) |
static void |
assertContains(Object expectedItem,
Collection<?> actualCollection)
Assert that the given
Collection contains the given item. |
static void |
assertContains(Object expectedItem,
ImmutableCollection<?> actualImmutableCollection)
Assert that the given
ImmutableCollection contains the given item. |
static void |
assertContains(String collectionName,
Object expectedItem,
Collection<?> actualCollection)
Assert that the given
Collection contains the given item. |
static void |
assertContains(String immutableCollectionName,
Object expectedItem,
ImmutableCollection<?> actualImmutableCollection)
Assert that the given
ImmutableCollection contains the given item. |
static void |
assertContains(String stringToFind,
String stringToSearch)
Assert that the given
stringToFind is contained within the stringToSearch . |
static void |
assertContains(String stringName,
String stringToFind,
String stringToSearch)
Assert that the given
stringToFind is contained within the stringToSearch . |
static void |
assertContainsAll(Iterable<?> iterable,
Object... items) |
static void |
assertContainsAll(String collectionName,
Iterable<?> iterable,
Object... items) |
static void |
assertContainsAllEntries(Multimap<?,?> actualMultimap,
Object... keyValues)
Assert the given
Multimap contains all of the given keys and values. |
static void |
assertContainsAllEntries(String multimapName,
Multimap<?,?> actualMultimap,
Object... expectedKeyValues)
Assert the given
Multimap contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(ImmutableMapIterable<?,?> immutableMapIterable,
Object... keyValues)
Assert that the given
ImmutableMapIterable contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(Map<?,?> actualMap,
Object... keyValues)
Assert that the given
Map contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(MapIterable<?,?> mapIterable,
Object... keyValues)
Assert that the given
MapIterable contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(MutableMapIterable<?,?> mutableMapIterable,
Object... keyValues)
Assert that the given
MutableMapIterable contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(String immutableMapIterableName,
ImmutableMapIterable<?,?> immutableMapIterable,
Object... expectedKeyValues)
Assert that the given
ImmutableMapIterable contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(String mapName,
Map<?,?> actualMap,
Object... expectedKeyValues)
Assert that the given
Map contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(String mapIterableName,
MapIterable<?,?> mapIterable,
Object... expectedKeyValues)
Assert that the given
MapIterable contains all of the given keys and values. |
static void |
assertContainsAllKeyValues(String mutableMapIterableName,
MutableMapIterable<?,?> mutableMapIterable,
Object... expectedKeyValues)
Assert that the given
MutableMapIterable contains all of the given keys and values. |
static <K,V> void |
assertContainsEntry(K expectedKey,
V expectedValue,
Multimap<K,V> actualMultimap)
Assert that the given
Multimap contains an entry with the given key and value. |
static <K,V> void |
assertContainsEntry(String multimapName,
K expectedKey,
V expectedValue,
Multimap<K,V> actualMultimap)
Assert that the given
Multimap contains an entry with the given key and value. |
static void |
assertContainsKey(Object expectedKey,
ImmutableMapIterable<?,?> immutableMapIterable)
Assert that the given
ImmutableMapIterable contains an entry with the given key. |
static void |
assertContainsKey(Object expectedKey,
Map<?,?> actualMap)
Assert that the given
Map contains an entry with the given key. |
static void |
assertContainsKey(Object expectedKey,
MapIterable<?,?> mapIterable)
Assert that the given
MapIterable contains an entry with the given key. |
static void |
assertContainsKey(Object expectedKey,
MutableMapIterable<?,?> mutableMapIterable)
Assert that the given
MutableMapIterable contains an entry with the given key. |
static void |
assertContainsKey(String immutableMapIterableName,
Object expectedKey,
ImmutableMapIterable<?,?> immutableMapIterable)
Assert that the given
ImmutableMapIterable contains an entry with the given key. |
static void |
assertContainsKey(String mapName,
Object expectedKey,
Map<?,?> actualMap)
Assert that the given
Map contains an entry with the given key. |
static void |
assertContainsKey(String mapIterableName,
Object expectedKey,
MapIterable<?,?> mapIterable)
Assert that the given
MapIterable contains an entry with the given key. |
static void |
assertContainsKey(String mutableMapIterableName,
Object expectedKey,
MutableMapIterable<?,?> mutableMapIterable)
Assert that the given
MutableMapIterable contains an entry with the given key. |
static void |
assertContainsKeyValue(Object expectedKey,
Object expectedValue,
ImmutableMapIterable<?,?> mapIterable)
Assert that the given
ImmutableMapIterable contains an entry with the given key and value. |
static void |
assertContainsKeyValue(Object expectedKey,
Object expectedValue,
Map<?,?> actualMap)
Assert that the given
Map contains an entry with the given key and value. |
static void |
assertContainsKeyValue(Object expectedKey,
Object expectedValue,
MapIterable<?,?> mapIterable)
Assert that the given
MapIterable contains an entry with the given key and value. |
static void |
assertContainsKeyValue(Object expectedKey,
Object expectedValue,
MutableMapIterable<?,?> mapIterable)
Assert that the given
MutableMapIterable contains an entry with the given key and value. |
static void |
assertContainsKeyValue(String mapIterableName,
Object expectedKey,
Object expectedValue,
ImmutableMapIterable<?,?> immutableMapIterable)
Assert that the given
ImmutableMapIterable contains an entry with the given key and value. |
static void |
assertContainsKeyValue(String mapName,
Object expectedKey,
Object expectedValue,
Map<?,?> actualMap)
Assert that the given
Map contains an entry with the given key and value. |
static void |
assertContainsKeyValue(String mapIterableName,
Object expectedKey,
Object expectedValue,
MapIterable<?,?> mapIterable)
Assert that the given
MapIterable contains an entry with the given key and value. |
static void |
assertContainsKeyValue(String mapIterableName,
Object expectedKey,
Object expectedValue,
MutableMapIterable<?,?> mutableMapIterable)
Assert that the given
MutableMapIterable contains an entry with the given key and value. |
static void |
assertContainsNone(Collection<?> actualCollection,
Object... items) |
static <T> void |
assertCount(int expectedCount,
Iterable<T> iterable,
Predicate<? super T> predicate) |
static void |
assertDeserializedForm(String expectedBase64Form,
Object actualObject) |
static void |
assertEmpty(Iterable<?> actualIterable)
Assert that the given
Iterable is empty. |
static void |
assertEmpty(Map<?,?> actualMap)
Assert that the given
Map is empty. |
static void |
assertEmpty(Multimap<?,?> actualMultimap)
Assert that the given
Multimap is empty. |
static void |
assertEmpty(MutableMapIterable<?,?> actualMutableMapIterable)
Assert that the given
MutableMapIterable is empty. |
static void |
assertEmpty(PrimitiveIterable primitiveIterable)
Assert that the given
PrimitiveIterable is empty. |
static void |
assertEmpty(String iterableName,
Iterable<?> actualIterable)
Assert that the given
Collection is empty. |
static void |
assertEmpty(String mapName,
Map<?,?> actualMap)
Assert that the given
Map is empty. |
static void |
assertEmpty(String multimapName,
Multimap<?,?> actualMultimap)
Assert that the given
Multimap is empty. |
static void |
assertEmpty(String mutableMapIterableName,
MutableMapIterable<?,?> actualMutableMapIterable)
Assert that the given
Collection is empty. |
static void |
assertEmpty(String iterableName,
PrimitiveIterable primitiveIterable)
Assert that the given
PrimitiveIterable is empty. |
static <T> void |
assertEndsWith(List<T> list,
T... items) |
static <T> void |
assertEndsWith(T[] array,
T... items) |
static void |
assertEqualsAndHashCode(Object objectA,
Object objectB)
Assert that
objectA and objectB are equal (via the Object.equals(Object) method,
and that they both return the same Object.hashCode() . |
static void |
assertEqualsAndHashCode(String itemNames,
Object objectA,
Object objectB)
Assert that
objectA and objectB are equal (via the Object.equals(Object) method,
and that they both return the same Object.hashCode() . |
static void |
assertError(Class<? extends Error> expectedErrorClass,
Runnable code) |
static void |
assertInstanceOf(Class<?> expectedClassType,
Object actualObject)
Assert that the given object is an instanceof expectedClassType.
|
static void |
assertInstanceOf(String objectName,
Class<?> expectedClassType,
Object actualObject)
Assert that the given object is an instanceof expectedClassType.
|
static void |
assertItemAtIndex(Object expectedItem,
int index,
List<?> list)
|
static void |
assertItemAtIndex(Object expectedItem,
int index,
Object[] array)
Assert that the given
item is at the index in the given array . |
static void |
assertItemAtIndex(String listName,
Object expectedItem,
int index,
List<?> list)
|
static void |
assertItemAtIndex(String arrayName,
Object expectedItem,
int index,
Object[] array)
|
static void |
assertIterableEmpty(Iterable<?> iterable)
Assert that the given
Iterable is empty. |
static void |
assertIterableEmpty(String iterableName,
Iterable<?> iterable)
Assert that the given
Iterable is empty. |
static void |
assertIterableNotEmpty(Iterable<?> iterable)
Assert that the given
Iterable is not empty. |
static void |
assertIterableNotEmpty(String iterableName,
Iterable<?> iterable)
Assert that the given
Iterable is not empty. |
static void |
assertIterablesEqual(Iterable<?> expectedIterable,
Iterable<?> actualIterable) |
static void |
assertIterablesEqual(String iterableName,
Iterable<?> expectedIterable,
Iterable<?> actualIterable) |
static void |
assertIterableSize(int expectedSize,
Iterable<?> actualIterable)
Assert the size of the given
Iterable . |
static void |
assertIterableSize(String iterableName,
int expectedSize,
Iterable<?> actualIterable)
Assert the size of the given
Iterable . |
static void |
assertListsEqual(List<?> expectedList,
List<?> actualList) |
static void |
assertListsEqual(String listName,
List<?> expectedList,
List<?> actualList) |
static void |
assertMapsEqual(Map<?,?> expectedMap,
Map<?,?> actualMap) |
static void |
assertMapsEqual(String mapName,
Map<?,?> expectedMap,
Map<?,?> actualMap) |
static void |
assertNegative(int value)
Asserts that a value is negative.
|
static <T> void |
assertNoneSatisfy(Iterable<T> iterable,
Predicate<? super T> predicate) |
static <K,V> void |
assertNoneSatisfy(Map<K,V> map,
Predicate<? super V> predicate) |
static <T> void |
assertNoneSatisfy(String message,
Iterable<T> iterable,
Predicate<? super T> predicate) |
static void |
assertNotContains(Object unexpectedItem,
Collection<?> actualCollection)
Assert that the given
Collection does not contain the given item. |
static void |
assertNotContains(Object unexpectedItem,
Iterable<?> iterable)
Assert that the given
Iterable does not contain the given item. |
static void |
assertNotContains(String collectionName,
Object unexpectedItem,
Collection<?> actualCollection)
Assert that the given
Collection does not contain the given item. |
static void |
assertNotContains(String collectionName,
Object unexpectedItem,
Iterable<?> iterable)
Assert that the given
Iterable does not contain the given item. |
static void |
assertNotContains(String unexpectedString,
String stringToSearch)
Assert that the given
unexpectedString is not contained within the stringToSearch . |
static void |
assertNotContains(String stringName,
String unexpectedString,
String stringToSearch)
Assert that the given
unexpectedString is not contained within the stringToSearch . |
static void |
assertNotContainsKey(Object unexpectedKey,
Map<?,?> actualMap)
Assert that the given
Collection does not contain the given item. |
static void |
assertNotContainsKey(String mapName,
Object unexpectedKey,
Map<?,?> actualMap)
Assert that the given
Collection does not contain the given item. |
static void |
assertNotEmpty(Iterable<?> actualIterable)
Assert that the given
Iterable is not empty. |
static void |
assertNotEmpty(Map<?,?> actualMap)
Assert that the given
Map is not empty. |
static void |
assertNotEmpty(Multimap<?,?> actualMultimap)
Assert that the given
Multimap is not empty. |
static void |
assertNotEmpty(MutableMapIterable<?,?> actualMutableMapIterable)
Assert that the given
MutableMapIterable is not empty. |
static void |
assertNotEmpty(PrimitiveIterable primitiveIterable)
Assert that the given
PrimitiveIterable is not empty. |
static void |
assertNotEmpty(String iterableName,
Iterable<?> actualIterable)
Assert that the given
Iterable is not empty. |
static void |
assertNotEmpty(String mapName,
Map<?,?> actualMap)
Assert that the given
Map is not empty. |
static void |
assertNotEmpty(String multimapName,
Multimap<?,?> actualMultimap)
Assert that the given
Multimap is not empty. |
static void |
assertNotEmpty(String mutableMapIterableName,
MutableMapIterable<?,?> actualMutableMapIterable)
Assert that the given
MutableMapIterable is not empty. |
static void |
assertNotEmpty(String iterableName,
PrimitiveIterable primitiveIterable)
Assert that the given
PrimitiveIterable is not empty. |
static <T> void |
assertNotEmpty(String itemsName,
T[] items) |
static <T> void |
assertNotEmpty(T[] items) |
static void |
assertNotEquals(boolean notExpected,
boolean actual)
Asserts that two booleans are not equal.
|
static void |
assertNotEquals(byte notExpected,
byte actual)
Asserts that two bytes are not equal.
|
static void |
assertNotEquals(char notExpected,
char actual)
Asserts that two chars are not equal.
|
static void |
assertNotEquals(double notExpected,
double actual,
double delta)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(double, double, double) in JUnit 4.11 instead. |
static void |
assertNotEquals(float expected,
float actual,
float delta)
Asserts that two floats are not equal concerning a delta.
|
static void |
assertNotEquals(int notExpected,
int actual)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(long, long) in JUnit 4.11 instead. |
static void |
assertNotEquals(long notExpected,
long actual)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(long, long) in JUnit 4.11 instead. |
static void |
assertNotEquals(Object item1,
Object item2)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(Object, Object) in JUnit 4.11 instead. |
static void |
assertNotEquals(short notExpected,
short actual)
Asserts that two shorts are not equal.
|
static void |
assertNotEquals(String itemName,
boolean notExpected,
boolean actual)
Asserts that two booleans are not equal.
|
static void |
assertNotEquals(String itemName,
byte notExpected,
byte actual)
Asserts that two bytes are not equal.
|
static void |
assertNotEquals(String itemName,
char notExpected,
char actual)
Asserts that two chars are not equal.
|
static void |
assertNotEquals(String itemName,
double notExpected,
double actual,
double delta)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(String, double, double, double) in JUnit 4.11 instead. |
static void |
assertNotEquals(String itemName,
float notExpected,
float actual,
float delta)
Asserts that two floats are not equal concerning a delta.
|
static void |
assertNotEquals(String itemName,
int notExpected,
int actual)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(String, long, long) in JUnit 4.11 instead. |
static void |
assertNotEquals(String itemName,
long notExpected,
long actual)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(String, long, long) in JUnit 4.11 instead. |
static void |
assertNotEquals(String itemsName,
Object item1,
Object item2)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(String, Object, Object) in JUnit 4.11 instead. |
static void |
assertNotEquals(String itemName,
short notExpected,
short actual)
Asserts that two shorts are not equal.
|
static void |
assertNotEquals(String notExpected,
String actual)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(Object, Object) in JUnit 4.11 instead. |
static void |
assertNotEquals(String itemName,
String notExpected,
String actual)
Deprecated.
in 3.0. Use
Assert.assertNotEquals(String, Object, Object) in JUnit 4.11 instead. |
static void |
assertObjectNotNull(String objectName,
Object actualObject) |
static void |
assertPositive(int value)
Asserts that a value is positive.
|
static void |
assertPostSerializedEqualsAndHashCode(Object object) |
static void |
assertPostSerializedIdentity(Object object) |
static void |
assertSerializedForm(long expectedSerialVersionUID,
String expectedBase64Form,
Object actualObject) |
static void |
assertSerializedForm(String expectedBase64Form,
Object actualObject) |
static void |
assertSetsEqual(Set<?> expectedSet,
Set<?> actualSet) |
static void |
assertSetsEqual(String setName,
Set<?> expectedSet,
Set<?> actualSet) |
static void |
assertShallowClone(Cloneable object) |
static void |
assertShallowClone(String itemName,
Cloneable object) |
static void |
assertSize(int expectedSize,
ImmutableSet<?> actualImmutableSet)
Assert the size of the given
ImmutableSet . |
static void |
assertSize(int expectedSize,
Iterable<?> actualIterable)
Assert the size of the given
Iterable . |
static void |
assertSize(int expectedSize,
Map<?,?> actualMap)
Assert the size of the given
Map . |
static void |
assertSize(int expectedSize,
Multimap<?,?> actualMultimap)
Assert the size of the given
Multimap . |
static void |
assertSize(int expectedSize,
MutableMapIterable<?,?> mutableMapIterable)
Assert the size of the given
MutableMapIterable . |
static void |
assertSize(int expectedSize,
Object[] actualArray)
Assert the size of the given array.
|
static void |
assertSize(int expectedSize,
PrimitiveIterable primitiveIterable)
Assert the size of the given
PrimitiveIterable . |
static void |
assertSize(String immutableSetName,
int expectedSize,
ImmutableSet<?> actualImmutableSet)
Assert the size of the given
ImmutableSet . |
static void |
assertSize(String iterableName,
int expectedSize,
Iterable<?> actualIterable)
Assert the size of the given
Iterable . |
static void |
assertSize(String mapName,
int expectedSize,
Map<?,?> actualMap)
Assert the size of the given
Map . |
static void |
assertSize(String multimapName,
int expectedSize,
Multimap<?,?> actualMultimap)
Assert the size of the given
Multimap . |
static void |
assertSize(String mapName,
int expectedSize,
MutableMapIterable<?,?> mutableMapIterable)
Assert the size of the given
MutableMapIterable . |
static void |
assertSize(String arrayName,
int expectedSize,
Object[] actualArray)
Assert the size of the given array.
|
static void |
assertSize(String primitiveIterableName,
int expectedSize,
PrimitiveIterable actualPrimitiveIterable)
Assert the size of the given
PrimitiveIterable . |
static void |
assertSortedBagsEqual(SortedBag<?> expectedBag,
SortedBag<?> actualBag) |
static void |
assertSortedBagsEqual(String bagName,
SortedBag<?> expectedBag,
SortedBag<?> actualBag) |
static void |
assertSortedMapsEqual(SortedMapIterable<?,?> expectedMap,
SortedMapIterable<?,?> actualMap) |
static void |
assertSortedMapsEqual(String mapName,
SortedMapIterable<?,?> expectedMap,
SortedMapIterable<?,?> actualMap) |
static void |
assertSortedSetsEqual(SortedSet<?> expectedSet,
SortedSet<?> actualSet) |
static void |
assertSortedSetsEqual(String setName,
SortedSet<?> expectedSet,
SortedSet<?> actualSet) |
static <T> void |
assertStartsWith(List<T> list,
T... items) |
static <T> void |
assertStartsWith(String listName,
List<T> list,
T... items) |
static <T> void |
assertStartsWith(T[] array,
T... items) |
static void |
assertThrows(Class<? extends Exception> expectedExceptionClass,
Callable<?> code)
|
static void |
assertThrows(Class<? extends Exception> expectedExceptionClass,
Runnable code)
|
static void |
assertThrowsWithCause(Class<? extends Exception> expectedExceptionClass,
Class<? extends Throwable> expectedCauseClass,
Callable<?> code)
Runs the
Callable code and asserts that it throws an Exception of the type
expectedExceptionClass , which contains a cause of type expectedCauseClass. |
static void |
assertThrowsWithCause(Class<? extends Exception> expectedExceptionClass,
Class<? extends Throwable> expectedCauseClass,
Runnable code)
Runs the
Runnable code and asserts that it throws an Exception of the type
expectedExceptionClass , which contains a cause of type expectedCauseClass. |
static void |
assertZero(int value)
Asserts that a value is positive.
|
static void |
denyContainsAny(Collection<?> actualCollection,
Object... items) |
static void |
denyContainsAny(String collectionName,
Collection<?> actualCollection,
Object... items) |
static void |
denyContainsKey(Object unexpectedKey,
Map<?,?> actualMap)
Deny that the given
Map contains an entry with the given key. |
static void |
denyContainsKey(String mapName,
Object unexpectedKey,
Map<?,?> actualMap)
Deny that the given
Map contains an entry with the given key. |
static void |
fail(String message,
Throwable cause) |
static void |
throwMangledException(AssertionError e)
Mangles the stack trace of
AssertionError so that it looks like its been thrown from the line that
called to a custom assertion. |
static void |
throwMangledException(AssertionError e,
int framesToPop)
Mangles the stack trace of
AssertionError so that it looks like
its been thrown from the line that called to a custom assertion. |
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertTrue, assertTrue, fail, fail
public static void throwMangledException(AssertionError e)
AssertionError
so that it looks like its been thrown from the line that
called to a custom assertion.
This method behaves identically to throwMangledException(AssertionError, int)
and is provided
for convenience for assert methods that only want to pop two stack frames. The only time that you would want to
call the other throwMangledException(AssertionError, int)
method is if you have a custom assert
that calls another custom assert i.e. the source line calling the custom asserts is more than two stack frames
away
e
- The exception to mangle.throwMangledException(AssertionError, int)
public static void throwMangledException(AssertionError e, int framesToPop)
AssertionError
so that it looks like
its been thrown from the line that called to a custom assertion.
This is useful for when you are in a debugging session and you want to go to the source of the problem in the test case quickly. The regular use case for this would be something along the lines of:
public class TestFoo extends junit.framework.TestCase { public void testFoo() throws Exception { Foo foo = new Foo(); ... assertFoo(foo); } // Custom assert private static void assertFoo(Foo foo) { try { assertEquals(...); ... assertSame(...); } catch (AssertionFailedException e) { AssertUtils.throwMangledException(e, 2); } } }
Without the try ... catch
block around lines 11-13 the stack trace following a test failure
would look a little like:
java.lang.AssertionError: ... at TestFoo.assertFoo(TestFoo.java:11) at TestFoo.testFoo(TestFoo.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) ...
Note that the source of the error isn't readily apparent as the first line in the stack trace is the code within the custom assert. If we were debugging the failure we would be more interested in the second line of the stack trace which shows us where in our tests the assert failed.
With the try ... catch
block around lines 11-13 the stack trace would look like the
following:
java.lang.AssertionError: ... at TestFoo.testFoo(TestFoo.java:5) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) ...
Here the source of the error is more visible as we can instantly see that the testFoo test is failing at line 5.
e
- The exception to mangle.framesToPop
- The number of frames to remove from the stack trace.AssertionError
- that was given as an argument with its stack trace mangled.@Deprecated public static void assertNotEquals(String itemsName, Object item1, Object item2)
Assert.assertNotEquals(String, Object, Object)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(Object item1, Object item2)
Assert.assertNotEquals(Object, Object)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(String itemName, String notExpected, String actual)
Assert.assertNotEquals(String, Object, Object)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(String notExpected, String actual)
Assert.assertNotEquals(Object, Object)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(String itemName, double notExpected, double actual, double delta)
Assert.assertNotEquals(String, double, double, double)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(double notExpected, double actual, double delta)
Assert.assertNotEquals(double, double, double)
in JUnit 4.11 instead.public static void assertNotEquals(String itemName, float notExpected, float actual, float delta)
public static void assertNotEquals(float expected, float actual, float delta)
@Deprecated public static void assertNotEquals(String itemName, long notExpected, long actual)
Assert.assertNotEquals(String, long, long)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(long notExpected, long actual)
Assert.assertNotEquals(long, long)
in JUnit 4.11 instead.public static void assertNotEquals(String itemName, boolean notExpected, boolean actual)
public static void assertNotEquals(boolean notExpected, boolean actual)
public static void assertNotEquals(String itemName, byte notExpected, byte actual)
public static void assertNotEquals(byte notExpected, byte actual)
public static void assertNotEquals(String itemName, char notExpected, char actual)
public static void assertNotEquals(char notExpected, char actual)
public static void assertNotEquals(String itemName, short notExpected, short actual)
public static void assertNotEquals(short notExpected, short actual)
@Deprecated public static void assertNotEquals(String itemName, int notExpected, int actual)
Assert.assertNotEquals(String, long, long)
in JUnit 4.11 instead.@Deprecated public static void assertNotEquals(int notExpected, int actual)
Assert.assertNotEquals(long, long)
in JUnit 4.11 instead.public static void assertEmpty(Iterable<?> actualIterable)
Iterable
is empty.public static void assertEmpty(String iterableName, Iterable<?> actualIterable)
Collection
is empty.public static void assertEmpty(MutableMapIterable<?,?> actualMutableMapIterable)
MutableMapIterable
is empty.public static void assertEmpty(String mutableMapIterableName, MutableMapIterable<?,?> actualMutableMapIterable)
Collection
is empty.public static void assertEmpty(PrimitiveIterable primitiveIterable)
PrimitiveIterable
is empty.public static void assertEmpty(String iterableName, PrimitiveIterable primitiveIterable)
PrimitiveIterable
is empty.public static void assertIterableEmpty(Iterable<?> iterable)
Iterable
is empty.public static void assertIterableEmpty(String iterableName, Iterable<?> iterable)
Iterable
is empty.public static void assertInstanceOf(Class<?> expectedClassType, Object actualObject)
public static void assertInstanceOf(String objectName, Class<?> expectedClassType, Object actualObject)
public static void assertEmpty(Multimap<?,?> actualMultimap)
Multimap
is empty.public static void assertEmpty(String multimapName, Multimap<?,?> actualMultimap)
Multimap
is empty.public static void assertEmpty(String mapName, Map<?,?> actualMap)
Map
is empty.public static void assertNotEmpty(Iterable<?> actualIterable)
Iterable
is not empty.public static void assertNotEmpty(String iterableName, Iterable<?> actualIterable)
Iterable
is not empty.public static void assertNotEmpty(MutableMapIterable<?,?> actualMutableMapIterable)
MutableMapIterable
is not empty.public static void assertNotEmpty(String mutableMapIterableName, MutableMapIterable<?,?> actualMutableMapIterable)
MutableMapIterable
is not empty.public static void assertNotEmpty(PrimitiveIterable primitiveIterable)
PrimitiveIterable
is not empty.public static void assertNotEmpty(String iterableName, PrimitiveIterable primitiveIterable)
PrimitiveIterable
is not empty.public static void assertIterableNotEmpty(Iterable<?> iterable)
Iterable
is not empty.public static void assertIterableNotEmpty(String iterableName, Iterable<?> iterable)
Iterable
is not empty.public static void assertNotEmpty(Map<?,?> actualMap)
Map
is not empty.public static void assertNotEmpty(String mapName, Map<?,?> actualMap)
Map
is not empty.public static void assertNotEmpty(Multimap<?,?> actualMultimap)
Multimap
is not empty.public static void assertNotEmpty(String multimapName, Multimap<?,?> actualMultimap)
Multimap
is not empty.public static <T> void assertNotEmpty(String itemsName, T[] items)
public static <T> void assertNotEmpty(T[] items)
public static void assertSize(int expectedSize, Object[] actualArray)
public static void assertSize(String arrayName, int expectedSize, Object[] actualArray)
public static void assertSize(int expectedSize, Iterable<?> actualIterable)
Iterable
.public static void assertSize(String iterableName, int expectedSize, Iterable<?> actualIterable)
Iterable
.public static void assertSize(int expectedSize, PrimitiveIterable primitiveIterable)
PrimitiveIterable
.public static void assertSize(String primitiveIterableName, int expectedSize, PrimitiveIterable actualPrimitiveIterable)
PrimitiveIterable
.public static void assertIterableSize(int expectedSize, Iterable<?> actualIterable)
Iterable
.public static void assertIterableSize(String iterableName, int expectedSize, Iterable<?> actualIterable)
Iterable
.public static void assertSize(String mapName, int expectedSize, Map<?,?> actualMap)
Map
.public static void assertSize(int expectedSize, Map<?,?> actualMap)
Map
.public static void assertSize(int expectedSize, Multimap<?,?> actualMultimap)
Multimap
.public static void assertSize(String multimapName, int expectedSize, Multimap<?,?> actualMultimap)
Multimap
.public static void assertSize(int expectedSize, MutableMapIterable<?,?> mutableMapIterable)
MutableMapIterable
.public static void assertSize(String mapName, int expectedSize, MutableMapIterable<?,?> mutableMapIterable)
MutableMapIterable
.public static void assertSize(int expectedSize, ImmutableSet<?> actualImmutableSet)
ImmutableSet
.public static void assertSize(String immutableSetName, int expectedSize, ImmutableSet<?> actualImmutableSet)
ImmutableSet
.public static void assertContains(String stringToFind, String stringToSearch)
stringToFind
is contained within the stringToSearch
.public static void assertNotContains(String unexpectedString, String stringToSearch)
unexpectedString
is not contained within the stringToSearch
.public static void assertContains(String stringName, String stringToFind, String stringToSearch)
stringToFind
is contained within the stringToSearch
.public static void assertNotContains(String stringName, String unexpectedString, String stringToSearch)
unexpectedString
is not contained within the stringToSearch
.public static <T> void assertCount(int expectedCount, Iterable<T> iterable, Predicate<? super T> predicate)
public static <T> void assertAllSatisfy(Iterable<T> iterable, Predicate<? super T> predicate)
public static <K,V> void assertAllSatisfy(Map<K,V> map, Predicate<? super V> predicate)
public static <T> void assertAllSatisfy(String message, Iterable<T> iterable, Predicate<? super T> predicate)
public static <T> void assertAnySatisfy(Iterable<T> iterable, Predicate<? super T> predicate)
public static <K,V> void assertAnySatisfy(Map<K,V> map, Predicate<? super V> predicate)
public static <T> void assertAnySatisfy(String message, Iterable<T> iterable, Predicate<? super T> predicate)
public static <T> void assertNoneSatisfy(Iterable<T> iterable, Predicate<? super T> predicate)
public static <K,V> void assertNoneSatisfy(Map<K,V> map, Predicate<? super V> predicate)
public static <T> void assertNoneSatisfy(String message, Iterable<T> iterable, Predicate<? super T> predicate)
public static void assertContainsAllKeyValues(Map<?,?> actualMap, Object... keyValues)
Map
contains all of the given keys and values.public static void assertContainsAllKeyValues(String mapName, Map<?,?> actualMap, Object... expectedKeyValues)
Map
contains all of the given keys and values.public static void assertContainsAllKeyValues(MapIterable<?,?> mapIterable, Object... keyValues)
MapIterable
contains all of the given keys and values.public static void assertContainsAllKeyValues(String mapIterableName, MapIterable<?,?> mapIterable, Object... expectedKeyValues)
MapIterable
contains all of the given keys and values.public static void assertContainsAllKeyValues(MutableMapIterable<?,?> mutableMapIterable, Object... keyValues)
MutableMapIterable
contains all of the given keys and values.public static void assertContainsAllKeyValues(String mutableMapIterableName, MutableMapIterable<?,?> mutableMapIterable, Object... expectedKeyValues)
MutableMapIterable
contains all of the given keys and values.public static void assertContainsAllKeyValues(ImmutableMapIterable<?,?> immutableMapIterable, Object... keyValues)
ImmutableMapIterable
contains all of the given keys and values.public static void assertContainsAllKeyValues(String immutableMapIterableName, ImmutableMapIterable<?,?> immutableMapIterable, Object... expectedKeyValues)
ImmutableMapIterable
contains all of the given keys and values.public static void denyContainsAny(Collection<?> actualCollection, Object... items)
public static void assertContainsNone(Collection<?> actualCollection, Object... items)
public static void assertContains(Object expectedItem, Collection<?> actualCollection)
Collection
contains the given item.public static void assertContains(String collectionName, Object expectedItem, Collection<?> actualCollection)
Collection
contains the given item.public static void assertContains(Object expectedItem, ImmutableCollection<?> actualImmutableCollection)
ImmutableCollection
contains the given item.public static void assertContains(String immutableCollectionName, Object expectedItem, ImmutableCollection<?> actualImmutableCollection)
ImmutableCollection
contains the given item.public static void assertContainsAll(String collectionName, Iterable<?> iterable, Object... items)
public static void assertListsEqual(String listName, List<?> expectedList, List<?> actualList)
public static void assertSetsEqual(String setName, Set<?> expectedSet, Set<?> actualSet)
public static void assertSortedSetsEqual(SortedSet<?> expectedSet, SortedSet<?> actualSet)
public static void assertSortedSetsEqual(String setName, SortedSet<?> expectedSet, SortedSet<?> actualSet)
public static void assertSortedBagsEqual(SortedBag<?> expectedBag, SortedBag<?> actualBag)
public static void assertSortedBagsEqual(String bagName, SortedBag<?> expectedBag, SortedBag<?> actualBag)
public static void assertSortedMapsEqual(SortedMapIterable<?,?> expectedMap, SortedMapIterable<?,?> actualMap)
public static void assertSortedMapsEqual(String mapName, SortedMapIterable<?,?> expectedMap, SortedMapIterable<?,?> actualMap)
public static void assertIterablesEqual(Iterable<?> expectedIterable, Iterable<?> actualIterable)
public static void assertIterablesEqual(String iterableName, Iterable<?> expectedIterable, Iterable<?> actualIterable)
public static void assertMapsEqual(String mapName, Map<?,?> expectedMap, Map<?,?> actualMap)
public static void assertBagsEqual(String bagName, Bag<?> expectedBag, Bag<?> actualBag)
public static <K,V> void assertContainsEntry(K expectedKey, V expectedValue, Multimap<K,V> actualMultimap)
Multimap
contains an entry with the given key and value.public static <K,V> void assertContainsEntry(String multimapName, K expectedKey, V expectedValue, Multimap<K,V> actualMultimap)
Multimap
contains an entry with the given key and value.public static void assertContainsAllEntries(Multimap<?,?> actualMultimap, Object... keyValues)
Multimap
contains all of the given keys and values.public static void assertContainsAllEntries(String multimapName, Multimap<?,?> actualMultimap, Object... expectedKeyValues)
Multimap
contains all of the given keys and values.public static void denyContainsAny(String collectionName, Collection<?> actualCollection, Object... items)
public static void assertContainsKey(Object expectedKey, Map<?,?> actualMap)
Map
contains an entry with the given key.public static void assertContainsKey(String mapName, Object expectedKey, Map<?,?> actualMap)
Map
contains an entry with the given key.public static void assertContainsKey(Object expectedKey, MapIterable<?,?> mapIterable)
MapIterable
contains an entry with the given key.public static void assertContainsKey(String mapIterableName, Object expectedKey, MapIterable<?,?> mapIterable)
MapIterable
contains an entry with the given key.public static void assertContainsKey(Object expectedKey, MutableMapIterable<?,?> mutableMapIterable)
MutableMapIterable
contains an entry with the given key.public static void assertContainsKey(String mutableMapIterableName, Object expectedKey, MutableMapIterable<?,?> mutableMapIterable)
MutableMapIterable
contains an entry with the given key.public static void assertContainsKey(Object expectedKey, ImmutableMapIterable<?,?> immutableMapIterable)
ImmutableMapIterable
contains an entry with the given key.public static void assertContainsKey(String immutableMapIterableName, Object expectedKey, ImmutableMapIterable<?,?> immutableMapIterable)
ImmutableMapIterable
contains an entry with the given key.public static void denyContainsKey(Object unexpectedKey, Map<?,?> actualMap)
Map
contains an entry with the given key.public static void denyContainsKey(String mapName, Object unexpectedKey, Map<?,?> actualMap)
Map
contains an entry with the given key.public static void assertContainsKeyValue(Object expectedKey, Object expectedValue, Map<?,?> actualMap)
Map
contains an entry with the given key and value.public static void assertContainsKeyValue(String mapName, Object expectedKey, Object expectedValue, Map<?,?> actualMap)
Map
contains an entry with the given key and value.public static void assertContainsKeyValue(Object expectedKey, Object expectedValue, MapIterable<?,?> mapIterable)
MapIterable
contains an entry with the given key and value.public static void assertContainsKeyValue(String mapIterableName, Object expectedKey, Object expectedValue, MapIterable<?,?> mapIterable)
MapIterable
contains an entry with the given key and value.public static void assertContainsKeyValue(Object expectedKey, Object expectedValue, MutableMapIterable<?,?> mapIterable)
MutableMapIterable
contains an entry with the given key and value.public static void assertContainsKeyValue(String mapIterableName, Object expectedKey, Object expectedValue, MutableMapIterable<?,?> mutableMapIterable)
MutableMapIterable
contains an entry with the given key and value.public static void assertContainsKeyValue(Object expectedKey, Object expectedValue, ImmutableMapIterable<?,?> mapIterable)
ImmutableMapIterable
contains an entry with the given key and value.public static void assertContainsKeyValue(String mapIterableName, Object expectedKey, Object expectedValue, ImmutableMapIterable<?,?> immutableMapIterable)
ImmutableMapIterable
contains an entry with the given key and value.public static void assertNotContains(Object unexpectedItem, Collection<?> actualCollection)
Collection
does not contain the given item.public static void assertNotContains(String collectionName, Object unexpectedItem, Collection<?> actualCollection)
Collection
does not contain the given item.public static void assertNotContains(Object unexpectedItem, Iterable<?> iterable)
Iterable
does not contain the given item.public static void assertNotContains(String collectionName, Object unexpectedItem, Iterable<?> iterable)
Iterable
does not contain the given item.public static void assertNotContainsKey(Object unexpectedKey, Map<?,?> actualMap)
Collection
does not contain the given item.public static void assertNotContainsKey(String mapName, Object unexpectedKey, Map<?,?> actualMap)
Collection
does not contain the given item.public static void assertBefore(Object formerItem, Object latterItem, List<?> actualList)
Collection
.
Both the formerItem and the latterItem must appear in the collection, or this assert will fail.public static void assertBefore(String listName, Object formerItem, Object latterItem, List<?> actualList)
Collection
.
assertContains(String, Object, Collection)
will be called for both the formerItem and the
latterItem, prior to the "before" assertion.public static void assertItemAtIndex(Object expectedItem, int index, List<?> list)
public static void assertItemAtIndex(Object expectedItem, int index, Object[] array)
item
is at the index
in the given array
.public static <T> void assertStartsWith(T[] array, T... items)
public static <T> void assertStartsWith(List<T> list, T... items)
public static <T> void assertEndsWith(List<T> list, T... items)
public static <T> void assertEndsWith(T[] array, T... items)
public static void assertItemAtIndex(String listName, Object expectedItem, int index, List<?> list)
public static void assertItemAtIndex(String arrayName, Object expectedItem, int index, Object[] array)
public static void assertPostSerializedEqualsAndHashCode(Object object)
public static void assertPostSerializedIdentity(Object object)
public static void assertSerializedForm(String expectedBase64Form, Object actualObject)
public static void assertSerializedForm(long expectedSerialVersionUID, String expectedBase64Form, Object actualObject)
public static void assertDeserializedForm(String expectedBase64Form, Object actualObject)
public static void assertEqualsAndHashCode(Object objectA, Object objectB)
objectA
and objectB
are equal (via the Object.equals(Object)
method,
and that they both return the same Object.hashCode()
.public static void assertNegative(int value)
public static void assertPositive(int value)
public static void assertZero(int value)
public static void assertEqualsAndHashCode(String itemNames, Object objectA, Object objectB)
objectA
and objectB
are equal (via the Object.equals(Object)
method,
and that they both return the same Object.hashCode()
.public static void assertShallowClone(Cloneable object)
public static <T> void assertClassNonInstantiable(Class<T> aClass)
public static void assertError(Class<? extends Error> expectedErrorClass, Runnable code)
public static void assertThrows(Class<? extends Exception> expectedExceptionClass, Callable<?> code)
Callable
code
and asserts that it throws an Exception
of the type
expectedExceptionClass
.
Callable
is most appropriate when a checked exception will be thrown.
If a subclass of RuntimeException
will be thrown, the form
assertThrows(Class, Runnable)
may be more convenient.
e.g.
Verify.assertThrows(StringIndexOutOfBoundsException.class, new Callable<String>() { public String call() throws Exception { return "Craig".substring(42, 3); } });
assertThrows(Class, Runnable)
public static void assertThrows(Class<? extends Exception> expectedExceptionClass, Runnable code)
Runnable
code
and asserts that it throws an Exception
of the type
expectedExceptionClass
.
Runnable
is most appropriate when a subclass of RuntimeException
will be thrown.
If a checked exception will be thrown, the form assertThrows(Class, Callable)
may be more
convenient.
e.g.
Verify.assertThrows(NullPointerException.class, new Runnable() { public void run() { final Integer integer = null; LOGGER.info(integer.toString()); } });
assertThrows(Class, Callable)
public static void assertThrowsWithCause(Class<? extends Exception> expectedExceptionClass, Class<? extends Throwable> expectedCauseClass, Callable<?> code)
Callable
code
and asserts that it throws an Exception
of the type
expectedExceptionClass
, which contains a cause of type expectedCauseClass.
Callable
is most appropriate when a checked exception will be thrown.
If a subclass of RuntimeException
will be thrown, the form
assertThrowsWithCause(Class, Class, Runnable)
may be more convenient.
e.g.
Verify.assertThrowsWithCause(RuntimeException.class, IOException.class, new Callable() { public Void call() throws Exception { try { new File("").createNewFile(); } catch (final IOException e) { throw new RuntimeException("Uh oh!", e); } return null; } });
public static void assertThrowsWithCause(Class<? extends Exception> expectedExceptionClass, Class<? extends Throwable> expectedCauseClass, Runnable code)
Runnable
code
and asserts that it throws an Exception
of the type
expectedExceptionClass
, which contains a cause of type expectedCauseClass.
Runnable
is most appropriate when a subclass of RuntimeException
will be thrown.
If a checked exception will be thrown, the form assertThrowsWithCause(Class, Class, Callable)
may be more convenient.
e.g.
Verify.assertThrowsWithCause(RuntimeException.class, StringIndexOutOfBoundsException.class, new Runnable() { public void run() { try { LOGGER.info("Craig".substring(42, 3)); } catch (final StringIndexOutOfBoundsException e) { throw new RuntimeException("Uh oh!", e); } } });
Copyright © 2004–2016. All rights reserved.