Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking methods with inline class return type #249

Closed
nyarian opened this issue Feb 7, 2019 · 10 comments
Closed

Mocking methods with inline class return type #249

nyarian opened this issue Feb 7, 2019 · 10 comments

Comments

@nyarian
Copy link

nyarian commented Feb 7, 2019

The issue

Seems that here are some issues with inline classes returning using returns infix function, but I am not sure if it's actually an issue inside Mockk.
When I'm trying to return a response wrapped with inline class type ClassCastException happens.
After inline modifier removal on class definition all works fine.
Here is the code:

Test method part:
every { refToObjectThatHasInlineClassReturnType.method(any()) } returns ResponseCode(0)

Invoked method's part that actually returns a wrapped type:

fun method(): ResponseCode { return ResponseCode(/*value*/) }

The inline class itself:

inline class ResponseCode(private val value: Int) { val isSuccessful: Boolean get() = value == SOME_CONSTANT }

Steps to Reproduce

  1. Create an inline class
  2. Make a return type of any method to be this inline class'es type
  3. Mock the method to return "an instance" of this type

Context

  • MockK version: 1.9.kotlin12
  • OS: Ubuntu 18.04 (Linux 4.15.0-45-generic amd64)
  • Kotlin version: 1.3.20
  • JDK version: 1.8.0_152-release
  • JUnit version: 4.12
  • Unit test

Stack trace

// -----------------------[ YOUR STACK STARTS HERE ] -----------------------
java.lang.ClassCastException: package.ResponseCode cannot be cast to java.lang.Integer

	at package.ClassThatReturnsInlineClass.method$module_debug(ClassThatReturnsInlineClass.kt:11)
	at package.CallingClass.someOtherMethod(CallingClass.kt:57)
	at package.CallingClass.someOtherMethod2(CallingClass.kt:45)
	at package.CallingClass.someOtherMethod3(CallingClass.kt:28)
	at package.TestClass$testMethod$2.invokeSuspend(TestClass.kt:109)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
	at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:236)
	at kotlinx.coroutines.EventLoopBase.processNextEvent(EventLoop.kt:123)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:69)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:45)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at package.TestClass.testMethod(TestClass.kt:109)
	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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	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.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	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.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassCastException: kotlin.Unit cannot be cast to java.lang.Void
	at eu.example.schedule.kafka.KafkaAdminImplTest$mockkVoidReturnFails$1.invoke(KafkaAdminImplTest.kt:87)
	at eu.example.schedule.kafka.KafkaAdminImplTest$mockkVoidReturnFails$1.invoke(KafkaAdminImplTest.kt:24)
	at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:22)
	at io.mockk.impl.recording.JvmAutoHinter.autoHint(JvmAutoHinter.kt:23)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:31)
	... 25 more

Dependency definition

// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
    //...
    testImplementation "io.mockk:mockk:$mockk_version"
    //...
}

whereas mockk_version property has a value of 1.9.kotlin12

@robsn
Copy link

robsn commented Mar 15, 2019

Same problem occurs for me with 1.9.2

@oleksiyp
Copy link
Collaborator

Inline classes are not supported

@mkobit
Copy link
Contributor

mkobit commented Apr 7, 2019

Is there another issue tracking and what needs to happen to support inline class?

@oleksiyp
Copy link
Collaborator

oleksiyp commented Apr 8, 2019

Check #152. There is kind of workaround. Don't know if it works

@stale
Copy link

stale bot commented Jul 23, 2019

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 put an important tag.

@stale stale bot added the stale label Jul 23, 2019
@stale stale bot closed this as completed Jul 30, 2019
@oleksiyp
Copy link
Collaborator

oleksiyp commented Nov 2, 2019

Don't reopen in favour of #152

@oleksiyp oleksiyp removed the stale label Nov 2, 2019
@tomyuval
Copy link

@oleksiyp, this seems to still have not been solved in 1.10.0. Also, #152 is about receiving a parameter of an inline class type, whereas this issue is about a return value of an inline class type, so maybe this issue should be reopened? (Note in particular that the workaround mentioned in #152 - namely not passing any() directly, but wrapped in a call to the inline class's constructor - is not applicable for the case of returning a value of an inline class type.)

@tomyuval
Copy link

tomyuval commented Nov 3, 2020

Hey, @oleksiyp, any news about this issue? Anything we can do to help?

@dibog
Copy link

dibog commented Nov 9, 2020

Hi @oleksiyp, I have the same issue with inline classes as return value of a mockk call:

every { somthing} return InstanceId("value")

The Issue #152 seems to addresses inline classes as parameter but not as return value.

@angryziber
Copy link

3 years in the making... Unfortunately, mockk's own code is very complex, not easy to contribute...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants