lundi 30 mars 2020

Unit Testing with Mockk, java.lang.ClassCastException: PhoneValidationKt$isPhoneValid$1 cannot be cast to kotlin.jvm.functions.Function1

Need some help in Unit testing my Kotlin funtion as follows, as i am new to unit testing, i had tried but failed,

My Kotlin top level function as follows,

package com.reprator.phone
//PhoneValidation.kt
const val PHONE_LENGTH = 10

fun isPhoneValid(
phoneNumber: String, successBlock: (() -> Unit) = {},
failBlock: (Int?.() -> Unit) = {}
) =
when {
    phoneNumber.isEmpty() ->
        failBlock(R.string.phone_validation_mobile_empty)
    phoneNumber.length < PHONE_LENGTH ->
        failBlock(R.string.phone_validation_mobile)
    else -> successBlock.invoke()
}

My Unit test code for the above method is as follows,

@Test
fun `Invalid Phone Number`() {
    mockkStatic("com.reprator.phone.PhoneValidationKt")

    val fn: (Int?) -> Unit = mockk(relaxed = true)

    val result = R.string.phone_validation_mobile
    every {
        isPhoneValid("904186605", failBlock = captureLambda())
    } answers {
        secondArg<(Int?) -> Unit>()(result)
    }

    isPhoneValid("904186605")

    verify { fn.invoke(result) }
}

The following is the error, i get while unit testing the code, as follows,

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)

The following are the links, that i referred to try those unit testing as follows,

Link 1 Link 2 Link 3 Link 4

Aucun commentaire:

Enregistrer un commentaire