T
tomjbr.16324861
For example, if I will parse two files and compare row by row, I
would like to see all the differences and not just the first
difference.
Another situation might be when you want to compare all elements in
some collection, and not just want to see the values of the first
failures.
If there is no good solution within JUnit itself, then maybe there is
some extension ?
I started writing some code below, just to illustrate the idea.
But instead of trying to improve this code of mine, which probably
would be to reinvent the wheel, I would prefer to reuse some good
existing code.
For example, when I iterate these Strings and compare each pair of
items normally with JUnit:
new String[][] {
new String[]{"abc", "abcd"},
new String[]{"def", "def"},
new String[]{"ghi", "gghi"},
new String[]{"jkl", "jkl"}
};
then the result will be like this:
junit.framework.ComparisonFailure: my message expected:<...> but
was:<...d>
at junit.framework.Assert.assertEquals(Assert.java:81)
However, I would instead like this kind of output message:
junit.framework.AssertionFailedError:
-----------------------
my title
ItemNumber 1
Expected: abc
Actual: abcd
ItemNumber 3
Expected: ghi
Actual: gghi
The number of items with failures was: 2
-----------------------
expected:<0> but was:<2>
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:282)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:201)
Here is the code that produced this output:
(but I would like to find a better existing framework for this kind
of comparisons)
public class AggregatingFailuresTest extends TestCase {
private String[][] getStringArrayOfArrays()
{
// Of course this method usually is in the class
// under test but for simplicity I just put it here
return new String[][] {
new String[]{"abc", "abcd"},
new String[]{"def", "def"},
new String[]{"ghi", "gghi"},
new String[]{"jkl", "jkl"}
};
}
public void testGetStringArrayOfArrays() {
AssertionsAggregator oAssertionsAggregator = new
AssertionsAggregator("my title");
String[][] arrayOfArrays = getStringArrayOfArrays();
String[] a;
for (int i = 0; i < arrayOfArrays.length; i++) {
a = arrayOfArrays;
oAssertionsAggregator.assertEquals("my
message", a[0], a[1]);
//assertEquals("my message", a[0], a[1]);
}
oAssertionsAggregator.doAssertion();
}
}
public class AssertionsAggregator {
private int itemNumber = 0;
private int numberOfItemsWhichAreNotEqual = 0;
private static final String LINE_WITH_HYPHENS = "-----
------------------";
private static final String LINEBREAK = "\n";
private StringBuffer messageBuffer = new StringBuffer
();
public AssertionsAggregator(String title)
{
messageBuffer.append(LINEBREAK);
messageBuffer.append(LINE_WITH_HYPHENS);
messageBuffer.append(LINEBREAK);
messageBuffer.append(title);
messageBuffer.append(LINEBREAK);
}
public void assertEquals(String message, String
expected, String actual)
{
itemNumber++;
if(!expected.equals(actual))
{
numberOfItemsWhichAreNotEqual++;
messageBuffer.append(LINEBREAK);
messageBuffer.append("ItemNumber " +
itemNumber);
messageBuffer.append(LINEBREAK);
messageBuffer.append("Expected: " +
expected);
messageBuffer.append(LINEBREAK);
messageBuffer.append("Actual: " +
actual);
messageBuffer.append(LINEBREAK);
}
}
public void doAssertion() {
messageBuffer.append(LINEBREAK);
messageBuffer.append("The number of items
with failures was: " + numberOfItemsWhichAreNotEqual);
messageBuffer.append(LINEBREAK);
messageBuffer.append(LINE_WITH_HYPHENS);
messageBuffer.append(LINEBREAK);
Assert.assertEquals(messageBuffer.toString(),
0, numberOfItemsWhichAreNotEqual);
}
}
would like to see all the differences and not just the first
difference.
Another situation might be when you want to compare all elements in
some collection, and not just want to see the values of the first
failures.
If there is no good solution within JUnit itself, then maybe there is
some extension ?
I started writing some code below, just to illustrate the idea.
But instead of trying to improve this code of mine, which probably
would be to reinvent the wheel, I would prefer to reuse some good
existing code.
For example, when I iterate these Strings and compare each pair of
items normally with JUnit:
new String[][] {
new String[]{"abc", "abcd"},
new String[]{"def", "def"},
new String[]{"ghi", "gghi"},
new String[]{"jkl", "jkl"}
};
then the result will be like this:
junit.framework.ComparisonFailure: my message expected:<...> but
was:<...d>
at junit.framework.Assert.assertEquals(Assert.java:81)
However, I would instead like this kind of output message:
junit.framework.AssertionFailedError:
-----------------------
my title
ItemNumber 1
Expected: abc
Actual: abcd
ItemNumber 3
Expected: ghi
Actual: gghi
The number of items with failures was: 2
-----------------------
expected:<0> but was:<2>
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:282)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:201)
Here is the code that produced this output:
(but I would like to find a better existing framework for this kind
of comparisons)
public class AggregatingFailuresTest extends TestCase {
private String[][] getStringArrayOfArrays()
{
// Of course this method usually is in the class
// under test but for simplicity I just put it here
return new String[][] {
new String[]{"abc", "abcd"},
new String[]{"def", "def"},
new String[]{"ghi", "gghi"},
new String[]{"jkl", "jkl"}
};
}
public void testGetStringArrayOfArrays() {
AssertionsAggregator oAssertionsAggregator = new
AssertionsAggregator("my title");
String[][] arrayOfArrays = getStringArrayOfArrays();
String[] a;
for (int i = 0; i < arrayOfArrays.length; i++) {
a = arrayOfArrays;
oAssertionsAggregator.assertEquals("my
message", a[0], a[1]);
//assertEquals("my message", a[0], a[1]);
}
oAssertionsAggregator.doAssertion();
}
}
public class AssertionsAggregator {
private int itemNumber = 0;
private int numberOfItemsWhichAreNotEqual = 0;
private static final String LINE_WITH_HYPHENS = "-----
------------------";
private static final String LINEBREAK = "\n";
private StringBuffer messageBuffer = new StringBuffer
();
public AssertionsAggregator(String title)
{
messageBuffer.append(LINEBREAK);
messageBuffer.append(LINE_WITH_HYPHENS);
messageBuffer.append(LINEBREAK);
messageBuffer.append(title);
messageBuffer.append(LINEBREAK);
}
public void assertEquals(String message, String
expected, String actual)
{
itemNumber++;
if(!expected.equals(actual))
{
numberOfItemsWhichAreNotEqual++;
messageBuffer.append(LINEBREAK);
messageBuffer.append("ItemNumber " +
itemNumber);
messageBuffer.append(LINEBREAK);
messageBuffer.append("Expected: " +
expected);
messageBuffer.append(LINEBREAK);
messageBuffer.append("Actual: " +
actual);
messageBuffer.append(LINEBREAK);
}
}
public void doAssertion() {
messageBuffer.append(LINEBREAK);
messageBuffer.append("The number of items
with failures was: " + numberOfItemsWhichAreNotEqual);
messageBuffer.append(LINEBREAK);
messageBuffer.append(LINE_WITH_HYPHENS);
messageBuffer.append(LINEBREAK);
Assert.assertEquals(messageBuffer.toString(),
0, numberOfItemsWhichAreNotEqual);
}
}