How to test .equals () method with JUnit and Hamcrest

I have a List class (EDIT: I wrote it myself), with the List.equals method, so I want to run something like

List list1 = new List(); List list2 = new List(); assertTrue(list1.equals(list2)); 

So, using matches and assertThat, I thought maybe

 assertThat(list1.equals(list2), is(true)); 

But it gets pretty dumb ... EDIT: maybe I can write my own matches

Is there a better way to check if my equals method works correctly?

This is with JUnit4.5

+4
source share
2 answers
 import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; ... assertThat(list1, equalTo(list2)); 
+9
source

assertEquals(list1, list2) is the easiest way.

+1
source

Source: https://habr.com/ru/post/1491024/


All Articles