I have downloaded this code from developer.android
public class SpinnerTestActivity extends
ActivityInstrumentationTestCase2<SpinnerActivity> {
private SpinnerActivity mActivity;
private Spinner mSpinner;
private SpinnerAdapter mPlanetData;
public static final int ADAPTER_COUNT = 9;
public static final int INITIAL_POSITION = 0;
public static final int TEST_POSITION = 5;
private String mSelection;
private int mPos;
public SpinnerTestActivity() {
super("com.android.example.spinner", SpinnerActivity.class);
} // end of SpinnerActivityTest constructor definition
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
mActivity = getActivity();
mSpinner = (Spinner) mActivity
.findViewById(com.android.example.spinner.R.id.Spinner01);
mPlanetData = mSpinner.getAdapter();
} // end of setUp() method definition
public void testPreConditions() {
assertTrue(mSpinner.getOnItemSelectedListener() != null);
assertTrue(mPlanetData != null);
assertEquals(mPlanetData.getCount(), ADAPTER_COUNT);
} // end of testPreConditions() method definition
public void testSpinnerUI() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
mSpinner.requestFocus();
mSpinner.setSelection(INITIAL_POSITION);
} // end of run() method definition
} // end of anonymous Runnable object instantiation
); // end of invocation of runOnUiThread
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
for (int i = 1; i <= TEST_POSITION; i++) {
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
} // end of for loop
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
mPos = mSpinner.getSelectedItemPosition();
mSelection = (String) mSpinner.getItemAtPosition(mPos);
TextView resultView = (TextView) mActivity
.findViewById(com.android.example.spinner.R.id.SpinnerResult);
String resultText = (String) resultView.getText();
assertEquals(resultText, mSelection);
}
}
My question is: How the testSpinnerUI is invoked? From where? I have read the junit documentation but cannot figure out.. Thanks!
Stupid question, though. I found the answer in this blog.
The lifecycle of a test case is basically this:
construction,setUp(),tests run,tearDown(), anddestruction. ThesetUp()method is used to do any general initialization used by all of specific tests. Each test to be run in the test case is implemented as its own method, where the method name begins with “test”. ThetearDown()method is then used to uninitialize any resources acquired by thesetUp()method.Each specific test will have it’s own method beginning with “test” – the “test” method name prefix is case sensitive!
My initial question was how the test method ran, since no one is calling it. But from the above test each method should sart with test, so as to be identified.