mocking only one call at a time with mockk

At the excellent post Mocking is not rocket science are documented two alternatives: returnsMany specify a number of values that are used one by one i.e. first matched call returns first element, second returns second element: every { mock1.call(5) } returnsMany listOf(1, 2, 3) You can achieve the same using andThen construct: every { mock1.call(5) } returns …

Read more

Mock static java methods using Mockk

After mockk 1.8.1: Mockk version 1.8.1 deprecated the solution below. After that version you should do: @Before fun mockAllUriInteractions() { mockkStatic(Uri::class) val uriMock = mockk<Uri>() every { Uri.parse(“test/path”) } returns uriMock } mockkStatic will be cleared everytime it’s called, so you don’t need to unmock it before using it again. However, if that static namespace …

Read more

Unit testing coroutines runBlockingTest: This job has not completed yet

As can be seen in this post: This exception usually means that some coroutines from your tests were scheduled outside the test scope (more specifically the test dispatcher). Instead of performing this: private val networkContext: CoroutineContext = TestCoroutineDispatcher() private val sut = Foo( networkContext, someInteractor ) fun `some test`() = runBlockingTest() { // given … …

Read more