-3

I have the following sentence:

assertArrayEquals(Arrays.asList(df.sort("State", new SortDescending())).toArray(),Arrays.asList("[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]").toArray());

The return of the function is a List< Object > and it contains:

[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]

The error is:

Expected :java.util.LinkedList<[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]>
Actual   :java.lang.String<[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]>

Why i don't pass the test when I compare these two lists?

  • 2
    I didn't double check, but `is` might imply identity, i.e., they're the same object, which is obviously not true. Try an `assertEquals` comparison. – markspace Jan 06 '22 at 03:41
  • 1
    Does this answer your question? [Comparing arrays in JUnit assertions, concise built-in way?](https://stackoverflow.com/questions/4228161/comparing-arrays-in-junit-assertions-concise-built-in-way) – Hanchen Jiang Jan 06 '22 at 03:41
  • 1
    In Java, you CANNOT compare two different arrays with "==". Similarly, in JUnit you cannot compare two arrays with "is()". You must use [assertArrayEquals](http://junit.sourceforge.net/javadoc/org/junit/Assert.html). – paulsm4 Jan 06 '22 at 03:45
  • 4
    `Arrays.asList("[WV, WV, …, DE]")` is a List containing exactly one element. You probably meant to write `Arrays.asList("WV", "WV", "WI", "WA", /*etc.*/, "DE")`. There should be no `[` or `]` in the code. – VGR Jan 06 '22 at 03:46
  • I edited the question... I tried to do it with assertArrayEquals but it continues wrong... – Newbie Programmer Jan 06 '22 at 04:09
  • @DawoodibnKareem Since there were a few issues going on, I wasn’t sure if it was the answer, but now I’m convinced it is. – VGR Jan 06 '22 at 05:23
  • @VGR Upvoted as promised – Dawood ibn Kareem Jan 06 '22 at 22:12

1 Answers1

1

In Java, " characters specify a String value. Not a List. Just a single String value.

This:

"[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]"

is not a List. It’s a single String object, whose value may happen to look like a List, but it’s not a List. It is in double-quotes ("), therefore it is a single String object.

This:

Arrays.asList("[WV, WV, WI, WA, VA, TX, SD, PA, OH, ND, NC, NC, MB, MA, KS, FL, DE]")

create a List which contains one element. It is a List<String> with a size of one.

Arrays.asList does not attempt to parse Strings in any way. You cannot specify a List of items inside a single String value.

What you probably meant to write was this:

Arrays.asList("WV", "WV", "WI", "WA", "VA", "TX", "SD", "PA", "OH", "ND", "NC", "NC", "MB", "MA", "KS", "FL", "DE")

Notice that are no [ or ] characters. The List is comprised of individual String values, each specified as a separate argument to Arrays.asList.

You might find it easier to just compare the Lists directly:

Assert.assertEquals("Checking for correctly sorted list.",
    Arrays.asList("WV", "WV", "WI", "WA", "VA", "TX", "SD", "PA", "OH", "ND", "NC", "NC", "MB", "MA", "KS", "FL", "DE"),
    Arrays.asList(df.sort("State", new SortDescending())));
VGR
  • 40,506
  • 4
  • 48
  • 63