Java Utililty Methods Assert Equal

List of utility methods to do Assert Equal

Description

The list of methods to do Assert Equal are organized into topic(s).

Method

doubleassertAboveEqual(final double number, final double value)
assert Above Equal
if (number < value) {
    return value;
return number;
voidassertApproxEquals(double a, double b, double relative, double absolute)
assert Approx Equals
assertApproxEquals(a, b, relative, absolute, null);
booleanassertArrayEquals(double[] p1, double[] p2, double eps)
assert Array Equals
if (p1.length != p2.length) {
    return false;
for (int i = 0; i < p1.length; i++) {
    if (Math.abs(p1[i] - p2[i]) > eps) {
        return false;
return true;
voidassertArrayLengthEqual(Object[] array, String name, Object[] array1, String name1)
Check if the lengths of the two arrays are equal.
if (array.length != array1.length) {
    throw new IllegalArgumentException("The length of " + name + " should be the same as that of " + name1);
booleanassertCharacterArraysEqual(char[] first, char[] second)
Asserts that the specified first array of characters is identical to the specified second array of characters.
int firstLength = first.length;
int secondLength = second.length;
if (firstLength != secondLength) {
    return false;
for (int index = 0; index < firstLength; index++) {
    char firstCharacter = first[index];
    char secondCharacter = second[index];
...
voidassertDatesEqual(Calendar first, Calendar second)
assert Dates Equal
if (first == null && second != null) {
    throw new AssertionError("dates are not equal. first is null, second is not");
} else if (first != null && second == null) {
    throw new AssertionError("dates are not equal. second is null, first is not");
boolean yearsNotEqual = first.get(Calendar.YEAR) != second.get(Calendar.YEAR);
boolean monthsNotEqual = first.get(Calendar.MONTH) != second.get(Calendar.MONTH);
boolean daysNotEqual = first.get(Calendar.DAY_OF_MONTH) != second.get(Calendar.DAY_OF_MONTH);
...
booleanassertEqual(final double d1, final double d2, final double precisionRange)
Asserts if the given two doubles are equal within the given precision range by the operation:
 Math.abs(d1 - d2) < precision 
.
return Math.abs(d1 - d2) < precisionRange;
voidassertEqual(Object obj1, Object obj2)
Method assertEqual.
if (obj1 == null || obj2 == null) {
    throw new RuntimeException("assertion: " + obj1 + " must be equal to " + obj2);
if (obj1 != null && !obj1.equals(obj2)) {
    throw new RuntimeException("assertion: " + obj1 + " must be equal to " + obj2);
if (obj2 != null && !obj2.equals(obj1)) {
    throw new RuntimeException("assertion: " + obj1 + " must be equal to " + obj2);
...
voidassertEquality(Object original, Object equal, Object... notEqual)
assert Equality
if (!original.equals(equal)) {
    throw new AssertionError("Objects where not equal " + original + " != " + equal);
if (original.hashCode() != equal.hashCode()) {
    throw new AssertionError("Equal objects did not have same hash :" + original + "(" + original.hashCode()
            + ")" + " " + equal + "(" + equal.hashCode() + ")");
for (Object notEqualObject : notEqual) {
...
voidassertEquals(double x, double y)
assert Equals
assertEquals(x, y, 1e-10);