having a test using mockito-inline 3.8.0, it was using java and converted to kotlin then it got compiler error below:
Type mismatch.
Required: (Mutable)Map<String!, String!>?
Found: MutableMap<*, *>!
the function to be verified is logActionEvent(@Nullable Map<String, String> customParams)
and the ArgumentCaptor is defined as
lateinit private var additionalParamCaptor: ArgumentCaptor<MutableMap<*, *>>
ArgumentCaptor.forClass(MutableMap::class.java).also { additionalParamCaptor = it }
or
lateinit private var additionalParamCaptor: ArgumentCaptor<MutableMap<*, *>>
additionalParamCaptor = ArgumentCaptor.forClass<MutableMap<*, *>, MutableMap<*, *>>(MutableMap::class.java)
if change from ArgumentCaptor<MutableMap<*, *>> to ArgumentCaptor<Map<*, *>> the error become:
Type mismatch.
Required: (Mutable)Map<String!, String!>?
Found: Map<*, *>!
How to define the ArgumentCaptor?
here is the related test:
@RunWith(RobolectricTestRunner::class)
class TestK {
lateinit private var eventLogger: EventLogger
lateinit private var additionalParamCaptor: ArgumentCaptor<MutableMap<*, *>>
@Before
fun setUp() {
eventLogger = Mockito.spy(EventLogger.getInstance())
ReflectionHelpers.setStaticField(EventLogger::class.java, "sEventLoggerSingleton", eventLogger)
ArgumentCaptor.forClass(MutableMap::class.java).also { additionalParamCaptor = it }
}
@Test
@Throws(NoSuchMethodException::class, IllegalAccessException::class, InvocationTargetException::class, InstantiationException::class)
fun logNotificationOpenEvent() {
// setup
val params: Map<String, String?> = HashMap() // = fillMap()
// fill in the map. i.e.
params[KEY_MESSAGE_TYPE] = "type"
// test
EventLogger.getInstance().logActionEvent(EVENT_NAME_OPEN, params)
// verify
Mockito.verify(eventLogger).logActionEvent(ArgumentMatchers.eq(EVENT_NAME_OPEN), additionalParamCaptor.capture()) //<=== does not compile
val realMap = additionalParamCaptor.value as Map<String, String>
verifyRivendellMetadata_ShouldExist(realMap, false)
Assert.assertEquals("type", realMap[KEY_MESSAGE_TYPE])
......
}
}
the class to be tested
public class EventLogger {
public static EventLogger getInstance() {
if (sEventLoggerSingleton == null) {
sEventLoggerSingleton = new EventLogger();
}
return sEventLoggerSingleton;
}
public void logActionEvent(@Nullable Map<String, String> customParams) {
// do logging
}
