Here you can find the source of assertEquals(String message, Object expected, Object actual)
Parameter | Description |
---|---|
message | the identifying message for the AssertionError ( <code>null</code> okay) |
expected | expected value |
actual | actual value |
public static void assertEquals(String message, Object expected, Object actual)
//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 + ">"; } }