Currently incompatible with inline class
See original GitHub issueWe want to experiment with inline classes in our application, but we are running into a compatibility issue with mockito-kotlin
. I know inline classes are currently experimental and expected to change, so if it’s not worth the effort looking into at the moment I understand, I just want to start a conversation about it.
Issue
I can repro my issue with a unit test that looks like this:
interface Methods {
...
fun inlineClass(): InlineClass
}
inline class InlineClass(val value: String)
class MockingTest : TestBase() {
@Test
fun testMockStubbing_inlineClass() {
/* Given */
val mock = mock<Methods>() {
on { inlineClass() } doReturn InlineClass("A")
}
/* When */
val result = mock.inlineClass()
/* Then */
expect(result).toBe(InlineClass("A"))
}
}
I get a stack trace that looks like this
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method test.InlineClass.box-impl, parameter v
at test.InlineClass.box-impl(Classes.kt)
at test.MockingTest$testMockStubbing_inlineClass$mock$1$1.invoke(MockingTest.kt:19)
at com.nhaarman.mockitokotlin2.KStubbing.on(KStubbing.kt:70)
at test.MockingTest.testMockStubbing_inlineClass(MockingTest.kt:94)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
which points to where we call on { inlineClass() }
or expect(result)
.
I tried to dig down with the debugger but couldn’t really get too far into what’s going on. I assume reflection is being used to create an instance of InlineClass
but that has some core logic that an instance can’t be created without a value?
Has anyone seen this or have any ideas of how to go about fixing this? Or should we wait until inline classes mature a little bit?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:19
- Comments:9
Top GitHub Comments
For me the following is working:
It looks like another option may be using Mockito’s
Answer
type.The below is working for me with
mockito-kotlin:2.1.0
andmockito-core:2.24.5
Answer
itself seems like a bit of an odd feature, but it seems to work here.One possible issue with the above is the
doAnswer
andthenAnswer
being type constrained, when it doesn’t seem like theAnswer
API in Mockito really requires it?