TestCase: improve error messages for comparisons
authorNiki Roo <niki@nikiroo.be>
Sun, 25 Mar 2018 09:40:12 +0000 (11:40 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 25 Mar 2018 09:40:12 +0000 (11:40 +0200)
src/be/nikiroo/utils/test/TestCase.java

index 5cff3630a725aa1e5fb67301ff1518875589a96c..4e7d3800c5a588bf6a1b06f341bfc5d3c2c8c2ac 100644 (file)
@@ -44,7 +44,6 @@ abstract public class TestCase {
         * @throws Exception
         *             in case of error
         */
-       @SuppressWarnings("unused")
        public void setUp() throws Exception {
        }
 
@@ -54,7 +53,6 @@ abstract public class TestCase {
         * @throws Exception
         *             in case of error
         */
-       @SuppressWarnings("unused")
        public void tearDown() throws Exception {
        }
 
@@ -259,14 +257,37 @@ abstract public class TestCase {
         */
        public void assertEquals(List<?> expected, List<?> actual)
                        throws AssertException {
+               assertEquals("Assertion failed", expected, actual);
+       }
+
+       /**
+        * Check that 2 {@link List}s are equals.
+        * 
+        * @param errorMessage
+        *            the error message to display if they differ
+        * @param expected
+        *            the expected value
+        * @param actual
+        *            the actual value
+        * @param errorMessage
+        *            the error message to display if they differ
+        * 
+        * @throws AssertException
+        *             in case they differ
+        */
+       public void assertEquals(String errorMessage, List<?> expected,
+                       List<?> actual) throws AssertException {
 
-               assertEquals("The 2 lists don't contain the same number of items",
-                               expected.size(), actual.size());
+               if (expected.size() != actual.size()) {
+                       assertEquals(errorMessage + ": not same number of items",
+                                       list(expected), list(actual));
+               }
 
                int size = expected.size();
                for (int i = 0; i < size; i++) {
-                       assertEquals("Line " + i + " (0-based) is not correct",
-                                       expected.get(i), actual.get(i));
+                       assertEquals(errorMessage + ": item " + i
+                                       + " (0-based) is not correct", expected.get(i),
+                                       actual.get(i));
                }
        }
 
@@ -314,4 +335,25 @@ abstract public class TestCase {
                                + "Expected value: [%s]%n" //
                                + "Actual value: [%s]", expected, actual);
        }
+
+       private static String list(List<?> items) {
+               StringBuilder builder = new StringBuilder();
+               for (Object item : items) {
+                       if (builder.length() == 0) {
+                               builder.append(items.size() + " item(s): ");
+                       } else {
+                               builder.append(", ");
+                       }
+
+                       builder.append("" + item);
+
+                       if (builder.length() > 60) {
+                               builder.setLength(57);
+                               builder.append("...");
+                               break;
+                       }
+               }
+
+               return builder.toString();
+       }
 }