I have a static class that I want to mock, I can do this successfully in java. But when I convert to kotlin and tests now fail with the error
Misplaced or misused argument matcher detected here: -> at TestTest.testWrapperStatic$lambda$0(TestTest.kt:19)
Any ideas on what is different when mocking static in kotlin?
Class to mock,
object TextUtilsWrapper {
@JvmStatic
fun createFromParcel(p: Parcel): CharSequence {
return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p)
}
}
Passing Java test case,
public class TestTest extends BaseMockingUnitTest {
@Test
public void testWrapperStatic() {
Parcel parcel = Mockito.mock(Parcel.class);
final MockedStatic<TextUtilsWrapper> textUtilsWrapperMockedStatic = mockStatic(
TextUtilsWrapper.class);
textUtilsWrapperMockedStatic.when(
() -> TextUtilsWrapper.createFromParcel(any())).thenReturn("hello");
assertThat(TextUtilsWrapper.createFromParcel(parcel), is("hello"));
}
}
Failing kotlin test case,
class TestTest : BaseMockingUnitTest() {
@Test
fun testWrapperStatic() {
val parcel = Mockito.mock(Parcel::class.java)
val textUtilsWrapperMockedStatic = Mockito.mockStatic(
TextUtilsWrapper::class.java
)
textUtilsWrapperMockedStatic.`when`<Any> { createFromParcel(ArgumentMatchers.any()) }
.thenReturn("hello")
MatcherAssert.assertThat(createFromParcel(parcel), Matchers.`is`("hello"))
}
}