I am novice with testing. When I develop my app, I use Robotium in order to test my apps, but now, I would like test some functions that are members of my Util class. For example:
public static boolean internetConnection(Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i == null)
return false;
else if (!i.isConnected())
return false;
else if (!i.isAvailable())
return false;
return true;
}
or for example:
public static boolean isTabletDevice(Context context) {
if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
// test screen size, use reflection because isLayoutSizeAtLeast is
// only available since 11
Configuration con = context.getResources().getConfiguration();
try {
Method mIsLayoutSizeAtLeast = con.getClass().getMethod(
"isLayoutSizeAtLeast", int.class);
Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con,
0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
return r;
} catch (Exception x) {
x.printStackTrace();
return false;
}
}
return false;
}
How could I test these functions?
Thanks a lot!!
For the first one you should stub/mock the connection manager.
Simulate all the conditions the connection manager may produce and make sure your method returns the proper value for each. Rather than nested else if
statements you may just want to return the value. IMO this makes the code a bit cleaner and easier to think about.
For the second I'm not even sure I'd bother, since you're basically making sure the Android call works for the value you provide–you'd need to explain what specifically you want to test. That reflection works? That you're calling it with the right size?