运行 Android JUnit 测试时,.getText().toString() 无法访问 TextView 中的文本值,导致测试失败
运行 Android JUnit 测试时,TextView 中的文本值无法通过 .getText().toString() 访问,从而导致测试失败。
http://www.android10 .org/index.php/articlesother/203-unit-testing-with-the-junit-testing-framework
我从上面的网页下载了代码并运行了单元测试。
所有单元测试均失败。框架没有从 result.getText().toString() 返回任何值;
为什么会这样呢?
CODE// 得到结果
String mathResult = result.getText().toString();
assertTrue("Add result should be 98 " + ADD_RESULT + " but was "
+ mathResult, mathResult.equals(ADD_RESULT));
}
public void testAddDecimalValues() {
sendKeys(NUMBER_5_DOT_5 + NUMBER_74 + "ENTER");
String mathResult = result.getText().toString();
assertTrue("Add result should be " + ADD_DECIMAL_RESULT + " but was "
+ mathResult, mathResult.equals(ADD_DECIMAL_RESULT));
}
TRACEjunit.framework.AssertionFailedError:
相加结果应该是 79.5,但是却是 在 com.mamlambo.article.simplecalc.test.MathValidation.testAddDecimalValues(MathValidation.java:57) 在 java.lang.reflect.Method.invokeNative(Native 方法) 在 android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204) 在 android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194) 在 android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 在 android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 在 android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
When running Android JUnit tests the values of text in TextView can not be accessed by .getText().toString() causing tests to fail.
http://www.android10.org/index.php/articlesother/203-unit-testing-with-the-junit-testing-framework
I downloaded the code from the above web-page and ran the unit tests.
All unit tests failed. The framework did not return any values from the result.getText().toString();
Why should this be?
CODE
// get result
String mathResult = result.getText().toString();
assertTrue("Add result should be 98 " + ADD_RESULT + " but was "
+ mathResult, mathResult.equals(ADD_RESULT));
}
public void testAddDecimalValues() {
sendKeys(NUMBER_5_DOT_5 + NUMBER_74 + "ENTER");
String mathResult = result.getText().toString();
assertTrue("Add result should be " + ADD_DECIMAL_RESULT + " but was "
+ mathResult, mathResult.equals(ADD_DECIMAL_RESULT));
}
TRACE
junit.framework.AssertionFailedError:
Add result should be 79.5 but was
at com.mamlambo.article.simplecalc.test.MathValidation.testAddDecimalValues(MathValidation.java:57)
at java.lang.reflect.Method.invokeNative(Native
Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在哪里初始化了对象结果?
添加结果应为 79.5,但 "" 位于 ...
也许这会对您有所帮助。
Where do you have the Object result initialized?
Add result should be 79.5 but was "" at ...
Maybe that will help you.