-
Notifications
You must be signed in to change notification settings - Fork 752
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
Using Volley from a Unit test? #346
Comments
Forgot to say - if I write this as an 'instrumented test' then it works |
AFAIK it should work if HttpUrlConnection does. Obviously not a good practice to make real HTTP calls in a unit test but I don't see a reason Volley should cause problems here. The stack trace doesn't make sense to me, because "Volley.newRequestQueue(context, HurlStack())" does not depend on org.apache.http.StatusLine. Are you somehow referencing this in your test? Do you have a full code sample demonstrating the problem? |
Yes I don't really want to leave it as a unit test, I just wanted to play around without having to deploy the app to the emulator. Not sure whether it's something corrupted on my machine |
Simple example is here: https://github.com/davidfrancisandroidemul/VolleySampleForGithub2 I noticed I'm having the same issue as in this stack overflow post: |
Thanks for the sample code! I don't understand why the JVM attempts to initialize this class. The only use of StatusLine in HttpStack is in the parent class performRequest method, which certainly isn't invoked at any point in this test. My understanding is that JVM classloading is generally lazy in practice, but that there are no strict guarantees in terms of how it decides to load classes, and so perhaps it's being too eager. I can just add fake empty versions of these classes to the test project (StatusLine/BasicStatusLine, HttpEntity/BasicHttpEntity, HttpResponse/BasicHttpResponse) and use HurlStack.executeRequest without issue. (I also have to set testOptions.unitTests.returnDefaultValues = true to prevent calls to android.os.Log methods from failing). It doesn't seem like it actually attempts to initialize these empty classes - if I add a static initializer block, it doesn't appear to run. But it's loading them. Ultimately, the important thing is that this doesn't happen on Android devices, and it sounds like that's consistent with what you're seeing. For unit tests, unless someone can track down why these classes are loading and propose a fix for it in Volley, I think you'll need to do one of the following workarounds:
Closing this out as infeasible, but happy to take a closer look if someone can explain why this is happening in more detail and propose a fix. |
Thanks for looking into this, sounds like a good place to leave it as you suggest. |
Robolectric doesn't seem to load those classes, at least not in 4.5.1. I'm trying to shadow volley so that I can inject NetworkResponse objects for integration testing with the code that we've written at the request layer. (edit) |
(per google/volley#346, this is not recommended.)
Hi,
Not really an issue, more of a question.
I'm rather new to Android development so please bear with me.
TL;DR question is:
Can I use Volley from a JUnit test (which runs on my Macbook, not on the Android emulator)
and make a real Http call?
I'm using Kotlin if that makes any difference.
Problem I'm seeing is when I hit the line:
Volley.newRequestQueue(context, HurlStack())
I get an error:
This is just a simple test to play around with the Volley library and make real http calls.
Am I allowed to use it like this?
Regards,
David
Edit: Volley version: com.android.volley:volley:1.1.1
The text was updated successfully, but these errors were encountered: