Android http testing with Robolectric - android

I have an Android app where the main part of the app is the APIcalls.java class where I make http requests to get data from server an display the data in the app.
I wanted to create unit test for this Java class since it's the most part of the app. Here is the method for getting the data from server:
StringBuilder sb = new StringBuilder();
try {
httpclient = new DefaultHttpClient();
Httpget httpget = new HttpGet(url);
HttpEntity entity = null;
try {
HttpResponse response = httpclient.execute(httpget);
entity = response.getEntity();
} catch (Exception e) {
Log.d("Exception", e);
}
if (entity != null) {
InputStream is = null;
is = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
} catch (IOException e) {
throw e;
} catch (RuntimeException e) {
httpget.abort();
throw e;
} finally {
is.close();
}
httpclient.getConnectionManager().shutdown();
}
} catch (Exception e) {
Log.d("Exception", e);
}
String result = sb.toString().trim();
return result;
I thought I can make simple API calls from the tests like this:
api.get("www.example.com")
But every time I make some http calls from the tests, I get an error:
Unexpected HTTP call GET
I know I am doing something wrong here, but can anyone tell me how can I properly test this class in Android?

Thank you for all your answers but I found what I was looking for.
I wanted to test real HTTP calls.
By adding Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
you tell Robolectric not to intercept these requests and it allows you to make real HTTP calls

Robolectric provides some helper methods to mock http response for DefaultHttpClient. If you use DefaultHttpClient without using those methods, you would get a warning message.
Here is an example of how to mock http response:
#RunWith(RobolectricTestRunner.class)
public class ApiTest {
#Test
public void test() {
Api api = new Api();
Robolectric.addPendingHttpResponse(200, "dummy");
String responseBody = api.get("www.example.com");
assertThat(responseBody, is("dummy"));
}
}
You can find more examples by looking at Robolectric's test codes.

I answered another version of this same question, but...
What you have here is not using anything from Android, so Robolectric is basically irrelevant. This is all standard Java and the Apache HTTP library. You simply need a mocking framework and dependency injection to simulate the HttpClient (see my other answer for links). It doesn't have network access while unit testing, and so it fails.
When testing classes that use parts of the Android framework, you can use Robolectric (or similar) to mock or simulate Android.jar since your unit testing framework isn't going to have access to that either.

Related

How can i create and use only SOAP server without client on GAE?

