How to aggregate differences instead of asserting just the first difference ?

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);
}

}
 
J

Joan

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.

Are you certain that this is how assert is supposed to be used?
If a customer sees an assert, they usually get quite upset, don't
they?
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);
}

}
 
T

tomjbr.16324861

Are you certain that this is how assert is supposed to be used?
If a customer sees an assert, they usually get quite upset, don't
they?


I do not know what you are supposed to do according to whom, but I just
know that I am interested in getting relevant information about all
problems.

And I do also not think that the purpose of unit tests is to hide the
actual problems from the customer (if that would be the purpose you may
also choose to write zero or at least fewer unit tests, or even write a
lot of simple/faked tests if you want to show an customer a lot of
green successed tests).

Anyway, if there is a lot of failures in the situation I described many
of them likely have similar cause, so the amount of output would not be
proportinoal to the time of fixing it.

Lastly, in my case, the bugs does not come from code i have written,
but I am a new employee, who want to get information about the cause
for previous developers failed unit tests.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top