14

Could not find explanation about the "just run", what does it mean when stub a function with it?

Will it make the mock object to call its real function, or make the function run a stub which does nothing?

Is there sample for showing some real use case?

@Test
fun `mocking functions that return Unit`() {
    val SingletonObject = mockkObject<SingletonObject>()
    every { SingletonObject.functionReturnNothing() } just Runs. // ???

    SingletonObject.otherMemberFunction(). //which internally calls functionReturnNothing()
    //...
}

with or without this every { SingletonObject.functionReturnNothing() } just Runs stub, the test is doing same.

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • 2
    Hopefully, https://notwoods.github.io/mockk-guidebook/docs/tips/unit/ will answer your question. – imvishi Jul 09 '21 at 07:33
  • 1
    sorry, it did not. what does it "When stubbing a function that returns nothing, MockK provides a few shortcuts." mean? – lannyf Jul 09 '21 at 11:21

1 Answers1

17

Copy of the answer from @Raibaz:

just runs is used for methods returning Unit (i.e., not returning a value) on strict mocks.

If you create a mock that is not relaxed and invoke a method on it that has not being stubbed with an every block, MockK will throw an exception.

To stub a method returning Unit, you can do

every { myObject.myMethod() } just runs

No, it doesn't (like mockito's .thenCallRealMethod()) :)
It "just runs", meaning it does not do anything.

To run the real method you can use:

every { ... } answers { callOriginal() }
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
lannyf
  • 9,865
  • 12
  • 70
  • 152