Kotlin + Power-Assert ︓ ⾔語組み込みならではの Assertion Library 採⽤と運⽤ベストプラクティス @ Kotlin Fest 2025 , 2025-11-01 / opinions are my own 19
AssertK (or AssertJ / usingRecursiveComparison) だと?
Sample Code
(Paste Ready)
// https://pl.kotl.in/GqM4RFeuU
private fun Assert.isEqualToIgnoringTimestamp(expected: R) {
val ignoreFieldName = listOf("createdAt", "updatedAt")
given { actual: T ->
if (actual == null || expected == null) {
return
[email protected](expected)
}
val actualProperties = actual::class.memberProperties.associateBy { it.name }
val expectedProperties = expected::class.memberProperties.associateBy { it.name }
prop("_fields") { actualProperties.keys.filter { !ignoreFieldName.contains(it) } }
.isEqualTo(expectedProperties.keys.filter { !ignoreFieldName.contains(it) })
[email protected] {
for (name in (actualProperties.keys + expectedProperties.keys)) {
if (ignoreFieldName.contains(name)) continue
val actualProperty = actualProperties.getValue(name)
val expectedProperty = expectedProperties.getValue(name)
val expectedValue = expectedProperty.getter.call(expected)
prop(name) { actualProperty.call(it) }
.isEqualTo(expectedValue)
}
}
}
}