2016-06-30 2 views
0

私は単純なrobolectricクラスを持っています。私はtextViewのString値をチェックします。テストでは、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!"); 
関連する問題