Custom Assertions

Some custom assertion techniques.


Equality of arrays

The simplest implementation is this:

public void assertArrayEquals(Object[] expected, Object[] actual) {
assertEquals(Arrays.asList(expected), Arrays.asList(actual));
}

Stop. Go use this and come back only when you feel it's not good enough.

You may eventually find you need more diagnostic information. In that case, here is some code. Feel free to refactor, but please keep it simple. I will refactor regularly. -- JbRainsberger

public class ArrayDifference? {

public Object expected;// If you prefer get/set, by all means... public Object actual;

public ArrayDifference?(Object anExpected, Object anActual) { expected = anExpected; actual = anActual; }

public String toString() { return ("Expected <" + expected + "> but got <" + actual + ">"; }

}

public abstract class MyTestFixture? extends TestCase {

public void assertEqualArrays(Object[] expected, Object[] actual) { if (expected == actual) { return; } assertNotNull("expected is null", expected); assertNotNull("actual is null", actual); assertEquals(expected.length, actual.length); final List differences = new ArrayList(); for (int i = 0; i < expected.length; i++) { if (expected[i] != actual[i]) { if (expected[i] == null || !(expected[i].equals(actual[i]))) { differences.add(new ArrayDifference?(expected[i], actual[i])); } } } assertEquals(new ArrayList(), differences); }

}

This builds up a list of differences. The list knows how to present itself as a string (List does that). Each difference presents itself as a string (ArrayDifference? does that). If you need prettier output, create a class that wraps a List of ArrayDifferences? and makes the output prettier.


(VladimirBossicard) Stop! First write tests before your code (XP concept, isn't it?) and come back when you have tested your program... Your class fails to pass the following tests:

(JbRainsberger) I didn't think I was writing production code. Apparently, my mistake.

Object a[] = null; Object b[] = null; assertEqualArrays(a, b) and
Object a[] = { null }; Object b[] = { null }; assertEqualArrays(a, b)

(JbRainsberger) I think I fixed the two bugs above, although, to be fair, I didn't consider either of the above cases useful. The first is pointless to me; the second less so.

and why comparing the differences object with a new ArrayList object? (the ouput is not particulary nice)...

(JbRainsberger) I wanted to know more than whether the differences were empty. If not, I wanted to know what the differences were. The output could use cleaning up, but again, the above was an illustration, not a patch to be submitted to the JavaUnit project.

Incidentally, did you even look at my last sentence above? (If you need prettier output, create a class that wraps a List of ArrayDifferences? and makes the output prettier.) Don't be in such a hurry.

Ok, I spare you some work and propose my solution:

public class AssertArray2 extends TestCase {

public AssertArray2 (String name) { super(name); }

public void assertEqualArrays (Object[] expected, Object[] actual) { assertEqualArrays(null, expected, actual); }

public void assertEqualArrays (Object message, Object[] expected, Oject[] actual) { if (Arrays.equals(expected, actual)) return;

String formatted = ""; if (message != null) formatted = message + " "; assertNotNull(formatted + "expected array: <not null> but was <null>", actual); assertNotNull(formatted + "expected array: <null> but was <not null>", expected);

assertEquals(formatted + "[array length] ", expected.length, actual.length);

StringBuffer differences = new StringBuffer(); for (int i = 0; i < expected.length; i++) { if (expected[i] == null || !(expected[i].equals(actual[i]))) { differences.append("(").append(i).append("): expected <").append(expected[i]).append("> but was <").append(actual[i]).append(">\n"); } } if (differences.length() != 0) fail(differences.toString()); } }


Some JavaUnitBestPractices.


EditText of this page (last edited August 27, 2001) or FindPage with title or text search