public class Asserts
extends java.lang.Object
NOTE Selected routines in this class are originally copied from TestNG, to allow us to make assertions without having to introduce a runtime dependency on TestNG.
Modifier and Type | Class and Description |
---|---|
static class |
Asserts.BooleanWithMessage |
static class |
Asserts.ShouldHaveFailedPreviouslyAssertionError
Subclass of
AssertionError which indicates that code which should have thrown did not,
so that callers can disambiguate expected errors from this one. |
Modifier and Type | Field and Description |
---|---|
static Duration |
DEFAULT_LONG_TIMEOUT
Timeout for use when something should happen within several seconds,
but there might be network calls or computation so
DEFAULT_SHORT_TIMEOUT is not applicable. |
static Duration |
DEFAULT_SHORT_TIMEOUT
Timeout for use when waiting for other threads to finish.
|
static Duration |
DEFAULT_TIMEOUT
Deprecated.
since 0.9.0 use
DEFAULT_LONG_TIMEOUT |
Modifier and Type | Method and Description |
---|---|
static void |
assertEquals(boolean actual,
boolean expected)
Asserts that two booleans are equal.
|
static void |
assertEquals(boolean actual,
boolean expected,
java.lang.String message)
Asserts that two booleans are equal.
|
static void |
assertEquals(byte actual,
byte expected)
Asserts that two bytes are equal.
|
static void |
assertEquals(byte actual,
byte expected,
java.lang.String message)
Asserts that two bytes are equal.
|
static void |
assertEquals(char actual,
char expected)
Asserts that two chars are equal.
|
static void |
assertEquals(char actual,
char expected,
java.lang.String message)
Asserts that two chars are equal.
|
static void |
assertEquals(java.util.Collection<?> actual,
java.util.Collection<?> expected)
Asserts that two collections contain the same elements in the same order.
|
static void |
assertEquals(java.util.Collection<?> actual,
java.util.Collection<?> expected,
java.lang.String message)
Asserts that two collections contain the same elements in the same order.
|
static void |
assertEquals(double actual,
double expected,
double delta)
Asserts that two doubles are equal within a delta.
|
static void |
assertEquals(double actual,
double expected,
double delta,
java.lang.String message)
Asserts that two doubles are equal within a delta.
|
static void |
assertEquals(float actual,
float expected,
float delta)
Asserts that two floats are equal within a delta.
|
static void |
assertEquals(float actual,
float expected,
float delta,
java.lang.String message)
Asserts that two floats are equal within a delta.
|
static void |
assertEquals(int actual,
int expected)
Asserts that two ints are equal.
|
static void |
assertEquals(int actual,
int expected,
java.lang.String message)
Asserts that two ints are equal.
|
static void |
assertEquals(java.lang.Iterable<?> actual,
java.lang.Iterable<?> expected)
Asserts that two iterables return iterators with the same elements in the same order.
|
static void |
assertEquals(java.lang.Iterable<?> actual,
java.lang.Iterable<?> expected,
java.lang.String message)
Asserts that two iterables return iterators with the same elements in the same order.
|
static void |
assertEquals(java.util.Iterator<?> actual,
java.util.Iterator<?> expected)
Asserts that two iterators return the same elements in the same order.
|
static void |
assertEquals(java.util.Iterator<?> actual,
java.util.Iterator<?> expected,
java.lang.String message)
Asserts that two iterators return the same elements in the same order.
|
static void |
assertEquals(long actual,
long expected)
Asserts that two longs are equal.
|
static void |
assertEquals(long actual,
long expected,
java.lang.String message)
Asserts that two longs are equal.
|
static void |
assertEquals(java.util.Map<?,?> actual,
java.util.Map<?,?> expected)
Asserts that two maps are equal.
|
static void |
assertEquals(java.lang.Object[] actual,
java.lang.Object[] expected)
Asserts that two arrays contain the same elements in the same order.
|
static void |
assertEquals(java.lang.Object[] actual,
java.lang.Object[] expected,
java.lang.String message)
Asserts that two arrays contain the same elements in the same order.
|
static void |
assertEquals(java.lang.Object actual,
java.lang.Object expected)
Asserts that two objects are equal.
|
static void |
assertEquals(java.lang.Object actual,
java.lang.Object expected,
java.lang.String message)
Asserts that two objects are equal.
|
static void |
assertEquals(java.util.Set<?> actual,
java.util.Set<?> expected)
Asserts that two sets are equal.
|
static void |
assertEquals(java.util.Set<?> actual,
java.util.Set<?> expected,
java.lang.String message)
Assert set equals
|
static void |
assertEquals(short actual,
short expected)
Asserts that two shorts are equal.
|
static void |
assertEquals(short actual,
short expected,
java.lang.String message)
Asserts that two shorts are equal.
|
static void |
assertEquals(java.lang.String actual,
java.lang.String expected)
Asserts that two Strings are equal.
|
static void |
assertEquals(java.lang.String actual,
java.lang.String expected,
java.lang.String message)
Asserts that two Strings are equal.
|
static void |
assertEqualsIgnoringOrder(java.lang.Iterable<?> actual,
java.lang.Iterable<?> expected) |
static void |
assertEqualsIgnoringOrder(java.lang.Iterable<?> actual,
java.lang.Iterable<?> expected,
boolean logDuplicates,
java.lang.String errmsg) |
static void |
assertFails(java.util.concurrent.Callable<?> c) |
static void |
assertFails(java.lang.Runnable r) |
static void |
assertFailsWith(java.util.concurrent.Callable<?> c,
|
static void |
assertFailsWith(java.util.concurrent.Callable<?> c,
groovy.lang.Closure<java.lang.Boolean> exceptionChecker) |
static void |
assertFailsWith(java.lang.Runnable r,
|
static void |
assertFailsWith(java.lang.Runnable c,
java.lang.Class<? extends java.lang.Throwable> validException,
java.lang.Class<? extends java.lang.Throwable>... otherValidExceptions) |
static void |
assertFalse(boolean condition,
java.lang.String message)
Asserts that a condition is false.
|
static void |
assertNotNull(java.lang.Object object)
Assert that an object reference is not null.
|
static void |
assertNotNull(java.lang.Object object,
java.lang.String message)
Assert that an object reference is not null.
|
static void |
assertNull(java.lang.Object object)
Assert that an object reference is null.
|
static void |
assertNull(java.lang.Object object,
java.lang.String message)
Assert that an object reference is null.
|
static void |
assertReturnsEventually(java.lang.Runnable r,
Duration timeout) |
static void |
assertStringContains(java.lang.String input,
java.lang.String phrase1ToContain,
java.lang.String... optionalOtherPhrasesToContain) |
static void |
assertStringContainsAtLeastOne(java.lang.String input,
java.lang.String possiblePhrase1ToContain,
java.lang.String... optionalOtherPossiblePhrasesToContain) |
static void |
assertStringContainsIgnoreCase(java.lang.String input,
java.lang.String phrase1ToContain,
java.lang.String... optionalOtherPhrasesToContain) |
static void |
assertStringMatchesRegex(java.lang.String input,
java.lang.String regex1ToMatch,
java.lang.String... optionalOtherRegexesToMatch) |
static <T> void |
assertThat(T object,
|
static void |
assertTrue(boolean condition)
Asserts that a condition is true.
|
static void |
assertTrue(boolean condition,
java.lang.String message)
Asserts that a condition is true.
|
static <T> void |
continually(
As
#continually(Supplier, Predicate, Duration, Duration, String) with defaults. |
static <T> void |
continually(
Asserts that continually the supplier gives a value accepted by the predicate.
|
static <T> void |
continually(java.util.Map<java.lang.String,?> flags,
Deprecated.
since 0.9.0 use
#continually(Supplier, Predicate, Duration, Duration, String) |
static <T> void |
continually(java.util.Map<java.lang.String,?> flags,
Deprecated.
since 0.9.0 use
#continually(Supplier, Predicate, Duration, Duration, String) |
static <T> void |
eventually(
As
#eventually(Supplier, Predicate, Duration, Duration, String) with defaults. |
static <T> void |
eventually(
As
#eventually(Supplier, Predicate, Duration, Duration, String) with default. |
static <T> void |
eventually(
Asserts that eventually the supplier gives a value accepted by the predicate.
|
static <T> void |
eventually(java.util.Map<java.lang.String,?> flags,
Deprecated.
since 0.9.0 use
#eventually(Supplier, Predicate, Duration, Duration, String) |
static <T> void |
eventually(java.util.Map<java.lang.String,?> flags,
Deprecated.
since 0.9.0 use
#eventually(Supplier, Predicate, Duration, Duration, String) |
static <T> void |
eventuallyOnNotify(java.lang.Object notifyTarget,
As
#eventuallyOnNotify(Object, Supplier, Predicate, Duration, boolean) with default timeout. |
static <T> void |
eventuallyOnNotify(java.lang.Object notifyTarget,
as
#eventually(Supplier, Predicate) for cases where an object is notified;
more efficient as it waits on the notify target object. |
static <T> void |
eventuallyOnNotify(T object,
As
#eventuallyOnNotify(Object, Predicate, Duration) with the default duration of #eventuallyOnNotify(Object, Supplier, Predicate) . |
static <T> void |
eventuallyOnNotify(T object,
Convenience for
#eventuallyOnNotify(Object, Supplier, Predicate, Duration, boolean)
when the notify target and the value under test are the same. |
static void |
expectedFailure(java.lang.Throwable e)
Tests that an exception is not
Asserts.ShouldHaveFailedPreviouslyAssertionError . |
static void |
expectedFailureContains(java.lang.Throwable e,
java.lang.String phrase1ToContain,
java.lang.String... optionalOtherPhrasesToContain)
Tests
expectedFailure(Throwable) and that the toString
satisfies assertStringContains(String, String, String...) . |
static void |
expectedFailureContainsIgnoreCase(java.lang.Throwable e,
java.lang.String phrase1ToContain,
java.lang.String... optionalOtherPhrasesToContain)
As
expectedFailureContains(Throwable, String, String...) but case insensitive |
static void |
expectedFailureOfType(java.lang.Throwable e,
java.lang.Class<?>... permittedSupertypes)
Tests that an exception is not
Asserts.ShouldHaveFailedPreviouslyAssertionError
and is either one of the given types, or has a caused-by of one of the given types. |
static java.lang.AssertionError |
fail(java.lang.String message)
Fails a test with the given message.
|
static void |
shouldHaveFailedPreviously()
Throws a
Asserts.ShouldHaveFailedPreviouslyAssertionError exception,
to more easily distinguish this failure from other fails. |
static void |
shouldHaveFailedPreviously(java.lang.String message)
As
shouldHaveFailedPreviously() but allowing detail,
for example a value which was received when it shouldn't have been. |
static <T> T |
succeedsContinually(java.util.concurrent.Callable<T> c) |
static <T> T |
succeedsContinually(java.util.Map<?,?> flags,
java.util.concurrent.Callable<T> job) |
static <T> void |
succeedsContinually(java.util.Map<?,?> flags,
java.lang.Runnable r) |
static <T> void |
succeedsContinually(java.lang.Runnable r) |
static <T> T |
succeedsEventually(java.util.concurrent.Callable<T> c) |
static <T> T |
succeedsEventually(java.util.Map<java.lang.String,?> flags,
java.util.concurrent.Callable<T> c)
Convenience method for cases where we need to test until something is true.
|
static void |
succeedsEventually(java.util.Map<java.lang.String,?> flags,
java.lang.Runnable r) |
static void |
succeedsEventually(java.lang.Runnable r) |
public static final Duration DEFAULT_LONG_TIMEOUT
DEFAULT_SHORT_TIMEOUT
is not applicable.public static final Duration DEFAULT_SHORT_TIMEOUT
Long enough for parallel execution to catch up, even on overloaded mediocre test boxes most of the time, but short enough not to irritate you when your test is failing.
@Deprecated public static final Duration DEFAULT_TIMEOUT
DEFAULT_LONG_TIMEOUT
public static void assertNull(java.lang.Object object)
object
- The object reference.java.lang.AssertionError
- if the assertion fails.public static void assertNotNull(java.lang.Object object)
object
- The object reference.java.lang.AssertionError
- if the assertion fails.public static void assertNull(java.lang.Object object, java.lang.String message)
object
- The object reference.message
- The assertion error message.java.lang.AssertionError
- if the assertion fails.public static void assertNotNull(java.lang.Object object, java.lang.String message)
object
- The object reference.message
- The assertion error message.java.lang.AssertionError
- if the assertion fails.public static void assertEquals(java.util.Collection<?> actual, java.util.Collection<?> expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(java.util.Collection<?> actual, java.util.Collection<?> expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(java.util.Iterator<?> actual, java.util.Iterator<?> expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(java.util.Iterator<?> actual, java.util.Iterator<?> expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(java.lang.Iterable<?> actual, java.lang.Iterable<?> expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(java.lang.Iterable<?> actual, java.lang.Iterable<?> expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(java.util.Set<?> actual, java.util.Set<?> expected)
public static void assertEquals(java.util.Set<?> actual, java.util.Set<?> expected, java.lang.String message)
public static void assertEquals(java.util.Map<?,?> actual, java.util.Map<?,?> expected)
public static void assertEquals(java.lang.Object[] actual, java.lang.Object[] expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(java.lang.Object[] actual, java.lang.Object[] expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(java.lang.Object actual, java.lang.Object expected)
actual
- the actual valueexpected
- the expected valuejava.lang.AssertionError
- if the values are not equal.public static void assertEquals(java.lang.Object actual, java.lang.Object expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagejava.lang.AssertionError
- if the values are not equal.public static void assertEquals(java.lang.String actual, java.lang.String expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(java.lang.String actual, java.lang.String expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(double actual, double expected, double delta, java.lang.String message)
actual
- the actual valueexpected
- the expected valuedelta
- the absolute tolerable difference between the actual and expected valuesmessage
- the assertion error messagepublic static void assertEquals(double actual, double expected, double delta)
actual
- the actual valueexpected
- the expected valuedelta
- the absolute tolerable difference between the actual and expected valuespublic static void assertEquals(float actual, float expected, float delta, java.lang.String message)
actual
- the actual valueexpected
- the expected valuedelta
- the absolute tolerable difference between the actual and expected valuesmessage
- the assertion error messagepublic static void assertEquals(float actual, float expected, float delta)
actual
- the actual valueexpected
- the expected valuedelta
- the absolute tolerable difference between the actual and expected valuespublic static void assertEquals(long actual, long expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(long actual, long expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(boolean actual, boolean expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(boolean actual, boolean expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(byte actual, byte expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(byte actual, byte expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(char actual, char expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(char actual, char expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(short actual, short expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(short actual, short expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertEquals(int actual, int expected, java.lang.String message)
actual
- the actual valueexpected
- the expected valuemessage
- the assertion error messagepublic static void assertEquals(int actual, int expected)
actual
- the actual valueexpected
- the expected valuepublic static void assertTrue(boolean condition)
condition
- the condition to evaluatepublic static void assertTrue(boolean condition, java.lang.String message)
condition
- the condition to evaluatemessage
- the assertion error messagepublic static void assertFalse(boolean condition, java.lang.String message)
condition
- the condition to evaluatemessage
- the assertion error messagepublic static java.lang.AssertionError fail(java.lang.String message)
message
- the assertion error messagepublic static void assertEqualsIgnoringOrder(java.lang.Iterable<?> actual, java.lang.Iterable<?> expected)
public static void assertEqualsIgnoringOrder(java.lang.Iterable<?> actual, java.lang.Iterable<?> expected, boolean logDuplicates, java.lang.String errmsg)
public static <T> void eventually(supplier, predicate)
#eventually(Supplier, Predicate, Duration, Duration, String)
with defaults.@Deprecated public static <T> void eventually(java.util.Map<java.lang.String,?> flags,supplier, predicate)
#eventually(Supplier, Predicate, Duration, Duration, String)
@Deprecated public static <T> void eventually(java.util.Map<java.lang.String,?> flags,supplier, predicate, java.lang.String errMsg)
#eventually(Supplier, Predicate, Duration, Duration, String)
public static <T> void eventually(supplier, predicate, Duration timeout)
#eventually(Supplier, Predicate, Duration, Duration, String)
with default.public static <T> void eventually(supplier, predicate, Duration timeout, Duration period, java.lang.String errMsg)
supplier
- supplies the value to test, such as Suppliers#ofInstance(Object)
for a constantpredicate
- the Predicate
to apply to each value given by the suppliertimeout
- how long to wait, default DEFAULT_SHORT_TIMEOUT
period
- how often to check, default quite often so you won't notice but letting the CPU do workerrMsg
- an error message to display if not satisfied, in addition to the last-tested supplied value and the predicatepublic static <T> void continually(supplier, predicate)
#continually(Supplier, Predicate, Duration, Duration, String)
with defaults.@Deprecated public static <T> void continually(java.util.Map<java.lang.String,?> flags,supplier, predicate)
#continually(Supplier, Predicate, Duration, Duration, String)
@Deprecated public static <T> void continually(java.util.Map<java.lang.String,?> flags,supplier, predicate, java.lang.String errMsg)
#continually(Supplier, Predicate, Duration, Duration, String)
public static <T> void continually(supplier, predicate, Duration duration, Duration period, java.lang.String errMsg)
supplier
- supplies the value to test, such as Suppliers#ofInstance(Object)
for a constantpredicate
- the Predicate
to apply to each value given by the supplierduration
- how long to test for, default DEFAULT_SHORT_TIMEOUT
period
- how often to check, default quite often to minimise chance of missing a flashing violation but letting the CPU do workerrMsg
- an error message to display if not satisfied, in addition to the last-tested supplied value and the predicatepublic static void succeedsEventually(java.lang.Runnable r)
succeedsContinually(Map, Callable)
public static void succeedsEventually(java.util.Map<java.lang.String,?> flags, java.lang.Runnable r)
succeedsContinually(Map, Callable)
public static <T> T succeedsEventually(java.util.concurrent.Callable<T> c)
succeedsContinually(Map, Callable)
public static <T> T succeedsEventually(java.util.Map<java.lang.String,?> flags, java.util.concurrent.Callable<T> c)
The following flags are supported:
DEFAULT_LONG_TIMEOUT
)
flags,
- accepts the flags listed abovec
- The callable to invokepublic static <T> void succeedsContinually(java.lang.Runnable r)
public static <T> void succeedsContinually(java.util.Map<?,?> flags, java.lang.Runnable r)
public static <T> T succeedsContinually(java.util.concurrent.Callable<T> c)
public static <T> T succeedsContinually(java.util.Map<?,?> flags, java.util.concurrent.Callable<T> job)
public static void assertFails(java.lang.Runnable r)
public static void assertFails(java.util.concurrent.Callable<?> c)
public static void assertFailsWith(java.util.concurrent.Callable<?> c, groovy.lang.Closure<java.lang.Boolean> exceptionChecker)
@SafeVarargs public static void assertFailsWith(java.lang.Runnable c, java.lang.Class<? extends java.lang.Throwable> validException, java.lang.Class<? extends java.lang.Throwable>... otherValidExceptions)
public static void assertFailsWith(java.lang.Runnable r,exceptionChecker)
public static void assertFailsWith(java.util.concurrent.Callable<?> c,exceptionChecker)
public static void assertReturnsEventually(java.lang.Runnable r, Duration timeout) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
public static <T> void assertThat(T object,condition)
public static void assertStringContains(java.lang.String input, java.lang.String phrase1ToContain, java.lang.String... optionalOtherPhrasesToContain)
public static void assertStringContainsAtLeastOne(java.lang.String input, java.lang.String possiblePhrase1ToContain, java.lang.String... optionalOtherPossiblePhrasesToContain)
public static void assertStringContainsIgnoreCase(java.lang.String input, java.lang.String phrase1ToContain, java.lang.String... optionalOtherPhrasesToContain)
public static void assertStringMatchesRegex(java.lang.String input, java.lang.String regex1ToMatch, java.lang.String... optionalOtherRegexesToMatch)
public static void shouldHaveFailedPreviously()
Asserts.ShouldHaveFailedPreviouslyAssertionError
exception,
to more easily distinguish this failure from other fails.
In particular, use one of the expectedFailure
methods
in the surrounding catch
block and this error will pass through it.public static void shouldHaveFailedPreviously(java.lang.String message)
shouldHaveFailedPreviously()
but allowing detail,
for example a value which was received when it shouldn't have been.public static void expectedFailure(java.lang.Throwable e)
Asserts.ShouldHaveFailedPreviouslyAssertionError
.
See shouldHaveFailedPreviously()
public static void expectedFailureOfType(java.lang.Throwable e, java.lang.Class<?>... permittedSupertypes)
Asserts.ShouldHaveFailedPreviouslyAssertionError
and is either one of the given types, or has a caused-by of one of the given types.
If you want *just* the instanceof (without checking the caused-by), then just catch
those exception types, treat that as success, and let any other exception be propagated.public static void expectedFailureContains(java.lang.Throwable e, java.lang.String phrase1ToContain, java.lang.String... optionalOtherPhrasesToContain)
expectedFailure(Throwable)
and that the toString
satisfies assertStringContains(String, String, String...)
.public static void expectedFailureContainsIgnoreCase(java.lang.Throwable e, java.lang.String phrase1ToContain, java.lang.String... optionalOtherPhrasesToContain)
expectedFailureContains(Throwable, String, String...)
but case insensitivepublic static <T> void eventuallyOnNotify(java.lang.Object notifyTarget,supplier, predicate)
#eventuallyOnNotify(Object, Supplier, Predicate, Duration, boolean)
with default timeout.public static <T> void eventuallyOnNotify(java.lang.Object notifyTarget,supplier, predicate, Duration timeout)
#eventually(Supplier, Predicate)
for cases where an object is notified;
more efficient as it waits on the notify target object.
See also the simpler #eventuallyOnNotify(Object, Predicate)
when looking at a collection which is getting notified.
Timeout defaults to DEFAULT_SHORT_TIMEOUT
.
This synchronizes on the notify target for the duration of the wait, including while getting and checking the value, so as not to miss any notification.
public static <T> void eventuallyOnNotify(T object,predicate, Duration timeout)
#eventuallyOnNotify(Object, Supplier, Predicate, Duration, boolean)
when the notify target and the value under test are the same.public static <T> void eventuallyOnNotify(T object,predicate)
#eventuallyOnNotify(Object, Predicate, Duration)
with the default duration of #eventuallyOnNotify(Object, Supplier, Predicate)
.