I have a business logic in UI-less fragment which I have to test. I have tried 2 options and both have failed.
1. Use AndroidTestCase and create mock activity.
Following code
@Override
protected void setUp() {
Intent i = new Intent(getTestContext(), TestActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getTestContext().startActivity(i);
}
throws an exception
Permission denied: checkComponentPermission() reqUid=10104
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.xxx.iabsample.test/.TestActivity } from ProcessRecord{40769510 28116:com.xxx.iabsample/10070 (pid=28116, uid=10070) requires null
2. Use ActivityInstrumentationTestCase2 with mock activity
Code
public class IabTest extends ActivityInstrumentationTestCase2<TestActivity> {
public IabTest() {
super("com.xxx.iabsample.test", TestActivity.class);
}
}
throws an exception
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.xxx.iabsample/.test.TestActivity }
It seems that it tries to start activity from test-target app, not from the test app.
So, what is the correct way to test fragments?