lannyf
lannyf

Reputation: 11025

mockk, what is just run

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.

Upvotes: 24

Views: 31125

Answers (1)

lannyf
lannyf

Reputation: 11025

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() }

Upvotes: 39

Related Questions