Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit4 Android TestField text test

My test is:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class TipActivityTests {

    @Rule
    public ActivityTestRule<TipActivity> mActivityRule = new ActivityTestRule<>(TipActivity.class);

    @Test
    public void initialValues() {
        onView(withId(R.id.tip_label_base_price)).check(matches(ViewMatchers.withText("45"")));
    }
}

But I get the error 'with text: is "45"' doesn't match the selected view. Expected: with text: is "45":

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "45"' doesn't match the selected view.
Expected: with text: is "45"
Got: "AppCompatTextView{id=2131689669, res-name=tip_label_base_price, visibility=VISIBLE, width=266, height=106, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=141.0, y=96.0, text=$ 45.00, input-type=0, ime-target=false, has-links=false}"

It doesn't make sense to me, it should not print the actual value of the field vs the compared value?

like image 634
Daniel Gomez Rico Avatar asked Jul 07 '15 21:07

Daniel Gomez Rico


2 Answers

I had the same problem and spent quite some time trying to understand the root cause. It turned out the strings were not equals, that's why it was failing. The error message is not really explicit because it's printing the whole object properties etc instead of saying: expected: "foo", received: "bar". But the strings are actually compared.

like image 65
mbmc Avatar answered Oct 09 '22 09:10

mbmc


As per @mbmc answers, the error message is not too important in this scenario. The test is failing as you're expecting to get 45 but your textview actually has the value of 45.00. If you make your test pass it should get the text from the textview correctly.

like image 28
Francislainy Campos Avatar answered Oct 09 '22 10:10

Francislainy Campos