'Can't call verify an spy instance of Ktorm's EntitySequence using MockK's verify

When trying to call verify on a spy instance of EntitySequence I'm hit with an UnsupportedOperationException.

Here is the snippet I'm trying to execute:

interface TestEntity: Entity<TestEntity> {
    companion object: Entity.Factory<TestEntity>()
    var id: String
    var name: String
}

object TestTable: Table<TestEntity>(tableName = "test_table") {
    val id = varchar("id").primaryKey().bindTo { it.id }
    val name = varchar("name").bindTo { it.name }
}

val database = Database.connect("jdbc:h2:mem:ktorm;DB_CLOSE_DELAY=-1")
val repository = spyk(database.sequenceOf(TestTable))

fun <E: Entity<E>, T: Table<E>> EntitySequence<E, T>.upsert(entity: E) {
    if (this.none { it["id"] as Column<String> eq entity["id"] as String }) {
        this.add(entity)
    } else {
        this.update(entity)
    }
}

@Test
fun `Test upsert`() {
    listOf(
        TestEntity { id = "01" },
        TestEntity { id = "02" },
        TestEntity { id = "03" },
    ).forEach { repository.upsert(it) }

    verify(exactly = 3) { repository.add(any()) }
    verify(exactly = 1) { repository.update(any()) }
}

The goal here is to check if the upsert extension function triggers add or update. If I execute the test without the verify portion it goes fine and I can check the results on H2 directly. But if I try to execute the verify portion it throws the following error:

Entity manipulation functions are not supported by this sequence object. Please call on the origin sequence returned from database.sequenceOf(table)
java.lang.UnsupportedOperationException: Entity manipulation functions are not supported by this sequence object. Please call on the origin sequence returned from database.sequenceOf(table)
    at org.ktorm.entity.EntityDmlKt.checkIfSequenceModified(EntityDml.kt:202)
    at org.ktorm.entity.EntityDmlKt.add(EntityDml.kt:41)
    at example.redshift.RedshiftConnectorTest$Test upsert$5.invoke(RedshiftConnectorTest.kt:53)
    at example.redshift.RedshiftConnectorTest$Test upsert$5.invoke(RedshiftConnectorTest.kt:16)
    at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:25)
    at io.mockk.impl.eval.RecordedBlockEvaluator$enhanceWithRethrow$1.invoke(RecordedBlockEvaluator.kt:78)
    at io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:23)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:40)
    at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
    at io.mockk.MockKDsl.internalVerify(API.kt:119)
    at io.mockk.MockKKt.verify(MockK.kt:149)
    at io.mockk.MockKKt.verify$default(MockK.kt:146)
    at example.redshift.RedshiftConnectorTest.Test upsert(RedshiftConnectorTest.kt:53)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source