Reputation: 60081
I have a simple test class
class SimpleClassTest {
private fun <T> anyObject(): T {
return Mockito.anyObject<T>()
}
lateinit var simpleObject: SimpleClass
@Mock lateinit var injectedObject: InjectedClass
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testSimpleFunction() {
simpleObject = SimpleClass(injectedObject)
simpleObject.simpleFunction()
verify(injectedObject).settingDependentObject(anyObject())
}
}
It works fine and pass.
Since the private generic anyObject()
function is only used once, so I decide to inlining it (manually) i.e. remove the need of that function, whereby I change from
verify(injectedObject).settingDependentObject(anyObject())
to
verify(injectedObject).settingDependentObject(Mockito.anyObject<DependentClass>())
However this now error as
java.lang.IllegalStateException: Mockito.anyObject<DependentClass>() must not be null
Anything I did wrong inlining the function call to a direct statement?
Is there anything different between using
private fun <T> anyObject(): T {
return Mockito.anyObject<T>()
}
and the below?
Mockito.anyObject<DependentClass>()
Upvotes: 1
Views: 782
Reputation: 100378
The source for Mockito.anyObject()
:
/**
* Matches anything, including null.
* <p>
* This is an alias of: {@link #any()} and {@link #any(java.lang.Class)}
* <p>
* See examples in javadoc for {@link Matchers} class
*
* @return <code>null</code>.
*/
public static <T> T anyObject() {
return (T) reportMatcher(Any.ANY).returnNull();
}
This method sets some internal state for Mockito, and then proceeds to return null
. Since you're expecting a non-null instance in your settingDependentObject()
method, the runtime fails.
Why your first method succeeded, however, I'm not sure. Casting null
to T
seemed to work a while ago, but it doesn't work for me anymore. For that matter, I can't get your first implementation to succeed either.
Upvotes: 5