yeezusplusplus
yeezusplusplus

Reputation: 33

JUnit test of a method using ArrayList gets a String error

This is the error trace I get:

java.lang.AssertionError: expected: java.lang.String<[{Song: title=Always artist=Blink 182}]> but was: java.util.ArrayList<[{Song: title=Always artist=Blink 182}]>

when I try and run this piece of code in the test case

assertEquals("[{Song: title=Always artist=Blink 182}]", p.getSongList());

p.getSongList is a method that returns and ArrayList<Song>. I've only added the one song so far but I am confused about how to change the expected output to java.util.ArrayList instead of java.lang.String. Any help would be greatly appreciated.

Upvotes: 1

Views: 1289

Answers (1)

Mureinik
Mureinik

Reputation: 312219

You are comparing an ArrayList instance with the string representation of the desired output (i.e., the String you'd get if you run toString() on the output). Instead, you should just construct the actual output you're expecting.

Your question doesn't supply the code for your Song class, so I'm guessing it's just a POJO:

Song expectedSong = new Song();
expectedSong.setTitle("Always");
expectedSong.setArtist("Blink 182");
ArrayList<Song> expectedList = new ArrayList<>(1);
expectedList.add(song);
assertEquals(expectedList, p.getSongList());

Upvotes: 4

Related Questions