JUnit test failing but showing identical Expected and Actual results

21,610

Solution 1

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

Solution 2

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!

Solution 3

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.

Share:
21,610

Related videos on Youtube

nihilon
Author by

nihilon

Updated on July 09, 2022

Comments

  • nihilon
    nihilon almost 2 years

    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;
    }
    
  • nihilon
    nihilon over 10 years
    Accidently hit enter before I finished typing: Sometimes, I get really irritated with being new at this stuff. Thanks ZouZou! ********* The reason it doesn't return a List<Double> is that the Double[] sent into the method is half the result of separating numbers and words from a List<Object>. So, technically, this method does two things. It is converting Double[] back into a List<Object> and trimming the null values from the array as it populates the list. I know it really shouldn't be doing two things at once. Stop judging me. :D