Java 如何在Android studio中测试截击库?
我想使用Volley库将JSON文件从Android应用程序传输到RESTAPI服务器。首先,我想用JUnit测试库,看看我的请求是否正确发送,而不在应用程序中运行它们。这是我的测试:Java 如何在Android studio中测试截击库?,java,android,junit,android-volley,Java,Android,Junit,Android Volley,我想使用Volley库将JSON文件从Android应用程序传输到RESTAPI服务器。首先,我想用JUnit测试库,看看我的请求是否正确发送,而不在应用程序中运行它们。这是我的测试: public class NetworkCommunicationTest extends AndroidTestCase { private static final String JSON_URL = "https://www.example.com/data.json"; Context
public class NetworkCommunicationTest extends AndroidTestCase {
private static final String JSON_URL = "https://www.example.com/data.json";
Context context;
@Test
public void testSendGet() {
context = new MockContext();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Assert.assertTrue("",true);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(jsonObjectRequest);
}
}
我怎样才能解决这个问题?如何打印response.toString()消息
只是为了获得更多的参考资料,没有我链接的库,我遇到了一个可登录的问题。遵循本教程谢谢您的回答,但问题是如何在Android中使用,而是如何在Android studio中的测试中使用。我能够使用本主题中描述的FakeHttpStack完成这个技巧:
java.lang.NullPointerException
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:48)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
at tests.NetworkCommunicationTest.testSendPost(NetworkCommunicationTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)