I am trying to implement the same given below using MockK instead of Mockito. I am not able to mock the lambda function inside the JDBC template update() method.
class Repository(private val jdbcTemplate: JdbcTemplate) {
createRule(emp: Employee): Int? {
val insertRuleQuery: String = getAddEmployeeSqlString()
val holder: KeyHolder = GeneratedKeyHolder()
val row: Int = jdbcTemplate.update({ connection: Connection ->
val ps = connection.prepareStatement(insertRuleQuery, arrayOf("EMP_ID"))
ps.setLong(1, emp.batchId)
ps.setString(2, emp.permanent)
ps.setString(3, emp.groupId)
ps
}, holder)
return row
}
class RepositoryTest {
private val mockJT = Mockito.mock(JdbcTemplate::class.java)
fun `jdbc test emp insert`() {
Mockito.`when`(
mockJT.update(
Mockito.any(PreparedStatementCreator::class.java), Mockito.any(
KeyHolder::class.java
)
)
).thenReturn(1)
assertEquals(count, 1)
}