We had a few tests in Java and Mockito, which we are progressively converting into Kotlin and Mockk. There's a problem, though. This simple line:
verify(mockedInteractor).setIndex(1);
When we move it to mockk, we get this:
verify { mockedInteractor.index = 1 }
This of course passes the tests, as it's not actually checking that index
was set to 1. It's simply setting the mock's variable to 1. This below has the same effect.
verify { mockedInteractor.setIndex(1) }
Is there a way to verify setters?
Inside the verification block (between the opening curly bracket { and closing curly bracket } ), you write the method you want to verify. { navigator. navigateTo("Park") } tells MockK to check if the navigateTo method on the navigator object was called with the argument "Park" .
MockK Framework for Unit Testing in Kotlin (Android Mobility Development) Mocking is a process that is used in unit testing when the unit being tested has external dependencies.
Mockk is a mocking framework built for Kotlin. Like Mockito, Mockk allows you to create and stub objects within your test code. Mocking objects allows you to test in isolation other objects. Any dependency of the object under test can be mocked to provide fixed conditions, thus ensuring specific and stable tests.
A relaxed mock is the mock that returns some simple value for all functions. This allows you to skip specifying behavior for each case, while still stubbing things you need. For reference types, chained mocks are returned. Note: relaxed mocking is working badly with generic return types.
With VerifySet we can take a more direct approach in verifying that the property on our mock object is set with our value. Here we change the name with ChangeName method, and then verify that FirstName property is indeed set with that value.
Mocking property getters and setters are typically an infrequent need. But still a need. When your code requires mocking properties, JustMock provides a very clean mechanism for mocking both setters and getters, and allows you to cover even more of your code simply and cleanly.
Property set mocking is useful when you want to make sure or to verify that a particular property is set with an expected value. For this reason, we use a strict mocking. < TestMethod ()> < ExpectedException ( GetType ( StrictMockException ))> Public Sub ShouldThrowExceptionOnTheThirdPropertySetCall () ' Arrange Dim foo = Mock.
The situation would be different if you were testing the interaction between two classes - then it would be fine to set up an expectation on the property setter - as the setting action is the interaction you're testing.
You could try capture:
val fooSlot = slot<String>()
val mockBar = mockk<Bar>()
every { mockBar.foo = capture(fooSlot) } answers { }
assertEquals(fooSlot.captured, "expected")
Yes you can:
verify { mockedInteractor setProperty "index" value 1 }
There are more examples in here https://mockk.io/#private-functions-mocking--dynamic-calls
Compact solution without hardcoded string:
verify { mockedInteractor setProperty MockedInteractor::index.name value 1 }
where MockedInteractor
is mockedInteractor
class
I'm wondering if this was asked about an earlier version of Mockk, afterall, it is and old question.
verify { mockedInteractor.index = 1 }
does exactly what it says - it verifies that mockedInteractor.index
was set to 1
.
If you don't believe me, try it. Try setting mockedInteractor.index
to something other than 1
in the product code and watch this test fail.
Maybe this was a Mockk bug that has since been fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With