title: Unit Testing with Mockk, java.lang.ClassCastException: PhoneValidationKt$isPhoneValid$1 cannot be cast to kotlin.jvm.functions.Function1
body: <p>Need some help in Unit testing my Kotlin funtion as follows, as i am new to unit testing, i had tried but failed,</p>
  
  <p>My Kotlin top level function as follows,</p>
  
  <pre><code>package com.reprator.phone
  //PhoneValidation.kt
  const val PHONE_LENGTH = 10
  
  fun isPhoneValid(
  phoneNumber: String, successBlock: (() -&gt; Unit) = {},
  failBlock: (Int?.() -&gt; Unit) = {}
  ) =
  when {
      phoneNumber.isEmpty() -&gt;
          failBlock(R.string.phone_validation_mobile_empty)
      phoneNumber.length &lt; PHONE_LENGTH -&gt;
          failBlock(R.string.phone_validation_mobile)
      else -&gt; successBlock.invoke()
  }
  </code></pre>
  
  <p>My Unit test code for the above method is as follows,</p>
  
  <pre><code>@Test
  fun `Invalid Phone Number`() {
      mockkStatic("com.reprator.phone.PhoneValidationKt")
  
      val fn: (Int?) -&gt; Unit = mockk(relaxed = true)
  
      val result = R.string.phone_validation_mobile
      every {
          isPhoneValid("904186605", failBlock = captureLambda())
      } answers {
          secondArg&lt;(Int?) -&gt; Unit&gt;()(result)
      }
  
      isPhoneValid("904186605")
  
      verify { fn.invoke(result) }
  }
  </code></pre>
  
  <p>The following is the error, i get while unit testing the code, as follows,</p>
  
  <pre><code>java.lang.ClassCastException: com.reprator.phone.PhoneValidationKt$isPhoneValid$1 cannot be cast to kotlin.jvm.functions.Function1
  
  at com.reprator.phone.PhoneValidationKtTest$Invalid Phone Number$2.invoke(PhoneValidationKtTest.kt:31)
  at com.reprator.phone.PhoneValidationKtTest$Invalid Phone Number$2.invoke(PhoneValidationKtTest.kt:11)
  at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2149)
  at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2126)
  at io.mockk.FunctionAnswer.answer(Answers.kt:19)
  at io.mockk.impl.stub.AnswerAnsweringOpportunity.answer(AnswerAnsweringOpportunity.kt:13)
  at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:54)
  at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
  at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
  at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
  at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
  at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
  at com.reprator.phone.PhoneValidationKt.isPhoneValid(PhoneValidation.kt:15)
  at com.reprator.phone.PhoneValidationKt.isPhoneValid$default(PhoneValidation.kt:7)
  at com.reprator.phone.PhoneValidationKtTest.Invalid Phone Number(PhoneValidationKtTest.kt:26)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
  at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
  at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
  </code></pre>
  
  <p>The following are the links, that i referred to try those unit testing as follows,</p>
  
  <p><a href="https://stackoverflow.com/questions/53808565/cast-exception-when-mocking-kotlin-lambda-callbacks-in-mockk?rq=1">Link 1</a>
  <a href="https://stackoverflow.com/questions/53671695/how-to-mock-and-verify-lambda-expression-in-kotlin">Link 2</a>
  <a href="https://stackoverflow.com/questions/53911556/mockk-verify-lambda-argument">Link 3</a>
  <a href="https://stackoverflow.com/questions/53673292/how-to-call-a-lambda-callback-with-mockk">Link 4</a></p>