Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Mockito mocking of finals is broken #2677
Robolectric is not compatible with latest Mockito experimental feature for mocking finals:
I'm with Robolectric 3.1.2.
I'm also running into this issue. Tests run with the RobolectricTestRunner will fail whenever the mocking feature is enabled. If I switch the test runner to JUnit4 then I don't get any errors. I made a small test project to verify.
Agreed, especially when trying to mock Retrofit 2 since the Retrofit and Call objects are final. Since the final class mocking in Mockito 2 is still an incubating feature I think we will have to wait until it is fully ready before finding a cause of this issue.
If you are testing out Retrofit, I highly recommend using OKHttp's MockWebserver. It will at least let you make sure your mappings are correct and the input outputs are hitting the correct endpoints. Since both Mockito and Robolectric are doing byte code manipulation it may be a while before a solution is found that works with both.
@emartynov Yes I realize that. And Yes MockWebserver gets away from true unit testing, but in the case where you can't get around the mocking of the final classes, (you run into similar issues when writing tests for code that uses OkHttp), the mockwebserver allows you to at least test the results, and not incure the full overhead of a true Integration Test.
referenced this issue
Nov 28, 2016
Mockito maintainer here. I just debugged this and this turned out to be a combination of bugs in Mockito and Robolectrics.
Mockito throws an exception from the mock maker plugin's constructor upon an unexpected interaction with the Robolectrics class loader. This causes Mockito to attempt cleaning the stack trace what should not happen at this point as it is not a user fault but a configuration error. Cleaning stack traces runs via a plugin where the manager just failed loading the mock maker plugin what in turn results in a null pointer exception as the class initializer cannot correctly created the plugin manager where the class initializer was not executed to its end. I already fixed this in Mockito.
The mock maker does not work as the
A correct class loader implementation must request a class from its parent first. In this case, I understand why this is not happening as the system class loaders class's should be shadowed. However, this should happen by super-seeding the class loader, not by some child-first mechanism. I implemented this as a proposal pull request. I did not get the tests of your project to work, unfortunately, there are dependencies missing and I am not familiar enough with the Android space to get this working quickly.
While investigating this, I found a second bug in the Robolectrics class loader where the standard class employment is broken when loading classes via
Let me know if anything is unclear. Cheers.
referenced this issue
Dec 9, 2016
This was referenced
Jan 4, 2017
changed the title from
Robolectric and Mockito 2.1+
Mockito mocking of finals is broken
Jan 5, 2017
Ya, we are working on it.…
On Feb 3, 2017 11:48 AM, "james" ***@***.***> wrote: This issue is causing is quite a few problems. Any word on if a fix is being worked on? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#2677 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AGBaeBLuNfBMFptzg2W1cSFTnMtys-79ks5rY4RugaJpZM4KUy5T> .