by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Nikola-Milovic
    @Nikola-Milovic
    @bhavsi sorry, new question. Dont know how to use gitter, kinda weird. Idk why they dont switch to slack
    César Díez Sánchez
    @cesards

    Hi everyone!

    MockK newbie here. We are making a case study to move all our mockito-kotlin tests to be MockK dependant :) There are a few reasons why we would do it, but I'm here to ask about a problem we run into.

    I don't understand why MockK crashes during a test initialization. We get this error:

    Caused by: java.lang.RuntimeException: Method println in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.

    But neither the SUT doesn't have any android dependencies. I haven't seen any open/close issues related to this and I'm wondering if someone else has been in the same situation. I had to add

    testOptions {
            unitTests.returnDefaultValues = true
        }

    to the build.gradle to make it work

    Joseph
    @JosephGaiser

    When settings up a mock, does anyone know how to acheive something like this?

    every {  personService.someFunction(any<Person>(it.personId  == "12345") } returns foo

    We are looking to set up the mock to match on any instance of the passed object that has a field set to a specific value (in the example personId = "12345")

    formarfr
    @formarfr

    @JosephGaiser ,

    Looks like you can do this with Matcher Extensibility:

    fun MockkMatcherScope.someCustomMatcherFunction(person: Person, personId: String)  = match<Person> {
              it.personId == personId
    }
    
    every { personService.someFunction(someCustomMatcherFunction(any<Person>(), "12345")) } returns Foo
    Richard Glen Domingo
    @chardskarth

    Hi everyone! Can someone direct me to some multiplatform setup example?

    mockk-js is not published with 1.9.3, is this expected?

    Oleksiy Pylypenko
    @oleksiyp
    JS is experimental... all tou can actually do with multiplatform code is to test it on JVM
    Richard Glen Domingo
    @chardskarth
    Ohh. I'm okay with experimental. (Cause all I'm really doing is experimenting). Is that why mockk-js is not published?
    Oleksiy Pylypenko
    @oleksiyp
    Yes, because it have few hard to solve problems
    Richard Glen Domingo
    @chardskarth
    Maybe I could lend a help here. Could you link here the first hard problem you think you need to solve to make it work with Kotlin/JS?
    Richard Glen Domingo
    @chardskarth
    @oleksiyp I'm experiencing @gtbono 's problem as well. When building mockk from source, ANDROID_HOME is being setup. I'm relatively new with gradle but is there a way to skip this? I wanna lend help with Kotlin/JS first. No android needed
    Oleksiy Pylypenko
    @oleksiyp
    Turning off android should be possible once you remove all modules that require android in 'settings.gradle'. There is a bigger problem with Gradle builds. Currently, I am not able to make all these things build because of gradle and Android plugin. Basically builds are not reproducible with old versions of the Gradle and Android plugin. I tried upgrading to newer Gradle, but this became a kind of nightmare. So I just want to warn that it might be quite hard....
    Lance Hannestad
    @lhannest
    Hello, is it possible to spy on a class that is final but doesn't have an empty constructor?
    Lance Hannestad
    @lhannest
    And without copying an instance of that class, I guess.
    GaneshvrBhat
    @GaneshvrBhat
    Hello is there anyway to clear mockkObject(<classname>) ?
    I don't want to mock obj before every test, instead would like to mock once per class. Problem is with coEvery{} return <value> behaviour/value gets propagated to other tests. So I would like to do clearMocks, but could not do it successfully.
    Lance Hannestad
    @lhannest

    And without copying an instance of that class, I guess.

    Yes, I think my trouble was that I was trying to copy an instance of a class that should not be copied. So, is the only alternative making the class open?

    Patric Wilms
    @pwilms
    Hey, is there any advice to mockk the final class java.net.URL
    alexeenkoff
    @alexeenkoff
    Hi! Can anybody help me?
    Try to use mockK in ktor project.
    on mockk object initialization throws exception:
    Caused by: java.io.IOException: Cannot run program ""/Applications/IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/bin/java"": error=2, No such file or directory
    But my idea settings of java has different setting of jdk path
    barrettdavis01
    @barrettdavis01
    Does anyone have an example of using every to throw and then to verify if its been thrown?
    basically trying to verify that my Catch block is updated db and logging the error.
    Nícolas Iensen
    @nicolasiensen
    Hey, I'm trying to use mockk to mock Kotlin's readLine function, but nothing seems to work. Any suggestions on how to get this to work? Thanks!
    Stian H. Nesbø
    @Stianhn
    Are there an equivalent functionalilty for Mockito's doReturn() where the stubbed function is not invoked?
    Jan Pascal Maas
    @jpmaas
    Hello, is there an equivalent to Mockito's mock(...).thenCallRealMethod()? Or should this be done using relaxed mocks?
    Iury Souza
    @iurysza
    @jpmaas I think you might want to uses the Spy api
    Jan Pascal Maas
    @jpmaas
    @iurysza correct me if I'm wrong, but as far as I understand Spy's, they are a copy of a real object. I know I can verify calls of a spy, but can I also mock specific calls with it?
    Todd O'Bryan
    @toddobryan
    Is there a way to mock an internal val of a mock that returns an instance of an internal class, so I can't even import the right class to put in propertyType?
    Raman Gupta
    @rocketraman
    Hey all... mockk is great, but I do have an odd performance issue. I have a package with 4 unit test classes. When running all 4, one particular test takes 25 seconds to run. When running just the class which contains that test, that same test takes 1s to run. From thread dumps, it looks like the time is taken in mock creation. I thought maybe that mocks were being created on top of mocks, so I registered an extension that clears all mocks after each test class is complete. This did not help. Any ideas?
    Raman Gupta
    @rocketraman
    The tests are definitely interacting somehow... the more other tests that run, the longer it takes to run that test.
    Raman Gupta
    @rocketraman
    There is no TRACE output during the long pause.
    Raman Gupta
    @rocketraman
    From a profiler, looks like the perf hit isn't in the mocking process itself -- its in calling methods on a 3rd party library which were mocked in other tests but in this test should now be unmocked via unmockAll.
    Luan Albineli
    @luanalbineli
    Hi guys, is there any way to capture and execute a suspend lambda with a generic result? I've opened a StackOverflow question, but I had no replies: https://stackoverflow.com/questions/61446602/capture-suspend-lambda-parameter-with-generic-result-in-a-mockked-method
    Michael Johnson
    @ATXMJ
    Hello (mockk.io) World! I was curious if Mockk currently supports "real partial mocks", as Mockito's thenCallRealMethod() feature? I was going to create a github issue requesting the feature but figured I should ask here first to make sure the functionality doesn't already exist.
    3 replies
    Christian Sousa
    @xrofa

    Hey guys, having an issue with this.

    class A {
    ...

    fun foo(): String { return "bar" }
    }

    class B {
    private val instance = A()

    fun bar(): String {
    return instance.foo()
    }
    }
    In a test, how can I mock the A when testing B? I tried with mockStatic and so on, but with no success yet..

    I want to be able to mock A.foo when called inside the B class.

    Can anyone help with this?

    Mattia Tommasone
    @Raibaz
    @xrofa you can use mockkConstructor to have A() return a mocked instance and the drive its behavior
    Giovanni Tempobono
    @gtbono
    @oleksiyp I want to contribute and start solving issues in mockk, but I can't get it to build from source. When I do a git clone and try to build the master branch (should I try to build another?), there's a lot of Gradle projects, what should I look for to build? mockk-root? or should I go more specific and only build mockk-jvm?
    Oleksiy Pylypenko
    @oleksiyp
    @gtbono main build.gradle should be buildable, what error do you face?
    Giovanni Tempobono
    @gtbono

    @oleksiyp I get an error of missing signing.keyId, what is this signing key, and how can I generate one? I've searched the wiki and the issues and I haven't found anything about it

    FAILURE: Build failed with an exception.

    • Where:
      Script 'C:\Users\gtbon\Code\mockk\gradle\upload.gradle' line: 76

    • What went wrong:
      A problem occurred configuring project ':mockk-agent-android'.

      missing signing.keyId to sign archives

    • Exception is:
      org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':mockk-agent-android'.
      at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(LifecycleProjectEvaluator.java:75)
      at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:68)
      at org.gradle.configuration.project.LifecycleProjectEvaluator.access$600(LifecycleProjectEvaluator.java:51)
      at org.gradle.configuration.project.LifecycleProjectEvaluator$NotifyAfterEvaluate.run(LifecycleProjectEvaluator.java:194)
      at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:402)

        ....

    So I thought I must be doing something wrong, this is what I get when I try to import the mockk-root gradle project.

    Oleksiy Pylypenko
    @oleksiyp

    try to put something fake alike

    signing.keyId=
    signing.password=
    signing.secretKeyRingFile=

    in

    $HOME/.gradle/gradle.properties

    I have signing keys because I need to upload releases
    if it the case that build is not working without these keys
    it is first thing to fix :-)
    Hans van Luttikhuizen
    @pindab0ter
    I'm fairly new to mocking in general and Mockk specifically. I'm wondering how to configure a nested mockk in such a way that I can easily change variables for each test. So I want a scope to set a variable and want the mocks in that scope to reflect that. How do I go about doing that?
    Do I make a before call in that scope where I change just that, where I rebuild the entire mock in the before of the entire test?
    Are there best practices regarding this?
    Hans van Luttikhuizen
    @pindab0ter
    For anyone wondering, I ended up creating a real base object and cloning that with the adjusted values before each group of tests. It turned out I didn't even need a mock.
    Vladyslav
    @senioroman4uk
    Implemented a fix for mockk/mockk#273 can someone take a look mockk/mockk#444

    @oleksiyp I get an error of missing signing.keyId, what is this signing key, and how can I generate one? I've searched the wiki and the issues and I haven't found anything about it

    FAILURE: Build failed with an exception.

    • Where:
      Script 'C:\Users\gtbon\Code\mockk\gradle\upload.gradle' line: 76

    • What went wrong:
      A problem occurred configuring project ':mockk-agent-android'.

      missing signing.keyId to sign archives

    • Exception is:
      org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':mockk-agent-android'.
      at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(LifecycleProjectEvaluator.java:75)
      at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:68)
      at org.gradle.configuration.project.LifecycleProjectEvaluator.access$600(LifecycleProjectEvaluator.java:51)
      at org.gradle.configuration.project.LifecycleProjectEvaluator$NotifyAfterEvaluate.run(LifecycleProjectEvaluator.java:194)
      at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:402)

        ....

    So I thought I must be doing something wrong, this is what I get when I try to import the mockk-root gradle project.

    Try adding ext { localrepo = 'true' } in subprojects section of root build.gradle. It worked for me

    JonathanAaron
    @JonathanAaron
    Hi I'm trying to verify that my catch caught an error thrown by a function within my try block?
    I have an every { myService.getSomething() } throws 500Exception()