data class DataSample(val name: String, val exec: () -> Unit = {})
I am trying to understand why the following test would fail:
@Test
fun `testing data fails`() {
val dataOne = DataSample("the_same") {
}
val dataTwo = DataSample("the_same") {
}
assertEquals(dataOne, dataTwo)
}
and the following would pass:
@Test
fun `testing data pass`() {
val dataOne = DataSample("the_same")
val dataTwo = DataSample("the_same")
assertEquals(dataOne, dataTwo)
}
The test will pass when the lambda is omitted and it fails when one is provided.
Since instances of data classes are compared member by member the first test succeeds because “the same“ is equal to “the same“ and the default value for exec is {}
which is equal to itself. In the second test you give each instance its own lambda which are not equal to each other even though they behave equally. Thus, comparing dataOne
and dataTwo
fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With