Permalink
Browse files

more unit tests

  • Loading branch information...
achuinard
achuinard committed Apr 23, 2012
1 parent 2768959 commit dccbeaa2244511df5d59eed1de77c40e76091726
View
@@ -1,12 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="140"
- android:versionName="1.4-SNAPSHOT" package="com.twansoftware.basedroid">
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="140" android:versionName="1.4-SNAPSHOT" package="com.twansoftware.basedroid">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.INTERNET"/>
- <application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name"
- android:theme="@style/Theme.Sherlock">
+ <application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/Theme.Sherlock">
<activity android:label="@string/app_name" android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@@ -43,19 +43,20 @@ public BasedroidHttpClient() {
* This is an example API call. Say I have an app - when a user registers you may want to check
* if the username exists before sending the registration call through (because the registration
* call will fail). This fetches and parses the server-defined JSON object into a boolean value.
+ * @param username The username you want to check exists.
+ * @return true if it exists, false if it doesn't, null on server / deserialization error
*/
- public boolean speakbinUserExists(final String username) {
+ public Boolean speakbinUserExists(final String username) {
final String url = "http://www.speakbin.com/api/json/interaction/checkuser.sb";
final Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put("username", username);
try {
final JSONObject jo = fetchJsonObject(url, requestParams, RequestType.GET);
return Boolean.parseBoolean(jo.getString("bool"));
- } catch (Exception e) {
- System.out.println(e);
+ } catch (final Exception e) {
Ln.w(e);
}
- return false;
+ return null;
}
public HttpClient getHttpClient() {
@@ -11,8 +11,7 @@
import roboguice.activity.RoboActivity;
import roboguice.test.RobolectricRoboTestRunner;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.*;
/**
* Author: achuinard
@@ -43,7 +42,33 @@ public HttpResponse getResponse(final HttpRequest httpRequest) {
}
});
- final boolean usernameExists = client.speakbinUserExists("achuinard");
+ final Boolean usernameExists = client.speakbinUserExists("achuinard");
assertTrue(usernameExists);
}
+
+ @Test
+ public void testCheckUsernameNonexistence() {
+ Robolectric.addPendingHttpResponse(new HttpResponseGenerator() {
+ @Override
+ public HttpResponse getResponse(final HttpRequest httpRequest) {
+ return new TestHttpResponse(200, "{'bool':'false'}");
+ }
+ });
+
+ final Boolean usernameExists = client.speakbinUserExists("slkdalskjkfd");
+ assertFalse(usernameExists);
+ }
+
+ @Test
+ public void testMalformedHttpResponse() {
+ Robolectric.addPendingHttpResponse(new HttpResponseGenerator() {
+ @Override
+ public HttpResponse getResponse(final HttpRequest httpRequest) {
+ return new TestHttpResponse(200, "{THIS ISNT JSON}");
+ }
+ });
+
+ final Boolean usernameExists = client.speakbinUserExists("achuinard");
+ assertNull(usernameExists);
+ }
}

0 comments on commit dccbeaa

Please sign in to comment.