Issue
I have an AsyncTask that I would like to write a test for. It has a TextView that is passed in:
public LoadAssetTask(TextView target, boolean skipSingleLineBreak, InputStream in) { ...}
How do I write an Adroid JUnit test for this? Is there a way to use a Mock Object to Mock an activity with a text field, I'd hate to create a whole activity or use one that is already since I want to test just this class. I tried using a mock activity but you have to add that to the manifest and I don't want to do that for each and every AsyncTask that I have.
Solution
Have you tried using android.test.ActivityInstrumentationTestCase2? This class runs JUnit tests for methods in another Activity. In fact, it's the only way to run a JUnit test against a method in an Activity (or methods in a contained class of an Activity). AsyncTask has to run within an Android context (because it uses Android thread APIs), but I assume that ActivityInstrumentationTestCase2 can communicate with any thread in the process.
Documentation for ActivitInstrumentationTestCase2 is at http://developer.android.com/tools/testing/testing_android.html
Answered By - Joe Malin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.