Android

AndroidX Junit5 Instrumentation

2020-09-29  本文已影响0人  JaedenKil

This article is about how to build an androidx + JUnit 5 instrumentation test.

Steps:

  1. Update project-level build.gradle, add the following line:
classpath 'de.mannodermaus.gradle.plugins:android-junit5:1.6.0.0'
  1. Update com.android.tools.build:gradle to a newer version:
classpath "com.android.tools.build:gradle:4.0.1"
  1. Update gradle to a newer version:
// gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip
  1. Update module-level build.gradle:
// app/build.gradle
apply plugin: 'de.mannodermaus.android-junit5'
  1. Update module-level build.gradle instrumentation runner & argument:
// app/build.gradle
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArgument "runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder"
  1. Update module-level android block:
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE.md'
        exclude 'META-INF/LICENSE-notice.md'
        exclude("META-INF/*.kotlin_module")
    }
  1. Update dependencies:
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation 'androidx.appcompat:appcompat:1.2.0'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator-v18:2.2.0-alpha1'
    androidTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    androidTestImplementation 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
    androidTestImplementation 'org.junit.platform:junit-platform-runner:1.6.2'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test:rules:1.3.0'
    androidTestImplementation 'androidx.test:monitor:1.3.0'
    androidTestImplementation 'androidx.annotation:annotation:1.1.0'
    androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.0"
}
  1. In AndroidTest folder, declare UiDevice & other necessary depdendencies:
package com.jk.androidjunit5instrumenttestdemo;

import android.app.UiAutomation;
import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;

import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@SuppressWarnings("unused")
class InstrumentationTestDemo {

    // Possible needed components
    private static UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    private static UiAutomation mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
    private static Context ctx = InstrumentationRegistry.getInstrumentation().getTargetContext();

    @BeforeAll
    static void setUpBeforeClass() {

    }

    @AfterAll
    static void tearDownAfterClass() {

    }

    @BeforeEach
    void setUp() {

    }

    @AfterEach
    void tearDown() {

    }

    @Test
    void testEqualDemo() {
        Assert.assertEquals("Demo test for integer add", 10, 2 + 8);
    }

}
上一篇 下一篇

猜你喜欢

热点阅读