You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please answer the following questions for yourself before submitting an issue.
I am running the latest version (1.12.4)
I checked the documentation and found no answer
I checked to make sure that this issue has not already been filed
Expected Behavior
args[0] and firstArg<String>() should deliver correct value as non-nullable String
Current Behavior
Nullpointer Exceptions when accessing first param in mocked answer of extension method (with mockkStatic). For some reason, index 1 provides the value (instead of index 0 as expected).
Failure Information (for bugs)
For some reason, the parameters seems to be offset by one.
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
Given this extension method of Spring Data:
fun <T, ID> CrudRepository<T, ID>.findByIdOrNull(id:ID): T?= findById(id).orElse(null)
... and following code to mock a call like repo.findByIdOrNull("myId") in a test:
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
MockK version: 1.12.4
OS: Win10-x64 Pro
Kotlin version: 1.6.20
JDK version: 17
JUnit version: 5
Spring Boot 2.6.7
Type of test: unit test
Failure Logs
Please include any relevant log snippets or files here.
Stack trace
// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
class com.xxx.xxx.xxxapi.xxxconfig.IxxxConfigRepo$Subclass0 cannot be cast to class java.lang.String (com.xxx.xxx.xxxapi.xxxconfig.IxxxConfigRepo$Subclass0 is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
java.lang.ClassCastException: class com.xxx.xxx.xxxapi.xxxconfig.IxxxConfigRepo$Subclass0 cannot be cast to class java.lang.String (com.xxx.xxx.xxxapi.xxxconfig.IxxxConfigRepo$Subclass0 is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
at com.xxx.xxx.xxxapi.xxxconfig.domain.services.xxxConfigServiceTest$setup$7.invoke(xxxConfigServiceTest.kt:126)
at com.xxx.xxx.xxxapi.xxxconfig.domain.services.xxxConfigServiceTest$setup$7.invoke(xxxConfigServiceTest.kt:42)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2117)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2091)
at io.mockk.FunctionAnswer.answer(Answers.kt:19)
at io.mockk.impl.stub.AnswerAnsweringOpportunity.answer(AnswerAnsweringOpportunity.kt:14)
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:266)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:23)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
at org.springframework.data.repository.CrudRepositoryExtensionsKt.findByIdOrNull(CrudRepositoryExtensions.kt:26)
at com.xxx.xxx.xxxapi.xxxconfig.domain.services.xxxConfigService.findById(xxxConfigService.kt:49)
at com.xxx.xxx.xxxapi.xxxconfig.domain.services.xxxConfigServiceTest.findById(xxxConfigServiceTest.kt:65)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
at com.sun.proxy.$Proxy2.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker$3.run(TestWorker.java:193)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
// -----------------------[ YOUR STACK TRACE ENDS HERE ] -----------------------
Minimal reproducible code (the gist of this issue)
// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
// kotlin essentials
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
// Spring Boot stuff
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-actuator")
developmentOnly("org.springframework.boot:spring-boot-devtools")
// Testing
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(module ="mockito-core") // We use mockK instead (see below)
}
testImplementation("io.mockk:mockk:1.12.4")
testImplementation("org.assertj:assertj-core:3.20.2") // Temp. workaround for https://github.com/assertj/assertj-core/issues/2357
testImplementation("com.ninja-squad:springmockk:3.1.1") // for spring-based unit/integration tests
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------// See above// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
The text was updated successfully, but these errors were encountered:
I get the same with MockK 1.12.5. When I put a breakpoint in the answers { } block and try and run firstArg<>(), I get this exception
org.jetbrains.kotlin.backend.common.BackendException : Backend Internal error: Exception during file facade code generation
File being compiled: mock:///fragment.kt
The root cause java.lang.NullPointerException was thrown at: org.jetbrains.kotlin.codegen.ExpressionCodegen.generateExtensionReceiver(ExpressionCodegen.java:3123)
If I try and manually get the first arg, it seems to work
val firstArg = it.invocation.args[0] asMyClassprintln(firstArg) // prints the expected result
But when I run the test, it fails.
UPDATE: this is a value class issue. It might be fixed by #872.
In my instance the function I was stubbing returned a sealed interface, and one of the implementations is a @JvmInline value class.
sealedinterfaceEvaluation {
object Pass: Evaluation
@JvmInline
value classFail(valmessage:String): Evaluation
}
every {
myService.evaluate(
data = any<MyClass>(),
)
} answers {
val dataArg = firstArg<MyClass>()
if (dataArg ==<something>) {
Evaluation.Pass
} else {
Evaluation.Fail("some message")
}
}
The workaround is to turn the value class into a data class. This isn't ideal, but it works.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
args[0]
andfirstArg<String>()
should deliver correct value as non-nullable StringCurrent Behavior
Nullpointer Exceptions when accessing first param in mocked answer of extension method (with mockkStatic). For some reason, index 1 provides the value (instead of index 0 as expected).
Failure Information (for bugs)
For some reason, the parameters seems to be offset by one.
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
Given this extension method of Spring Data:
... and following code to mock a call like
repo.findByIdOrNull("myId")
in a test:Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
Failure Logs
Please include any relevant log snippets or files here.
Stack trace
Minimal reproducible code (the gist of this issue)
The text was updated successfully, but these errors were encountered: