I am fairly new to testing and am trying to explore jUnit Tests and Robotium
. My application has 4 screens.
Splash Screen --> Screen2--> Screen 3--> Autocomplete Screen.
[init()] [ check values generated by init() method]
Autocomplete screen is where the user types something, and the app performs autocomplete from a large database of Products. In the splash screen I am initializing many app variables using an init()
method in another class. This init()
method will load usedr preferences, check for database creation etc...(The product database comes pre-packaged in the apk and i am copying the database on first launch and mark it in SharedPreferences
as copied=true).
Problem : How do i isolate the testing of Autocomplete Screen ? The onCreate of AutocompleteActivity depends on init() having been called in the SplashScreen. I am not sure when the jUnit creates an instance of the Activity
, (probably in the constructor? ). Here is my TestCaseCode:
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.ListView;
import com.jayway.android.robotium.solo.Solo;
import com.supervalu.mobile.android.AutoCompleteActivity;
import com.supervalu.mobile.android.db.LocalDb;
public class AutocompleteTest extends
ActivityInstrumentationTestCase2<AutoCompleteActivity> {
private Solo solo;
public AutocompleteTest() {
super("com.****.*****.*****", AutoCompleteActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
LocalDb.init(getActivity().getApplicationContext());
solo = new Solo(getInstrumentation(), getActivity());
}
public void test1Character() {
solo.sleep(2000);
solo.enterText(0, "c");
solo.sleep(10000);
View v = getActivity().findViewById(
com.*****.****.*****.R.id.list);
solo.waitForView(v);
assertTrue(((ListView) v).getChildCount() > 0);
}
public void test3Character() {
LocalDb.init(getActivity().getApplicationContext());
solo.enterText(0, "che");
View v = getActivity().findViewById(
com.*****.****.*****.R.id.list);
solo.waitForView(v);
assertTrue(((ListView) v).getChildCount() > 0);
}
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
The test case keeps creashing because the onCreate requires some values from the LocalDb
whcih had to be initialiazed by init()
method.
I cannot add the init() function before the super call in the constructor. Is there any work around for this , or do i need to start the test case from the splash screen ?
Problem 2 : If i start testing from splash scree, I have to first navigate to the autocmplete screen before being able to perform any tests on it. Suppose i did that as well, then for each test: test1character() the class executes setUp() and then the test and then tearDown(), then it restarts whole sequence for test3characters() function. It gets very painful to keep repeating navigating to autocompltete screen every time before actually testing inputs. Any suggestions on this as well ?
I am not sure what approach to take. Can someone please guide me?