First, sorry for my poor English.
I found this article and follow it.
https://developers.google.com/appengine/articles/soap?hl=vi
It's worked. Now i want to create only the server like that to use in other client.Is that ok?
For example when i deployed the HelloSOAPServerServlet to the abc#appspot.com
And when i want to use my service, i just paste this URL: abc#appspot.com/hellosoapserver?name=SOAP&arriving=true to the browser. How can i do something like that?
Because i want my client what use this service is the Andoird phone.
abc#appspot.com is an email address. You can not deploy GAE code to it.
When you create a GAE application you must pick a unique application name, for example mysoap. The url of your app would then be http://mysoap.appspot.com/.
After you upload your code to it you could access your SOAP at http://mysoap.appspot.com/hellosoapserver?name=SOAP&arriving=true
You got it in that example.
You made the SOAP Webservice in :
Building a SOAP Server on Google App Engine
and then you created a client that consume it from a Servlet:
Building a SOAP Client on Google App Engine Using JAX-WS
Now what you need is to make a HTTP Client call to that URL from your Android application with correct values in params.
Using the example available at http://developer.android.com/reference/java/net/HttpURLConnection.html and url provided your sample
URL url = new URL(" http://greeter-client.appspot.com/hellosoapclient?name=SOAP&arriving=true");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
In readStream is where you read the response from your service hosted at GAE
readStream can be something like this:
private static String readStream(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

How to use webview or open URL in AndEngine - Android

I'm utilizing a game engine called AndEngine (which I'm completely new to) in my Android app. I need to load a different URL from the application based on what position an onscreen joystick is in (uploading to a .cgi server). The dilemma is that I cannot open a URL connection! This may seem simple, but I've looked everywhere, tried multiple solutions and nothing's worked. In basic Android, I've always used a WebView (loadUrl() method), and it worked well. However, I have no idea to how to create a webview while also using AndEngine. My preference is that the connection did not show (loaded underneath the AndEngine scene?) because I will need the screen for other things. I've also tried other solutions. I just tried this code, but when I checked the server, nothing was opened:
#Override
public void onLoadResources() {
//methods n/a to this question
try {
URL url = new URL(setUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
return scene; // AndEngine return statement
}
private void readStream(InputStream in) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
**I've tried using the HTTPConnection class before (without AndEngine) to open up a URL, but to no avail. So it may be that I was just doing something wrong here. Using AndEngine GLES2. If more info is needed, let me know (this is my first question on SO).
Also tried setting up my .xml layout on AndEngine using
#Override
protected int getLayoutID() {
return R.layout.main;
}
but it says: "The method getLayoutID() of type Control must override or implement a supertype method"
Edit in response to Nicolas Gramlich: Internet permissions were set and compiler was originally at 1.6. Still don't know what the issue is.
xml
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Set java compiler compliance to 1.6
I solved my issue. I had to run all network operations on a thread separate from the main one (else it will throw a NetworkOnMainThread exception). I don't know why nothing else worked, but this did the trick! Here I'm creating a new thread with the action I want to perform, and then starting it after exceptions are taken care of. I found my answer here
new Thread(new Runnable() {
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("your_url");
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

Error java.lang.RuntimeException: Stub! in Android with Fitnesse testing

I'm trying to create a test fixture using Fitnesse framework, and I want to test a function which retrieves data from a server (RESTFUL service). My test case is very simple:
public class FriendListActivityFixture extends ColumnFixture {
public int URL;
public String test() {
JSONArray array = JsonHelper.getJsonArrayFromUrl("http://107.22.209.62/android/get_users.php");
return array.toString();
}
}
public static JSONArray getJsonArrayFromUrl(String url) {
InputStream input = null;
String result = "";
JSONArray jsonArray = null;
try {
HttpClient httpclient = CustomHttpClient.getHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
input = entity.getContent();
}
catch (Exception e) {
Log.e(TAG + ".getJsonArrayFromUrl(String url)", "Error in http connection " + e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "iso-8859-1"), 8);
StringBuilder content = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
content.append(line + "\n");
}
input.close();
result = content.toString();
}
catch (Exception e) {
Log.e(TAG + ".getJsonArrayFromUrl(String url)", "Error parsing result " + e.toString());
}
try {
jsonArray = new JSONArray(result);
}
catch (JSONException e) {
Log.e(TAG + "getJsonArrayFromUrl(String url)", "Error converting data " + e.toString());
}
return jsonArray;
}
And here is the Fitnesse test page:!path fitnesse.jar
!path C:\Program Files (x86)\Android\android-sdk\platforms\android-10\android.jar
!path C:\Users\chan\git\Spotr\Spotr\bin\classes
!|com.csun.spotr.fitnesse.FriendListActivityFixture|
|URL|test?|
|0|"wwa"|
Since it's just a demo, my test might look a bit silly at the moment. Unfortunately, I keep getting these errors:
java.lang.RuntimeException: Stub!
at android.util.Log.e(Log.java:15)
at com.csun.spotr.util.JsonHelper.getJsonArrayFromUrl(JsonHelper.java:75)
at com.csun.spotr.fitnesse.FriendListActivityFixture.test(FriendListActivityFixture.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at fit.TypeAdapter.invoke(TypeAdapter.java:109)
at fit.TypeAdapter.get(TypeAdapter.java:97)
at fit.Fixture$CellComparator.compareCellToResult(Fixture.java:371)
at fit.Fixture$CellComparator.access$100(Fixture.java:357)
at fit.Fixture.compareCellToResult(Fixture.java:299)
at fit.Fixture.check(Fixture.java:295)
at fit.ColumnFixture.check(ColumnFixture.java:51)
at fit.Binding$QueryBinding.doCell(Binding.java:215)
at fit.ColumnFixture.doCell(ColumnFixture.java:37)
at fit.Fixture.doCells(Fixture.java:171)
at fit.Fixture.doRow(Fixture.java:165)
at fit.ColumnFixture.doRow(ColumnFixture.java:25)
at fit.Fixture.doRows(Fixture.java:159)
at fit.ColumnFixture.doRows(ColumnFixture.java:18)
at fit.Fixture.doTable(Fixture.java:153)
at fit.Fixture.interpretTables(Fixture.java:99)
at fit.Fixture.doTables(Fixture.java:79)
at fit.FitServer.process(FitServer.java:81)
at fit.FitServer.run(FitServer.java:56)
at fit.FitServer.main(FitServer.java:41)
And I have no idea what is it telling me? I wrote other testing method like add(), substract(), everything worked fine. I wonder does this error involve running a long task on the main thread? Any idea?
android.jar contains only stub implementation of the classes. It provides the means for you app to build, once you have your APK you must run it on an android device or emulator.
If I'm not wrong you are trying to run on host's JVM.
You can fix this problem and test within your IDE really easily by using Roboelectric.
As dtmilano said, you can't run Android code on your laptop as is, but Roboelectric basically substitutes the actual method implementations for the stubs in Android.jar.
Two things to watch out for if you go with this solution:
Make sure that your JAR import is above the Android JAR in your dependencies list if using IntelliJ
It defaults to blocking HTTP requests under the assumption that you don't actually want to change the state of a server somewhere. You can, however, disable this quite easily: Roboelectric.getFakeHttpLayer().interceptHttpRequests(false);
As mentioned previously, Android jars are not good for anything beyond compiling. Even most innocent things are stubbed out completely. But you can still use them in host VM unit tests with good mocking frameworks. My choice is jmockit:
Not sure whether it will work with Fitnesse though
I had this same issue because of the "static" declaration. In my case, .build() caused the issue in a static variable. The simple solution was to not declare the variable static. It can be final. Try removing "static" from the getJsonArrayFromUrl method and see if it works.

android.net vs java.net and the different URI classes

I'm writing an application with a model object that will expose a Restful interface to some web services. I've noticed that in Android there is a java.net.URI and an android.net.URI class. What are the benefits to using one versus the other? Has anyone else run into this and found that one works better than the other?
In the below code I'm parsing the individual parts of the URI into a java.net URI object so I can then call httpGet(URI uri). However, would there be any performance benefits or any benefits at all to using the android.net classes or just calling httpGet(String url)?
public class RestMethods {
private String protocol;
private String host;
private Integer port;
private URI uri;
public String restGet(String path) throws MalformedURLException, InterruptedException, ExecutionException{
StringBuilder builder = new StringBuilder();
try {
// Execute HTTP Post Request
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
} catch (ClientProtocolException e) {
return "Client Protocol Exception Exception " + e.toString();
} catch (IOException e) {
return "IO Exception " + e.toString();
}
return builder.toString();
}
...
//Other rest methods, Getters, and setters down here
...
}
Yes, there will be performance benefits. The android team doesn't have to conform to the same backwards compatibility restrictions when coding the android.net package that they do when they're implementing the java.net package. Therefore they can make better optimizations.

How to parse XML (Fogbugz XML API)?

I am creating an Android application that connects to the Fogbugz XML API (sends a request to the API, receives back an XML file). Right now, I am creating a service that will handle these requests, and parse each in order to access usable information. What would be the best way to do this? If I am getting an inputStream, should I use the SAX parser?
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("My URL THAT RETURNS AN XML FILE");
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String responseString = "";
String temp;
while ((temp=bufferedReader.readLine()) != null)
{
responseString += temp;
}
if (entity != null) {
entity.consumeContent();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I suggest that you use some XML DOM library like XOM or Dom4j. It will be much easier for you to work with tree structure than with SAX events. Personally, I use XOM in Foglyn -- FogBugz for Eclipse plugin. You can pass InputStream directly to your SAX/XOM/Dom4j parser, there is no need to build string first. Furthermore, make sure you use correct encoding ... your code is broken in this regard. (When you pass InputStream to your parser, this is handled by parser. In XOM you can use new Builder().build(InputStream) method).
One FogBugz API hint ... when getting details about case, and you don't need events (comments), don't fetch them at all. I.e. don't put events into list of columns.

Categories

Resources