I have an Activity with EditText and Button widgets. Clicking the button prints an error message if the EditText is empty or calls setResult()
to send the value back to another Activity which launched this one.
I am writing some simple unit tests to check that this interaction works. I have two different versions of the same test:
@UiThreadTest
public void testOkButtonOnClickWithNumber() {
this.numberText.setText(Integer.toString(this.testNumber));
Assert.assertTrue(this.okButton.performClick());
Assert.assertTrue(this.activity.isFinishing());
}
public void testOkButtonOnClickWithUserInputNumber() throws Throwable {
this.sendKeys(Integer.toString(this.testNumber));
this.runTestOnUiThread(new Runnable() {
@Override
public void run() {
Assert.assertTrue(NumberFilterTest.this.okButton.performClick());
}
});
this.getInstrumentation().waitForIdle(new Runnable() {
@Override
public void run() {
Assert.assertTrue(NumberFilterTest.this.activity.isFinishing());
}
});
}
As you can see, one test simply calls setText()
on the EditText widget. The other uses sendKeys()
. Is there a preferred best practice which of these two options to use during testing? Or should I keep both tests?