Espresso NoMatchingViewException

时间:2016-05-23 17:33:36

标签: android gradle android-espresso

我正在尝试创建我的第一个Espresso测试类并且遇到问题。 我一直在阅读官方samples 并做类比课。

package name.xunicorn.iocipherbrowserext.app;



import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import name.xunicorn.iocipherbrowserext.R;
import name.xunicorn.iocipherbrowserext.activities.MainActivity;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;

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;

@RunWith(AndroidJUnit4.class)
public class DefaultFragmentTest {
    public final static String TAG = "DefaultFragmentTest";

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

    @Test
    public void defaultFragment_isLoaded() {
        onView(withId(R.id.btnPlug)).check(matches(isDisplayed()));
    }

    @Test
    public void defaultFragment_plugButtonClick() {
        onView(withId(R.id.btnPlug)).check(matches(isDisplayed()));

    }
}

在主要活动中,我正在创建片段并将其添加到FrameLayout。我无法理解的是,为什么这个测试案例中的一个总是通过而另一个抛出NoMatchingViewException?有人可以解释一下吗?

我的傻瓜:

defaultConfig {
    applicationId 'name.xunicorn.iocipherbrowserext'
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 1
    versionName "0.0.5"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'

    compile 'info.guardianproject.iocipher:IOCipherStandalone:0.3'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.jaredrummler:android-device-names:1.0.8'
    compile "com.google.code.gson:gson:2.3.1"

    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Optional -- Hamcrest library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    // Optional -- UI testing with Espresso
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'

}

0 个答案:

没有答案