jeudi 26 mars 2015

Android unit testing and best practices

I am new to Android and I have a utility class (something like the following):



public static TextView constructTextView(Context context, int id,
String text, int size, int color, String font) {
TextView textView = new TextView(context);
if (id > 0) {
textView.setId(id);
}
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/" + font);
textView.setTypeface(typeface);
textView.setTextColor(color);
return textView;
}


This code was written by someone and I thought of writing unit test case for this.


I wrote a test case something like:



Context ctx = ShadowApplication.getInstance().getApplicationContext();
TextView tv = Foo.constructTextView(ctx,...);


How do I mock Typeface here as the Context object itself is mocked or something?


Do you think I need to set typeface as one of the arguments to the method than creating it in the method itself to mock it?


What I found is that when the method gets called by the test case class, the line


Typeface.createFromAsset(context.getAssets(),"fonts/" + font); fails to run.


Aucun commentaire:

Enregistrer un commentaire