Skip to content
Advertisement

How do I compare Integer and Long types with Junit?

How do I compare Integer and Long types with Junit ?

The result of assertThat(poiList.get(0).get("id_pk")).isEqualTo(member.getId_pk()); is:

org.opentest4j.AssertionFailedError:
expected: 1L
but was : 1
Expected :1L
Actual :1

The two types are:

log.info("getClass1: {}", poiList.get(0).get("id_pk").getClass());
         log.info("getClass2: {}", member.getId_pk().getClass());
2021-09-30 15:29:08.904 INFO 19504 --- [ main] c.p.i.domain.item.ItemServiceTest : getClass1: class java.lang.Integer
2021-09-30 15:29:08.904 INFO 19504 --- [ main] c.p.i.domain.item.ItemServiceTest : getClass2: class java.lang.Long

How can I compare 1 and 1L to be equal?

best regards

Advertisement

Answer

You can convert Integer to Long via java.lang.Integer#longValue:

assertThat(poiList.get(0).get("id_pk").longValue()).isEqualTo(member.getId_pk());

BUT beware of Null pointers, if poiList.get(0).get("id_pk") is null then a Null Pointer Exception will be thrown!

6 People found this is helpful
Advertisement