public class LoginActivityTest extends ActivityInstrumentationTestCase2<Login> {
Login mActivity;
private EditText username;
private EditText password;
@SuppressWarnings("deprecation")
public LoginActivityTest()
{
super("com.main.account.Login",Login.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
username = (EditText) mActivity.findViewById(R.id.username);
password = (EditText) mActivity.findViewById(R.id.password);
}
public void testPreconditions() {
assertNotNull(username);
assertNotNull(password);
}
public void testText() {
assertEquals("hello",username.getText());
assertEquals("hello123", password.getText());
}
}
錯誤我得到的是這樣的:EditTextView的單元測試。我該怎麼做?
04-18 16:03:47.132: I/TestRunner(12376): junit.framework.AssertionFailedError: expected:<hello> but was:<>
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.fail(Assert.java:47)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.failNotEquals(Assert.java:282)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.assertEquals(Assert.java:64)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.assertEquals(Assert.java:71)
04-18 16:03:47.132: I/TestRunner(12376): at com.crumbs.main.test.LoginActivityTest.testText(LoginActivityTest.java:46)
04-18 16:03:47.132: I/TestRunner(12376): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 16:03:47.132: I/TestRunner(12376): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestCase.runBare(TestCase.java:127)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult$1.protect(TestResult.java:106)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult.runProtected(TestResult.java:124)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult.run(TestResult.java:109)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestCase.run(TestCase.java:118)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
04-18 16:03:47.132: I/TestRunner(12376): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
如何測試它呢?如在,如何設置EditText的值,以便我可以在這裏測試它?
該文檔有點分散。
我收到此錯誤:04-18 16:35:26.632:I/TestRunner(13709):android.view.ViewRoot $ CalledFromWrongThreadException:只有創建視圖層次結構的原始線程可以觸及其視圖。 – Hick 2012-04-18 11:06:35
@Hiccup ..好的...我會編輯我的帖子.. – ngesh 2012-04-18 11:07:31
這並不能解決問題。 – Hick 2012-04-18 11:18:53