ch3d
10/07/2016, 1:41 PMMockito+Kotlin.
I have a class with a property, something like:
internal val hasSomething = storage.contains(Consts.SOMETHING)
Then in test I want to mock it:
`when`(preferences.hasSomething).thenReturn(true)
But I got org.mockito.exceptions.misusing.MissingMethodInvocationException
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
But when I switch from property to a method:
internal fun hasSomething() = storage.contains(Consts.SOMETHING)
and then
`when`(preferences.hasSomething()).thenReturn(true)
Everything's okay
Bytecode with property:
// access flags 0x11
public final getHasSomething$production_sources_for_module_testModule()Z
L0
LINENUMBER 14 L0
ALOAD 0
GETFIELD com/package/MyClass.hasSomething : Z
IRETURN
L1
LOCALVARIABLE this Lcom/package/MyClass; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
Bytecode with method:
// access flags 0x11
public final hasMobileKey$production_sources_for_module_testModule()Z
L0
LINENUMBER 14 L0
ALOAD 0
GETFIELD com/package/Preferences.storage : Lcom/package/Storage;
GETSTATIC com/package/Preferences.Companion : Lcom/package/Preferences$Companion;
INVOKEVIRTUAL com/package/Preferences$Companion.getSOMETHING ()Ljava/lang/String;
INVOKEINTERFACE com/package/Storage.contains (Ljava/lang/String;)Z
IRETURN
L1
LOCALVARIABLE this Lcom/package/Preferences; L0 L1 0
MAXSTACK = 2
MAXLOCALS = 1
Could you help to figure our the reason?