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

shouldNotBeEqualToUsingFields

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

Asserts that this is NOT equal to other using specific fields

Verifies that this instance is not 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 shouldBeEqualToUsingFields

Example:

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

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

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

Note:

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

See Also

beEqualToUsingFields

shouldNotBeEqualToIgnoringFields