Java Assert Equal assertEquals(String message, Object expected, Object actual)

Here you can find the source of assertEquals(String message, Object expected, Object actual)

Description

Asserts that two objects are equal.

License

Apache License

Parameter

Parameter Description
message the identifying message for the AssertionError ( <code>null</code> okay)
expected expected value
actual actual value

Declaration

public static void assertEquals(String message, Object expected, Object actual) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  www  . j  a  va  2  s .  c om*/
     * Asserts that two objects are equal. If they are not, an
     * {@link AssertionError} is thrown with the given message. If
     * <code>expected</code> and <code>actual</code> are <code>null</code>, they
     * are considered equal.
     * 
     * @param message
     *            the identifying message for the {@link AssertionError} (
     *            <code>null</code> okay)
     * @param expected
     *            expected value
     * @param actual
     *            actual value
     */
    public static void assertEquals(String message, Object expected, Object actual) {
        if (expected == null && actual == null)
            return;
        if (expected != null && isEquals(expected, actual))
            return;
        failNotEquals(message, expected, actual);
    }

    private static boolean isEquals(Object expected, Object actual) {
        return expected.equals(actual);
    }

    private static void failNotEquals(String message, Object expected, Object actual) {
        fail(format(message, expected, actual));
    }

    /**
     * Fails a test with the given message.
     * 
     * @param message
     *            the identifying message for the {@link AssertionError} (
     *            <code>null</code> okay)
     * @see AssertionError
     */
    public static void fail(String message) {
        throw new IllegalStateException(message == null ? "" : message);
    }

    static String format(String message, Object expected, Object actual) {
        String formatted = "";
        if (message != null && !message.equals(""))
            formatted = message + " ";
        String expectedString = String.valueOf(expected);
        String actualString = String.valueOf(actual);
        if (expectedString.equals(actualString))
            return formatted + "expected: " + formatClassAndValue(expected, expectedString) + " but was: "
                    + formatClassAndValue(actual, actualString);
        else
            return formatted + "expected:<" + expectedString + "> but was:<" + actualString + ">";
    }

    private static String formatClassAndValue(Object value, String valueString) {
        String className = value == null ? "null" : value.getClass().getName();
        return className + "<" + valueString + ">";
    }
}

Related

  1. assertEquals(Object obj1, Object obj2, String errMsg)
  2. assertEquals(Object obj1, Object obj2, String message)
  3. assertEquals(Object val1, Object val2)
  4. assertEquals(String expected, String actual)
  5. assertEquals(String expected, String current)
  6. assertEquals(T expected, T actual)
  7. assertEqualsAny(Object obj1, Object[] objects, String errMsg)
  8. assertEqualsToStringBuilder(final StringBuilder errorMessageHolder, final String currentEM, final Object expected, final Object actual)