2017-10-06 13 views
2

ユーザーがボタンをクリックしてTextViewが空の場合、テストが失敗するようにしようとしています。エスプレッソでこれを行う方法があるので、TextViewが空の場合は失敗します。 TextViewに特定のテキストが含まれているかどうかチェックしたくないので、空でないことを確認したい。エスプレッソを使用してTextViewの値が空ではなく、TextViewの値が空の場合はエラーが発生します。

答えて

1

これを試してみてください:TextViewには任意のテキストが含まれている場合

import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.assertion.ViewAssertions.matches; 
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 
import static android.support.test.espresso.matcher.ViewMatchers.withText; 
import static org.hamcrest.Matchers.not; 

@RunWith(AndroidJUnit4.class) 
public class MainActivityInstrumentedTest { 

    @Rule 
    public ActivityTestRule<MainActivity> mActivityTestRule = 
      new ActivityTestRule<>(MainActivity.class); 

    @Test 
    public void checkTextView_isDisplayed_and_notEmpty() throws Exception { 
     // perform a click on the button 
     onView(withId(R.id.button)).perform(click()); 

     // passes if the textView does not match the empty string 
     onView(withId(R.id.textView)).check(matches(not(withText("")))); 
    } 
} 

テストのみを通過します。

+0

上記のコードは、textViewが空の場合にテストに合格します。 textViewが空のときにテストを失敗させたい。私はそれが空のときにそれを渡すことを望んでいない。 –

+0

私は自分の答えを更新しました。もう一度やり直してみてください。 – chornge

+1

上記の編集済みコードが有効です。 –

関連する問題