Skip to content
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

Open
dave-r12 opened this issue May 29, 2016 · 4 comments
Labels

Comments

@dave-r12
Copy link

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.

@dave-r12 dave-r12 changed the title ClassNotFoundException not thrown when Android class not loaded robolectric should throw ClassNotFoundException when Android class not loaded May 29, 2016
@jongerrish
Copy link
Contributor

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.

@dave-r12
Copy link
Author

Thanks.

@jongerrish
Copy link
Contributor

Are you using Gradle? I don't think that the android.jar should be in the runtime system classpath, only the compile time classpath.

@adazh
Copy link

adazh commented Mar 7, 2019

Reopening. Class.forName("android.security.NetworkSecurityPolicy") should throw a ClassNotFoundException on SDKs prior to 23.

@xian xian reopened this Mar 7, 2019
@xian xian added the defect label Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants