question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add documentation for usage with `Mockk` and other mock libraries

See original GitHub issue

Recently, in Mockk’s slack and KotlinTest’s slack, some users have reported troubles when using Mockk with KotlinTest, due to the mockk context not being cleared after tests.

This is due to the isolation mode being incorrect for the use case, as the user normally wants the class to be instantiated once per test (InstancePerTest isolation).

As Mockk is a very used mocking library for Kotlin, among other libraries, maybe we should create a specific part in the documentation for explaining this, or a paragraph in isolation mode.

Users don’t associate directly that the “Bug” in their mocks using KotlinTest is due to isolation mode. Usually because the default mode for JUnit is single instance per test, and we don’t do that, so writing the same test “bugs” their code because of it.

What do you think, @sksamuel ?

Also summoning @oleksiyp

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
OliverCulleyDeLangecommented, Apr 13, 2020

I made a gist about getting up and running with kotest and mockk if anyone stubles upon this like i did and wants some copy paste code

0reactions
sksamuelcommented, Apr 12, 2020

It could be because MockK only scans top level fields. @kerooker is that the case ?

On Sun, 12 Apr 2020 at 10:00, Oliver Culley de Lange < notifications@github.com> wrote:

Reviving this issue from the dead, as some documentation around kotest + Mockk would still be nice. I’ve just started trying out a load of test & mock frameworks for Android and kotest & mockk seem nice given their kotlin based nature. I struggled initially setting them up together. I’ll share with you my failed naive attempts so you know what a newbie might do after reading the getting started readme…

These two fail with kotlin.UninitializedPropertyAccessException: lateinit property thing has not been initialized

class FirstAttemptSpec : FreeSpec({ @MockK lateinit var thing: Thing

beforeTest {
    MockKAnnotations.init(this)
}
"Test something"{
    every { thing.name } returns "Blah"
    thing.name shouldBe "Blah"
}

})

@ExtendWith(MockKExtension::class) class SecondAttemptSpec : FreeSpec({ @MockK lateinit var thing: Thing “Test something”{ every { thing.name } returns “Blah” thing.name shouldBe “Blah” } })

On reading up on kotest styles https://github.com/kotest/kotest/blob/master/doc/reference.md#testing-styles, it was mentioned that the two style are functionally equivalent. However I tried the other style (using the init{} block) and suddenly everything worked as expected:

class WorkingSpec : FreeSpec() { @MockK lateinit var thing: Thing init { beforeTest { MockKAnnotations.init(this) } “Test something”{ every { thing.name } returns “Blah” thing.name shouldBe “Blah” } } } @ExtendWith(MockKExtension::class) class AlsoWorkingSpec : FreeSpec() { @MockK lateinit var thing: Thing

init {
    "Test something"{
        every { thing.name } returns "Blah"
        thing.name shouldBe "Blah"
    }
}

}

This is either a bug or just somethin that needs documenting for newbies like me 😃

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kotest/kotest/issues/583#issuecomment-612629504, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFVSGVSMLNL3UVC5NS55J3RMHJPLANCNFSM4GQEEVCA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

MockK | mocking library for Kotlin
Provides DSL to mock behavior. Built from zero to fit Kotlin language. Supports named parameters, object mocks, coroutines and extension function mocking.
Read more >
MockK: A Mocking Library for Kotlin - Baeldung
Here comes the MockK library, which offers support for Kotlin language features and constructs. MockK builds proxies for mocked classes.
Read more >
MockK Guidebook, the upcoming all-in-one guide for the ...
A guide to using the MockK testing library. This resource is designed to help you learn how to use MockK, whether you're new...
Read more >
Kotlin Unit Testing with Mockk - Marco Cattaneo - Medium
Mockk is a Kotlin Library for mocking inside the Unit Test. We will see the most common scenarios and how improve the code...
Read more >
Unit testing in Kotlin projects with Mockk vs. Mockito
argumentCaptor is a function from the mockito-kotlin extension library. It helps to capture a single argument from the mocked object, usually ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found