2016-06-30 64 views
0

我有簡單的robolectric類,我檢查了我的textView的字符串值。測試只是檢查我們的TextView是否存在並且具有文本「Hello World!」junit.framework.AssertionFailedError:TextView包含正確的文本

@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) 
@RunWith(RobolectricGradleTestRunner.class) 
public class MainActivityTest { 
private MainActivity activity; 


@Test 
public void validateTextViewContent() { 
    activity = Robolectric.setupActivity(MainActivity.class); 
    TextView tvHelloWorld = (TextView) activity.findViewById(R.id.tvHelloWorld); 

    assertNotNull("TextView could not be found", tvHelloWorld); 
    assertTrue("TextView contains correct text", 
      "Hello world!".equals(tvHelloWorld.getText().toString())); 
    } 
} 

當我運行它雖然,我得到以下錯誤。

junit.framework.AssertionFailedError: TextView contains correct text 

at junit.framework.Assert.fail(Assert.java:57) 
at junit.framework.Assert.assertTrue(Assert.java:22) 
at testing.theo.androidtextviewrobo.MainActivityTest.validateTextViewContent(MainActivityTest.java:29) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

任何想法?

感謝

+1

的第一件事是,你可以代替assertTrue的使用的assertEquals。但我認爲這可能無法幫助你。 – toshkinl

+0

不,它不能與assertEquals一起工作:( – Theo

+0

嘗試在聲明前添加一些文本到該TextView中) tvHelloWorld.setText(「」); – toshkinl

回答

0

問題固定!!!我刪除了assertTrue,並將此行

assertEquals(tvHelloWorld.getText().toString(), "Hello World!");