I would like to compare two collections using AssertJ with strict type checking. However, I only care about the strict types of collection elements not the collection itself.
For instace in the case below I would like assertion
assertThat(listOfA).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(arrayListOfA) to be true while now it's false because listOfA is of type SingletonList and arrayListOfA is of type ArrayList
@Test
fun `test`() {
val listOfA = listOf(TestClassA("Name"))
val arrayListOfA = arrayListOf(TestClassA("Name"))
val arrayListOfB = arrayListOf(TestClassB("Name"))
assertThat(listOfA).usingRecursiveComparison().isEqualTo(arrayListOfA)
assertThat(listOfA).usingRecursiveComparison().isEqualTo(arrayListOfB)
assertThat(listOfA).usingRecursiveComparison().withStrictTypeChecking().isEqualTo(arrayListOfA)
assertThat(listOfA).usingRecursiveComparison().withStrictTypeChecking().isNotEqualTo(arrayListOfB)
}
class TestClassA(val name: String)
class TestClassB(val name: String)
Is there any way to achieve this?
Your target could be achieved with standard list assertions and a
RecursiveComparator:Not as fluent as one would expect but it's the best option I see at the moment.