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>()