Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you verify a property setter using mockk?

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?

like image 452
Sergi Juanola Avatar asked May 14 '20 15:05

Sergi Juanola


People also ask

How do you verify in MockK?

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" .

What is MockK used for?

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.

What is @MockK?

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.

What is relaxed true in MockK?

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.

How to verify that property on mock object is set with value?

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.

Do I need mocking property getters and setters?

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.

What is property set mocking and how to use it?

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.

Can We set an expectation on a property setter?

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.


4 Answers

You could try capture:

val fooSlot = slot<String>()
val mockBar = mockk<Bar>()
every { mockBar.foo = capture(fooSlot) } answers { }
assertEquals(fooSlot.captured, "expected")
like image 131
xilef Avatar answered Oct 19 '22 15:10

xilef


Yes you can:

verify { mockedInteractor setProperty "index" value 1 }

There are more examples in here https://mockk.io/#private-functions-mocking--dynamic-calls

like image 27
Eric Lee Avatar answered Oct 19 '22 15:10

Eric Lee


Compact solution without hardcoded string:

verify { mockedInteractor setProperty MockedInteractor::index.name value 1 }

where MockedInteractor is mockedInteractor class

like image 21
Hammer Avatar answered Oct 19 '22 16:10

Hammer


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.

like image 1
Myles Bennett Avatar answered Oct 19 '22 17:10

Myles Bennett