A set of assertion methods useful for writing tests. Only failed assertions are recorded.
These methods can be used directly:
Assert.assertEquals(...)
, however, they
read better if they are referenced through static import:
import static org.junit.Assert.*;
...
assertEquals(...);
assertArrayEquals
public static void assertArrayEquals(Object[] expecteds,
Object[] actuals)
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(String message,
Object[] expecteds,
Object[] actuals)
throws org.junit.internal.ArrayComparisonFailure
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(String message,
byte[] expecteds,
byte[] actuals)
throws org.junit.internal.ArrayComparisonFailure
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(String message,
char[] expecteds,
char[] actuals)
throws org.junit.internal.ArrayComparisonFailure
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(String message,
int[] expecteds,
int[] actuals)
throws org.junit.internal.ArrayComparisonFailure
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(String message,
long[] expecteds,
long[] actuals)
throws org.junit.internal.ArrayComparisonFailure
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(String message,
short[] expecteds,
short[] actuals)
throws org.junit.internal.ArrayComparisonFailure
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(byte[] expecteds,
byte[] actuals)
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(char[] expecteds,
char[] actuals)
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(int[] expecteds,
int[] actuals)
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(long[] expecteds,
long[] actuals)
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertArrayEquals
public static void assertArrayEquals(short[] expecteds,
short[] actuals)
TODO: fix javadoc
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
assertEquals
public static void assertEquals(Object expected,
Object actual)
Asserts that two objects are equal. If they are not, an AssertionError
without a message is thrown. If expected
and actual
are null
, they are considered equal.
expected
- expected valueactual
- the value to check against expected
assertEquals
public static void assertEquals(String message,
Object expected,
Object actual)
Asserts that two objects are equal. If they are not, an AssertionError
is thrown with the given message. If expected
and actual
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expected
- expected valueactual
- actual value
assertEquals
public static void assertEquals(String message,
double expected,
double actual,
double delta)
Asserts that two doubles or floats are equal to within a positive delta. If they
are not, an AssertionError
is thrown with the given message. If the
expected value is infinity then the delta value is ignored. NaNs are
considered equal:
assertEquals(Double.NaN, Double.NaN, *)
passes
message
- the identifying message or null
for the AssertionError
expected
- expected valueactual
- the value to check against expected
delta
- the maximum delta between expected
and actual
for which
both numbers are still considered equal.
assertEquals
public static void assertEquals(double expected,
double actual,
double delta)
Asserts that two doubles or floats are equal to within a positive delta. If they
are not, an AssertionError
is thrown. If the
expected value is infinity then the delta value is ignored.NaNs are
considered equal:
assertEquals(Double.NaN, Double.NaN, *)
passes
expected
- expected valueactual
- the value to check against expected
delta
- the maximum delta between expected
and actual
for which
both numbers are still considered equal.
assertFalse
public static void assertFalse(String message,
boolean condition)
Asserts that a condition is false. If it isn't it throws an
AssertionError
with the given message.
message
- the identifying message or null
for the AssertionError
condition
- condition to be checked
assertFalse
public static void assertFalse(boolean condition)
Asserts that a condition is false. If it isn't it throws an
AssertionError
without a message.
condition
- condition to be checked
assertNotNull
public static void assertNotNull(Object object)
Asserts that an object isn't null. If it is an AssertionError
is
thrown.
object
- Object to check or null
assertNotNull
public static void assertNotNull(String message,
Object object)
Asserts that an object isn't null. If it is an AssertionError
is
thrown with the given message.
message
- the identifying message or null
for the AssertionError
object
- Object to check or null
assertNotSame
public static void assertNotSame(Object unexpected,
Object actual)
Asserts that two objects do not refer to the same object. If they do
refer to the same object, an AssertionError
without a message is thrown.
unexpected
- the object you don't expectactual
- the object to compare to unexpected
assertNotSame
public static void assertNotSame(String message,
Object unexpected,
Object actual)
Asserts that two objects do not refer to the same object. If they do
refer to the same object, an AssertionError
is thrown with the given
message.
message
- the identifying message or null
for the AssertionError
unexpected
- the object you don't expectactual
- the object to compare to unexpected
assertNull
public static void assertNull(Object object)
Asserts that an object is null. If it isn't an AssertionError
is
thrown.
object
- Object to check or null
assertNull
public static void assertNull(String message,
Object object)
Asserts that an object is null. If it is not, an AssertionError
is
thrown with the given message.
message
- the identifying message or null
for the AssertionError
object
- Object to check or null
assertSame
public static void assertSame(Object expected,
Object actual)
Asserts that two objects refer to the same object. If they are not the
same, an AssertionError
without a message is thrown.
expected
- the expected objectactual
- the object to compare to expected
assertSame
public static void assertSame(String message,
Object expected,
Object actual)
Asserts that two objects refer to the same object. If they are not, an
AssertionError
is thrown with the given message.
message
- the identifying message or null
for the AssertionError
expected
- the expected objectactual
- the object to compare to expected
assertTrue
public static void assertTrue(String message,
boolean condition)
Asserts that a condition is true. If it isn't it throws an
AssertionError
with the given message.
message
- the identifying message or null
for the AssertionError
condition
- condition to be checked
assertTrue
public static void assertTrue(boolean condition)
Asserts that a condition is true. If it isn't it throws an
AssertionError
without a message.
condition
- condition to be checked
fail
public static void fail()
Fails a test with no message.
fail
public static void fail(String message)
Fails a test with the given message.
message
- the identifying message or null
for the AssertionError
void assertEquals
public static @Deprecated void assertEquals(Object[] expecteds,
Object[] actuals)
use assertArrayEquals
Asserts that two object arrays are equal. If they are not, an AssertionError
is thrown. If expected
and actual
are null
,
they are considered equal.
expecteds
- Object array or array of arrays (multi-dimensional array) with expected valuesactuals
- Object array or array of arrays (multi-dimensional array) with actual values
void assertEquals
public static @Deprecated void assertEquals(String message,
Object[] expecteds,
Object[] actuals)
use assertArrayEquals
Asserts that two object arrays are equal. If they are not, an
AssertionError
is thrown with the given message. If expecteds
and
actuals
are null
, they are considered equal.
message
- the identifying message or null
for the AssertionError
expecteds
- Object array or array of arrays (multi-dimensional array) with expected values.actuals
- Object array or array of arrays (multi-dimensional array) with actual values