kotest-assertions-core / io.kotest.matchers.equality / shouldBeEqualToUsingFields

shouldBeEqualToUsingFields

fun <T : Any> T.shouldBeEqualToUsingFields(other: T, vararg properties: KProperty<*>): Unit

Asserts that this is equal to other using specific fields

Verifies that this instance is equal to other using only some specific fields. This is useful for matching on objects that contain unknown values, such as a database Entity that contains an ID (you don't know this ID, and it doesn't matter for you, for example)

Opposite of shouldNotBeEqualToUsingFields

Example:

data class Foo(val id: Int, val description: String)

val firstFoo = Foo(1, "Bar!")
val secondFoo = Foo(2, "Bar!")

firstFoo.shouldBeEqualUsingFields(secondFoo, Foo::description) // Assertion passes

firstFoo shouldBe secondFoo // Assertion fails, `equals` is false!

Note:

  1. Throws IllegalArgumentException in case properties parameter is not provided.
  2. Throws IllegalArgumentException if properties contains any non public property