Java 在<;包装>;用浓缩咖啡测试时

Java 在<;包装>;用浓缩咖啡测试时,java,android,junit,android-espresso,Java,Android,Junit,Android Espresso,我想在Android应用程序中测试我的main活动。因此,我创建了第一个测试按钮功能的测试用例。如果用户单击此特定按钮,将打开一个新活动 这是我的密码: @RunWith(AndroidJUnit4.class) public class MainActivityTest { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainAct

我想在Android应用程序中测试我的
main活动。因此,我创建了第一个测试按钮功能的测试用例。如果用户单击此特定按钮,将打开一个新活动

这是我的密码:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

    @Test
    public void startNewActivityTest() {
        Intents.init();
        onView(withId(R.id.main_new)).perform(click());
        intended(hasComponent(NewActivity.class.getName()));
        Intents.release();
    }

    @Before
    public void setup() {
        closeSoftKeyboard();
    }

}
这是我的Gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "23.0.1"

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        applicationId "package"
        minSdkVersion 14
        targetSdkVersion 19
    }

    signingConfigs {
        debug {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.android.gms:play-services:4.2.+'
    androidTestCompile 'com.android.support:support-annotations:19.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Set this dependency if you want to use Hamcrest matching
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}

在同一build.gradle文件中添加以下行: android.defaultConfig:testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”

我从未用过意式浓缩咖啡,但也许你需要:

使用时请使用IntentsTestRule而不是ActivityTestRule 意式浓缩咖啡。IntentsTestRule使其易于使用 Espresso在功能UI测试中使用API。这门课是一门课程 ActivityTestRule的扩展,用于初始化意式浓缩咖啡 每次测试前,用@test标注并释放浓缩咖啡意向 每次测试运行后。每次测试后,活动将终止 此规则的使用方式与ActivityTestRule相同


如果您在2020年丢失了,您可能需要使用这个较新的AndroidX(jetpack)测试跑步器

android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

你能发布一些关于你的环境的更多信息吗(比如Gradle版本、Gradle文件等)?你考虑过使用吗?也许吧
apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

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

dependencies {
    // App's dependencies, including test
    compile 'com.android.support:support-annotations:23.0.1'

    ...
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}