0

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)

}
Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28

1 Answers1

1

any() would work to mock lamba

    val jdbcMock = mockk<JdbcTemplate>()

    every { jdbcMock.update(any<PreparedStatementCreator>(),any<KeyHolder>()) } returns  10

    val repo = Repository(jdbcMock)

    assertEquals(repo.createRule() == 10)
iamanbansal
  • 2,412
  • 2
  • 12
  • 20
  • Thanks but every { jdbcMock.update(any(),any()) } solution does not work due to "Overload resolution ambiguity". It matches all the below overloaded methods of jdbctemplate. – Shruthi Ranganatha Sep 27 '20 at 02:31
  • `public final fun update(psc: ((con: Connection!) → PreparedStatement!)!, generatedKeyHolder: KeyHolder!): Int defined in org.springframework.jdbc.core.JdbcTemplate public final fun update(sql: String!, pss: ((ps: PreparedStatement!) → Unit)!): Int defined in org.springframework.jdbc.core.JdbcTemplate` – Shruthi Ranganatha Sep 27 '20 at 02:34
  • `public open fun update(sql: String!, vararg args: Any!): Int defined in org.springframework.jdbc.core.JdbcTemplate public open fun update(sql: String!, pss: PreparedStatementSetter!): Int defined in org.springframework.jdbc.core.JdbcTemplate public open fun update(psc: PreparedStatementCreator!, generatedKeyHolder: KeyHolder!): Int defined in org.springframework.jdbc.core.JdbcTemplate` – Shruthi Ranganatha Sep 27 '20 at 02:34
  • And I am looking for mocking the last method having PreparedStatementCreator and KeyHolder arguments. – Shruthi Ranganatha Sep 27 '20 at 02:37
  • @ShruthiRanganatha I have updated my answer. You can mention the class name with any() for overloaded functions to test. – iamanbansal Oct 02 '20 at 15:08
  • I got the following error. Failed matching mocking signature for left matchers: [any(), any()] io.mockk.MockKException: Failed matching mocking signature for left matchers: [any(), any()] – Shruthi Ranganatha Oct 06 '20 at 03:57