Код: Выделить всё
@Test
fun saveAndLoadTest() {
val id = UUID.randomUuid().toString()
val expected = Foo(id)
dao.save(expected)
val actual = dao.load(id)
assertThat(actual).isEqualTo(expected)
}
Код: Выделить всё
@Table("Foo")
data class Foo (
@PrimaryKey
val id: String,
// the value of this field will differ before and after the object is written to the database
@Column(spannerCommitTimestamp = true)
val createdTimestamp: Timestamp = Timestamp.now()
)
Код: Выделить всё
fun ObjectAssert.isEqualIgnoringTimestamps(other: T): ObjectAssert {
this.usingRecursiveComparison()
.ignoringFieldsMatchingRegexes("createdTimestamp.*")
.isEqualTo(other)
return this
}
Код: Выделить всё
@Test
fun saveAndLoadTest() {
val id = UUID.randomUuid().toString()
val expected = Foo(id)
dao.save(expected)
val actual = dao.load(id)
// at this point, we're basically only checking that the `id` fields are the same
assertThat(actual).isEqualIgnoringTimestamps(expected)
}
Код: Выделить всё
@Table("Foo")
data class Foo (
@PrimaryKey
val id: String,
// the "recursive" comparison rules won't be applied to this child object
val child: Foo,
@Column(spannerCommitTimestamp = true)
val createdTimestamp: Timestamp = Timestamp.now()
)
< /code>
Теперь модульный тест не выполняется с сообщением об ошибке, которое выглядит следующим образом: < /p>
Expecting:
to be equal to:
when recursively comparing field by field, but found the following difference:
field/property 'child' differ:
- actual value : Foo(id=cs6ne8m8tilcbhxq3s4ejxn5n, child=null, createdTimestamp=2021-06-18T20:08:37.352000000Z)
- expected value : Foo(id=cs6ne8m8tilcbhxq3s4ejxn5n, child=null, createdTimestamp=2021-06-18T20:08:37.142614000Z)
The recursive comparison was performed with this configuration:
- the fields matching the following regexes were ignored in the comparison: createdTimestamp.*
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
Код: Выделить всё
fun ObjectAssert.isEqualIgnoringTimestamps(other: T): ObjectAssert {
this.usingRecursiveComparison()
.ignoringFieldsOfTypes(Timestamp::class.java)
.isEqualTo(other)
return this
}
< /code>
В любом случае, поле CreatedTimeStamp < /code> игнорируется в объекте, но не игнорируется в объекте Has-A. Объекты (я бы подумал, что это так, потому что это должно быть [b] рекурсивным [/b], но я думаю, что я был неправ)? Я думаю, я мог бы переопределить методы .equals (...)
Подробнее здесь: https://stackoverflow.com/questions/680 ... ation-isnt