I have an activity with a spinner, and I load spinner items from server. My problem is if I run the whole ApplicationTest class my spinner is null, but if I run only the testEditCard
method the spinner isn't null and I don't know what is wrong.
Here is my ApplicationTest.class
:
public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mainActivity;
private Button showLibBtn;
private String token;
private LibraryCardsActivity libraryCardsActivity;
private LibraryEditCard libraryEditCard;
private CustomButton btnEditCard;
public ApplicationTest() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false);
mainActivity = getActivity();
showLibBtn = (Button) mainActivity.findViewById(R.id.showLib);
token = mainActivity.token;
Thread.sleep(10000);
}
@MediumTest
public void testCardsActivity() throws Exception{
loadLibraryCardsActivity();
final CustomEditText inputET = (CustomEditText) libraryCardsActivity.findViewById(R.id.inputET);
assertNotNull("cvvInputET is null", cvvInputET);
/**
* Insert CVV code.
*/
final String inputValue = "1234";
libraryCardsActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
inputET.setText(inputValue);
}
});
CustomButton btnOk = (CustomButton) libraryCardsActivity.findViewById(R.id.cardsActivitybtnOk);
assertNotNull("btnOk is null", btnOk);
TouchUtils.clickView(this, btnOk);
Thread.sleep(5000);
assertEquals("", inputET.getText().toString());
}
@MediumTest
public void testEditSelectedCard() throws Exception{
/**
* Load LibraryCardsActivity
*/
loadLibraryCardsActivity();
Thread.sleep(5000);
final Spinner spCards = (Spinner) libraryCardsActivity.findViewById(R.id.spCards);
assertNotNull("spCards is null", spCards);
libraryCardsActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
spCards.setSelection(2);
}
});
Thread.sleep(4000);
CardModel cardModel = (CardModel) spCards.getSelectedItem();
assertNotNull(cardModel);
btnEditCard = (CustomButton) libraryCardsActivity.findViewById(R.id.cardsActivitybtnEditCard);
/**
* Load LibraryEditCard activity
*/
loadEditCardActivity();
CustomTextView cardNumber = (CustomTextView) libraryEditCard.findViewById(R.id.editCardNumberTV);
assertNotNull("cardNumber is null", cardNumber);
assertEquals(cardModel.getCardNumber(), cardNumber.getText().toString());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
private void loadLibraryCardsActivity() throws Exception{
/**
* Load LibraryCardsActivity
*/
Instrumentation.ActivityMonitor cardsMonitor = getInstrumentation().addMonitor(LibraryCardsActivity.class.getName(), null, false);
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
showLibBtn.performClick();//TouchUtils.clickView(this, showLibBtn);
}
});
Thread.sleep(10000);
libraryCardsActivity = (LibraryCardsActivity) cardsMonitor.waitForActivity();
assertNotNull("libActivity is null", libraryCardsActivity);
btnEditCard = (CustomButton) libraryCardsActivity.findViewById(R.id.cardsActivitybtnEditCard);
getInstrumentation().removeMonitor(cardsMonitor);
}
private void loadEditCardActivity() throws Exception{
/**
* Load LibraryEditCard activity
*/
Instrumentation.ActivityMonitor editMonitor = getInstrumentation().addMonitor(LibraryEditCard.class.getName(), null, false);
libraryCardsActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
btnEditCard.performClick();
}
});
Thread.sleep(4000);
//TouchUtils.clickView(this, btnEditCard);
libraryEditCard = (LibraryEditCard) editMonitor.waitForActivityWithTimeout(3000);
assertNotNull("libEditCardActivity is null", libraryEditCard);
getInstrumentation().removeMonitor(editMonitor);
}
}
And I am getting NullPointerException
for spinner in testEditSelectedCard()
method.
Aucun commentaire:
Enregistrer un commentaire