Open cgm-aw opened 2 months ago
I'm also encountering the same issue. @Raibaz @zkkv @ any ideas?
Hi, I also got the same problem today. @cgm-aw : Could you elaborate how you circumvented this issue using https://mockk.io/#property-backing-fields? Maybe you could just provide the adjusted test code. My current attempts do not work, unfortunately.
Hi @bjpe
yes I added it to the description. Use this:
every { testObject.someField } propertyType Optional::class answers { "mockValue" }
I also noticed that formatting in my post was a mess, I fixed that.
Hi @cgm-aw , thank you very much! I also tried this, but it failed in my case because the field is not accessible (the mocked class is located in a library). Anyway, thanks a lot!
Introduction
Hi everyone,
I encountered a behavior of mockk, where, if the backing field has a different type than the getter, a warning is printed to the logs but the test continues and passes. This seems weird to me - I would either expect the test to fail or no warning message. My suggestion: Add a parameter to the mockk() function to be able to suppress the warning, and if suppression is not active, the test will fail (or the other way around).
I'm aware of this: https://mockk.io/#property-backing-fields With the solution presented there, everything works fine. However, if you do not use this mechanism, you will just get confusing error messages and probably an inconsistent behavior.
Prerequisites
Expected Behavior
Either a broken test or no log message
Current Behavior
A warning message is printed, including a stack trace, but the test passes
Steps to Reproduce
Create this class (this is a Java class!)
Create this Kotlin test:
You will see this log entry:
Using propertyType yields the correct behavior:
every { testObject.someField } propertyType Optional::class answers { "mockValue" }
Context
Stack trace
Minimal reproducible code (the gist of this issue)
See above