You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying a mock a static method inside my Kotlin class for a unit test, but it seems the Java version of my Unit Test is successful but the Kotlin version is failing
public class MyTest {
@Test
public void givenStaticMethodWithArgs_whenMocked_thenReturnsMockSuccessfully() {
RestAdapterFactory restAdapterFactoryMock = Mockito.mock(RestAdapterFactory.class);
try (MockedStatic<RestAdapterFactory> utilities = Mockito.mockStatic(RestAdapterFactory.class)) {
utilities.when(() -> RestAdapterFactory.getInstance(Mockito.any()))
.thenReturn(restAdapterFactoryMock);
assertEquals(restAdapterFactoryMock, RestAdapterFactory.getInstance("fd"));
}
}
}
This test is running successfully but the Kotlin version of it below is failing
Kotlin Version
@Test
fun givenStaticMethodWithArgs_whenMocked_thenReturnsMockSuccessfully() {
val restAdapterFactoryMock = Mockito.mock(RestAdapterFactory::class.java)
Mockito.mockStatic(RestAdapterFactory::class.java).use { utilities ->
utilities.`when`<Any> { getInstance(Mockito.any()) }.thenReturn(restAdapterFactoryMock)
Assert.assertEquals(restAdapterFactoryMock, getInstance("test"))
}
}
Getting the below exception:
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(any());
verify(mock).someMethod(contains("foo"))
Can someone help with this ?
The text was updated successfully, but these errors were encountered:
Blog post you are referring to
https://rieckpil.de/mocking-static-methods-with-mockito-java-kotlin/
I was trying a mock a static method inside my Kotlin class for a unit test, but it seems the Java version of my Unit Test is successful but the Kotlin version is failing
@RunWith(RobolectricTestRunner.class)
This test is running successfully but the Kotlin version of it below is failing
Kotlin Version
Getting the below exception:
Can someone help with this ?
The text was updated successfully, but these errors were encountered: