运行Android JUnit测试时,.getText()。toString()无法访问TextView中的文本值,导致测试失败

时间:2011-02-03 20:12:37

标签: android unit-testing junit

运行Android JUnit测试时,.getText()。toString()无法访问TextView中的文本值,导致测试失败。

http://www.android10.org/index.php/articlesother/203-unit-testing-with-the-junit-testing-framework

我从上面的网页下载了代码并运行了单元测试。

所有单元测试都失败了。框架没有从result.getText()。toString();

返回任何值

为什么会这样?

//获得结果

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));
}
跟踪
junit.framework.AssertionFailedError:
     

添加结果应为79.5但是       在com.mamlambo.article.simplecalc.test.MathValidation.testAddDecimalValues(MathValidation.java:57)       at 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)

1 个答案:

答案 0 :(得分:-2)

你在哪里初始化了对象结果?

添加结果应为79.5,但“” ...

也许这会对你有帮助。