使用Mockk进行单元测试,java.lang.ClassCastException:无法将PhoneValidationKt $ isPhoneValid $ 1强制转换为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$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)

以下是我引用的链接,请尝试按以下方式进行这些单元测试

Link 1 Link 2 Link 3 Link 4

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

如果您将此测试用作尝试使用诸如catchLambda()之类的模仿K功能的游乐场,请忽略我的答案,因为我无法为您提供为何引发此异常的具体解释。但是,如果您真的要测试它:测试一个函数时,您不想模拟它的行为,而是想测试它的实际行为。因此,在这种情况下,我将摆脱模拟状态:

@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) }
}
本文链接:https://www.f2er.com/2552241.html

大家都在问