Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
    Anderson Reinkordt
    will try that
    Oleksiy Pylypenko
    you can always create some function that accepts mock and do the same thing
    I mean
    do a same sequence of every to intialize mock
    but with creating and storing mock itself you need to be careful
    as spek probably indeed run it in parallel
    I have written a few tests in Android where I am using Mockk to mock some objects. I am running into an issue that when I run the whole test suite, the tests fail with a crash but when I run each test separately, it works fine. Is there some cleanup required between the tests?
    The stack trace shows that Mockk injects a class that acts like contextWrapper and that's causing the failure.
    Oleksiy Pylypenko
    It might be the case that some classes are crtitical to the system and mocking them going to cause instability. Please minimize your example and submit it as a github issue if you think this should be fixed in the future

    Hello I am new to using Mockk.

    I am trying to mock an object however I keep getting the following error io.mockk.MockKException: Missing calls inside every { ... } block.

    I am guessing this should be pretty straight forward

        fun setup() {
        fun `should return an unmatched filter result if there is no context match`() {
            every { FilterFactory.getFilters("invalid") } returns emptyList()
    Andriy Matkivskiy
    Hi all, is there any way to verify that every call mocked with every { } was called ?
    As far as I know such approach is implemented in Mockito and called strict mode
    Andriy Matkivskiy
    Is there any similar functionality in Mockk?
    Joakim Forslund
    So Im trying to mockk a class which has constructor parameters, it seems like mockk is not able to override these variables for som reason because they are empty when I later try to use a method to log if they have been set or not.
    How do I create a mockked class what I also have specified constructor parameters on?
    Oleksiy Pylypenko
    This is not implemented yet

    Hello, I have the following question related to returning an instance of a final class from a spyk. I have the following code:

    val foo = Foo()
    val spyFoo = spyk(foo)
    val instanceOfFinalBar = BarFactory.createInstance() // interop with final Java class with package private constructors
    every {spyFoo.bar } returns instanceOfFinalBar

    The above fails with:

        at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
        at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144)
        at io.mockk.proxy.jvm.transformation.JvmInlineInstrumentation.retransform(JvmInlineInstrumentation.kt:28)
        at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1.invoke(RetransformInlineInstrumnetation.kt:19)
        at io.mockk.proxy.common.transformation.RetransformInlineInstrumnetation$execute$1.invoke(RetransformInlineInstrumnetation.kt:6)

    Any clues what I'm doing wrong? I assume it's related to the fact that Bar is final. I was expecting you cannot mock a final class, but you can return an instance of a final class from a mock.

    Mark Robinson
    Hi - trying to mock a call to Clock.instant() to return progressively increasing Instants (in a more generic way, returning subsequently different answers for the same call). Is this possible with mockk?
    Mark Robinson
    Never mind found it, returnsMany or andThen
    Stanislav Kinzl
    Hello, I'm trying to add mockk-android dependency of version 1.9.3 for some time now. I want it in order to statically mock getCurrentTimeMillis function in System, so I can override the current time the device returns, so I can snapshot test some cards we have in our app in different states (expires soon etc...). But I'm unable to do that because every time I try to sync project after adding the 'androidTestImplementation "io.mockk:mockk-android:1.9.3"' into the gradle, I get "Unable to resolve dependency" error. I don't get the error for normal mockk (I already used it in the project). I'm I missing something? Thank you.
    Hi - having an issue with a verify(exactly =) call. I have 2 tests that individually mock the same the same method, in one case to return true and in the other to return false. In the former case I'm verifying that the mock is called an exact number of times, in the latter case the number of calls is not a concern. If I run the tests individually there is no problem, they work as expected. However, if I run the full suite then the verify fails because it includes the count for the second case. Is there a way to reset the count for verify between test methods? (The tests are in a Junit5 @Nested block)
    Immediately after posting this I found the following solution - @BeforeEach
    fun clear() {
    Karthick Chinnathambi
    Hi ,
    I have a class with private constructor and exposed a public Builder that instantiates the class
    how can i verify if the private constructor is invoked with specified params ?
    Bulat Galeev
    @oleksiyp Greetings. My colleague wants to contribute. Does it needed to be discussed first here or somewhere else or he can just make a PR?

    Hi everybody, i'm having problems trying to test an Observable with concatMap and then compose. The function i want to test looks like:

    interactor?.let { interactor ->
    interactor.isServerConfigurationEmpty().concatMap { isEmpty ->
    if (isEmpty)
    .subscribe({ res -> ... }, {})

    How should i mock the result for interactor.isServerConfigurationEmpty() and interactor.isServerConfigurationEmpty().compose(...)
    I have post a question in stackoverflow if someone wants to check it.

    Oleksiy Pylypenko
    Sorry, gitter is not relyable. Just have seen your message @bulatgaleev
    Best to create issue / initial PR and then discuss things
    Romeo Disca
    Hi, I have fun process(set: Set<Entity>) {
    set.forEach{ }
    how can I verify that forEach is executed on each element of the set?
    Hey! looked everywhere but couldn't find an example where I can mock an android context. is that possible with this library?

    I can t' find the solution to this issue :
    this is my code

      every { fragment.findNavController() } returns navController
      val viewModelStoreSpy = spyk<ViewModelStore>(recordPrivateCalls = true)
     every { navController.getViewModelStoreOwner(navGraphId).viewModelStore } returns viewModelStoreSpy

    it works with robolectric but it doesn't work with Espresso.
    the exeption I have is

    Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. Check device logcat for details
    Test running failed: Instrumentation run failed due to 'Process crashed.'

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    There is no way to debug the exception or detect the logs. all I know is that it crashes exactly where mockkStatic is called.
    note: I'm using mockk-android as per documentation.
    @oleksiy or any takers?

    To be correct it fails whenever I launchFragmentInContainer. Although same solution works for robelectric
    Kartikay singh
    Hi guys
    I am using spyk to mock a foreground service in android
    problem is i am not able to mock startForeground method as it is final
    i had to wrap it with another open method to mock
    is this the right approach ?
    MockK doesnt support mocking final methods ?
    i am using mockk 1.9
    any feedback is appreciated...thanks in advance
    Kartikay singh
    I have above issue in API level27 or less...it is working fine in api level 28 or later
    Raul Bocanegra Algarra
    Hi guys
    I am having an issue calling verify on an extension function, it make the test fails when the extension function has been called. I created an issue here: mockk/mockk#372 Do any of you have experienced this or have any workaround? Thanks!
    Kartikay singh
    can someone look into this issue... i will be very grateful
    Kartikay singh
    Please guys need little help here mockk/mockk#377
    Dmitry Ivanishkin
    Hi! I want to find an elegant way to implement tracing logs for interface in Kotlin. So all implementers will have no logging responsibility, despite logging will work. Can I use mockk for it some way? For example, to define some interceptor that instantiate suitable wrapper? Or maybe you can suggest some existing solution?
    Max Weinbrown

    Hey all. I'm trying to mock out some Fuel calls and keep getting an error that makes it look like any<String>() is being evaluated wrong... any help would be much appreciated:

    every { Fuel.post(any<String>()) } returns mockk()


    no protocol: /-299039bc776dbdf3
    java.net.MalformedURLException: no protocol: /-299039bc776dbdf3
    (haven't posted to SO or GH issues because I might be fundamentally misunderstanding something)

    Hi guys. I started using mockk today and it looks great. But I can't seem to override a private property in a spy object.

    I have this object

    private val driverMapSnapshotMap: MutableMap<Int, SnapshotImage> = mutableMapOf()

    in a class that I spy on using

    viewModel = spyk(DriverListViewModel(), recordPrivateCalls = true)

    But when I try to make it fill up with mock values I get an error

            every {
                viewModel getProperty "driverMapSnapshotMap"
            } returns(mapOf(1 to mockkClass(DriverRemoteModel::class)))

    The error I get

    io.mockk.MockKException: Missing calls inside every { ... } block.

    Any thoughts?

    Max Weinbrown
    ^^No expert, but I think you are spying on an instance you create in the spyk call, which is going to get immediately discarded
    @orelzion and then, I'm assuming your test code is actually creating an instance of DriverListViewModel somewhere, which isn't the one being spied on
    Thank you @maxscott. I actually create DriverListViewModel only in the @Setup call. There's no other instance