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
robolectric should throw ClassNotFoundException when Android class not loaded #2478
Comments
I'm going to mark this as a duplicate of #2566 what do you think? I think if we make sure we always try to load classes from the android-all jar if present this kinds of issues will go away. Feel free to reopen if I've missed the point. |
Thanks. |
Are you using Gradle? I don't think that the android.jar should be in the runtime system classpath, only the compile time classpath. |
Reopening. |
I'm investigating an issue several people reported with regards to OkHttp and robolectric. OkHttp does platform checks to dynamically load classes. In this case, we test whether android.security.NetworkSecurityPolicy is present. That class was introduced in API 23. When running tests with Robolectric 3.1-rc1 and API 21, robolectric is not throwing ClassNotFoundException.
A simple project demonstrating the issue is here, https://github.com/kungfoo/okhttp-on-jdk-with-robolectric. Note: you will need to upgrade robolectric (3.1-rc1) and OkHttp (3.3.1).
In my investigation, I think the problem is here, https://github.com/robolectric/robolectric/blob/master/robolectric/src/main/java/org/robolectric/internal/bytecode/InstrumentingClassLoader.java#L137. When that class is loaded from urls, it properly returns null. It then calls super.getResourceAsStream. The project itself depends on API 23, so the class does get loaded because the Android jar is in the class path.
I was thinking a fix might be: if urls returns null and it's an Android class (starts with "android."?), throw ClassNotFoundException.
The text was updated successfully, but these errors were encountered: