Comparing lists with hamcrest

Georg :

I want to write a unittest that compares two lists. I tried to use hamcrest to get nice readability and error messages, but for some reason this does not compile:

List<GDSRecord> expectedRecords = getReferenceRecords(...);

List<GDSRecord> aktualRecords = gdsNetlist.getRecords();

assertThat(aktualRecords, hasItems(expectedRecords.toArray()));

This on the other hand does compile:

assertThat(asList("a", "b"), hasItems(new String[]{"a"}));

can someone explain the difference here? I am not getting it.., Thanks!

Naman :

Chances are, that

expectedRecords.toArray()

would be converting it to an Object[], you can change it to using List.toArray​(T[] a)

expectedRecords.toArray(new GDSRecord[0])

and it should work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=131178&siteId=1