Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit test failing but showing identical Expected and Actual results

So, I don't even know where to start with this one. This whole fiasco began over eight hours ago, jerry rigging my way out of one issue after another. Now, this: Junit test failing, with passing results And it gets even better, after clicking "Click to see difference": enter image description here

I have:
- Invalidated caches and restarted IntelliJ
- Attempted running where Double[] numSet = {23.0}; and then setting the expected result to numSet rather than "[23.0]". No dice.
- The answer regarding adding a delta didn't work: JUnit test fails even if the expected result is correct

I feel it is also worth noting that the error supposedly occuring at line 48 does not show up when calling the method from the main class, so I have no clue as to what that might be about.

Any help will be appreciated.

Here is the code for the method being tested. Almost forgot it:

public List<Object> trimNumberSet(Double[] numSet) {
    List<Object> trimmedNumberSet = new ArrayList<>();

    for (int i = 0; i < numSet.length; i++) {
        if (numSet[i] != null) {
            trimmedNumberSet.add(numSet[i]);
        }
    }
    return trimmedNumberSet;
}
like image 700
nihilon Avatar asked Feb 11 '14 11:02

nihilon


3 Answers

You are comparing a String and a List<Object>.

So you're using the method assertEquals(Object expected, Object actual);

Obviously a String is not equals to a List<Object> and hence the fail.

But since the String representation of the list is the same as you wrote, it hides you the result in the console because it uses this representation to show you the result of this test.

So you should compare the String representation of boths :

assertEquals("[23.0]", setSorteer.trimNumberSet(numSet).toString());

A better idea would be to compare two Lists directly, and not base your test on the String representations because they can change in the future and your test will fail :

assertEquals(Arrays.asList(23.0), setSorteer.trimNumberSet(numSet));


Also any reasons that you don't return a List<Double> in your method?
public List<Double> trimNumberSet(Double[] numSet) {
    List<Double> trimmedNumberSet = new ArrayList<>();
    ...
    return trimmedNumberSet;
}
like image 71
Alexis C. Avatar answered Nov 07 '22 00:11

Alexis C.


I think your problem came across the data types. Even when the data are the 'same', it is possible that the test is comparing String vs Integer (or whatever the number implementation being used is). This error would occur in other circumstances...

Try to convert the left side to the same data type that setSorter.trimNumberSet retrieve or convert setSorter.trimNumberSet's return object to a String in assertEquals.

Hope that helps!

like image 44
Francisco Hernandez Avatar answered Nov 07 '22 00:11

Francisco Hernandez


As ZouZou said, you are comparing different data types. numSet is an array of Doubles, whereas the result of trimNumberSet is a List of Doubles.

In addition, note: When comparing the Double value in your List to a String, I highly recommend that you also use a number formatter to ensure that the scale is as you expect (i.e. "23.0" instead of, for example, "23.000001"). The internal representation of Double values will often cause the effect that what you get out is not precisely what you have put in before.

like image 41
spike Avatar answered Nov 07 '22 00:11

spike