New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock properties in a spied object, test run with Robolectric #263
Comments
Same problem here, i cant mock spy properties Steps do reproduce
|
I get the same exception as OP's. In my case with a mock, not a spy, and in on-device instrumented test (not with Robolectric, the standard AndroidJunit4 runner). This happens only in instrumented tests. mockk<AlbumCameraManager> {
every { photoLiveData } returns fakeLiveData
}
Since I also use Mockito to work around other problems (like #237), could it be any interference? Because I noticed that this error didn't occur some times when launching isolated tests (i.e. running the test file or method directly) as opposed to launching the gradle task for all the instrumented tests, that makes it happen 100% of the times. |
Please check 1.9.2 as one race condition was fixed there. |
@oleksiyp 1.9.2 did not fix the issue for me |
For me, |
Such thing may happen if Kotlin generate direct field access. Please check on byte code level if there is a method call. I think in Kotlin some optimizations were introduced that reduce getter generation at some cases |
On version |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as |
Brief explanation. It is nearly impossible to mock private properties as they don't have getter methods attached. This is kind of Kotlin optimization and solution is major change. So keeping it closed as kind of duplicate for #104 |
The workaround I did is supplying my private property with 'VisibleForTesting' annotation. |
Any update on this ?? |
Expected Behavior
Properties of the spied objects should be mocked successfully.
Current Behavior
Properties of the spied objects are not mocked whatever property is public or private. But if I lazy initialize the field everything works perfectly.
Failure Information (for bugs)
I run a test with Robolectric and if I initialize property directly in the spied object, the property is not mocked, but as soon as I do lazy initialization mocking works.
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
I have Fragment class like this:
And I have tests to test it:
When I run like this, I have the real object instead of the mock for
presenter
property.If I set
presenter
property private, I haveio.mockk.MockKException: Missing calls inside every { ... } block.
And only if I lazy initialize
presenter
property withprivate val presenter by lazy { RoomPresenter(BluetoothAdapter.getDefaultAdapter()) }
everything works good.Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
Failure Logs
Please include any relevant log snippets or files here.
Stack trace
The text was updated successfully, but these errors were encountered: