使用 Mockk 进行单元测试,java.lang.ClassCastException:PhoneValidationKt$isPhoneValid$1 无法转换为 kotlin.jvm.functions.Function1

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

在对我的 Kotlin 函数进行单元测试时需要一些帮助,因为我是单元测试的新手,我尝试过但失败了,

我的Kotlin顶层函数如下,

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()
}

我对上述方法的单元测试代码如下,

@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) }
}

下面是我在单元测试代码时得到的错误,如下,

java.lang.ClassCastException: com.reprator.phone.PhoneValidationKt$isPhoneValid cannot be cast to kotlin.jvm.functions.Function1

at com.reprator.phone.PhoneValidationKtTest$Invalid Phone Number.invoke(PhoneValidationKtTest.kt:31)
at com.reprator.phone.PhoneValidationKtTest$Invalid Phone Number.invoke(PhoneValidationKtTest.kt:11)
at io.mockk.MockKStubScope$answers.invoke(API.kt:2149)
at io.mockk.MockKStubScope$answers.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.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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)

以下是链接,我指的是按如下方式尝试那些单元测试,

Link 2

如果您将此测试用作尝试 mockK 功能(如 captureLambda())的游乐场,请忽略我的回答,因为我无法向您提供有关引发此异常的原因的具体解释。然而,如果你真的想测试它:当你测试一个函数时,你不想模拟它的行为,你想测试它的实际行为。所以,在这种情况下,我会摆脱 mockStatic:

@Test
fun `Invalid Phone Number`() {
    val fn: (Int?) -> Unit = mockk(relaxed = true)

    val result = isPhoneValid("904186605", fn)

    verify { fn.invoke(R.string.phone_validation_mobile) }
}