Hi everyone, Is there any way to modify value of `...
# test
d
Hi everyone, Is there any way to modify value of
val
in test case other than reflection. I don't want this variable gets modified in main package. Appreciate you help.
o
Sometimes it is possible to mock(mockk.io) val if it is materialized in code as property i e. has get/set.
If it is private, then it is materialized in bytecode as field, not property
t
It might be better to explain what you’re trying to test - there’s probably a better way around it
d
Let's say, I have a data class Person(val name, val age). and i have mocked this person object, and i need to pass some real values for name/age of mocked object
as name/age is declared as val, i am getting val cannot be reassigned compilation error. ofcourse I can create a new object for Person instead of mocking it. But its a tedious task to create a real object for a class which has about 10+ fields
t
Is it any more/less tedious than mocking it?
That’s not what mocking libraries were created for- although you wouldn’t get that impression these days.
if you can create and use an object directly in your test, you should
d
Thanks @tddmonkey Definitely i will try to use real obj whenever possible. Also I just wanted to understand that is there any ways to pass/change val field in data class which i am not aware of.