Espressoでこの機能テストをAndroidで動作させるのに苦労しています。私のアプリはMultdexアプリなので、次のリンクに記載されている手順に従います:( https://developer.Android.com/tools/building/multidex.html )。
私はすでにこのようにbuild.gradleを設定しています:
apply plugin: 'com.Android.application'
apply plugin: 'Android-apt'
def AAVersion = '3.3.2'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:Android-apt:1.4'
}
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'br.com.foo'
}
}
Android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "br.com.foo"
minSdkVersion 15
targetSdkVersion 21
Android.enforceUniquePackageName = false
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
testApplicationId "br.com.foo.test"
testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "com.Android.test.runner.MultiDexTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.txt'
}
}
dexOptions {
preDexLibraries = false
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
configurations.all {
resolutionStrategy.force 'com.Android.support:support-annotations:22.1.0'}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile 'com.github.PhilJay:MPAndroidChart:v2.1.0'
compile 'com.github.daimajia:AndroidViewAnimations:v1.1.3'
compile 'com.makeramen:roundedimageview:2.1.1'
compile 'com.github.ACRA:acra:acra-4.6.2'
compile 'com.github.rahatarmanahmed:circularprogressview:2.3.2'
compile 'com.github.jhy:jsoup:jsoup-1.8.3'
compile project(':cacscore')
compile project(':croplib')
compile project(':zxingandroid')
compile project(':paho')
compile project(':urlImageViewHelper')
compile project(':facebookSDK')
compile project(':googleDateTimePickers')
compile 'com.Android.support:support-v4:22.0.0'
compile 'com.Android.support:appcompat-v7:22.0.0'
compile 'com.google.Android.gms:play-services:7.8.0'
compile 'com.Android.support:multidex:1.0.0'
compile 'org.Apache.commons:commons-lang3:3.4'
compile files('libs/httpmime-4.1.jar')
compile files('libs/jackson-all-1.9.11.jar')
compile files('libs/jaxrs-api-2.2.1.GA.jar')
compile files('libs/spring-Android-core-1.0.1.RELEASE.jar')
compile files('libs/spring-Android-rest-template-1.0.1.RELEASE.jar')
compile 'com.melnykov:floatingactionbutton:1.2.0'
compile files('libs/trace.jar')
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.2'
testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.2'
testCompile 'org.powermock:powermock-module-junit4:1.6.2'
androidTestCompile 'junit:junit:4.12'
// androidTestCompile('com.Android.support.test:testing-support-lib:0.1') {
// exclude group: 'junit' // junit:junit-dep conflicts with junit:unit
// }
androidTestCompile 'com.Android.support.test:runner:0.4'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.Android.support.test:rules:0.4'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.Android.support.test.espresso:espresso-core:2.2.1'
// Set this dependency to build and run UI Automator tests
// androidTestCompile 'com.Android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile('com.Android.support:multidex-instrumentation:1.0.1') {
exclude group: 'com.Android.support', module: 'multidex'
}
}
Android StudioからtestInstrumentationRunnerクラスをcom.Android.test.runner.MultiDexTestRunnerに設定して実行プロファイルを構成しました。
コマンド「adb Shell pm list instrumentation」を実行すると、次の出力が得られます。
instrumentation:br.com.doe.test/com.Android.test.runner.MultiDexTestRunner (target=br.com.doe)
instrumentation:br.com.foo.test/com.Android.test.runner.MultiDexTestRunner (target=br.com.foo)
instrumentation:br.com.foo/com.Android.test.runner.MultiDexTestRunner (target=br.com.foo)
それでも、テストを実行しようとすると、この出力が表示されます。
Testing started at 17:23 ...
Installing br.com.foo
DEVICE Shell COMMAND: pm install -r "/data/local/tmp/br.com.foo"
pkg: /data/local/tmp/br.com.foo
Success
Uploading file
local path: F:\Android\CACS\cacs\cacs\build\outputs\apk\cacs-debug-androidTest-unaligned.apk
remote path: /data/local/tmp/br.com.foo.test
Installing br.com.foo.test
DEVICE Shell COMMAND: pm install -r "/data/local/tmp/br.com.foo.test"
pkg: /data/local/tmp/br.com.foo.test
Success
Running tests
Test running started
Java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at Android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.Java:45)
at br.com.bagger.inicio.MapaEnderecoTest.setUp(MapaEnderecoTest.Java:35)
at Android.test.AndroidTestRunner.runTest(AndroidTestRunner.Java:191)
at Android.test.AndroidTestRunner.runTest(AndroidTestRunner.Java:176)
at Android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.Java:554)
at Android.app.Instrumentation$InstrumentationThread.run(Instrumentation.Java:1701)
Finish
そして、これは私が実行しようとしているユニークなテストです:
package br.com.doe.inicio;
import Android.support.test.InstrumentationRegistry;
import Android.support.test.espresso.Espresso;
import Android.support.test.espresso.action.ViewActions;
import Android.support.test.espresso.matcher.ViewMatchers;
import Android.support.test.runner.AndroidJUnit4;
import Android.test.ActivityInstrumentationTestCase2;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import br.com.foo.R;
@RunWith(AndroidJUnit4.class)
public class MapaEnderecoTest extends ActivityInstrumentationTestCase2<MapaEndereco>
{
private MapaEndereco mActivity;
public MapaEnderecoTest()
{
super(MapaEndereco.class);
}
@Override
@Before
protected void setUp() throws Exception
{
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}
@Test
public void testeAbertura()
{
Espresso.onView(ViewMatchers.withId(R.id.map)).perform(ViewActions.click());
}
}
エラーは、setUpメソッドでsuper.setUp()呼び出しの直後に発生します。
AndroidManifestファイルにインストルメンテーションを登録しています:
<instrumentation
Android:label="Multdex"
Android:name="com.Android.test.runner.MultiDexTestRunner"
Android:targetPackage="br.com.foo"
Android:functionalTest="true"/>
そして、私が直面している問題は、ここで説明したものと同じではありません Espressoを使用する場合のNullPointer例外
問題はこれらのgradle行にあります:
testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "com.Android.test.runner.MultiDexTestRunner"
MultiDexTestRunner
が必要ですが、このクラスはInstrumentationTestRunner
から派生しています。コードには、AndroidJUnitRunner
から派生したテストランナーが必要です
解決策は、TestRunner
を拡張し、MultiDexコードを実装するカスタムAndroidJUnitRunner
を作成することです。
import Android.os.Bundle;
import Android.support.multidex.MultiDex;
import Android.support.test.runner.AndroidJUnitRunner;
public class TestRunner extends AndroidJUnitRunner
{
@Override
public void onCreate(Bundle arguments)
{
MultiDex.install(getTargetContext());
super.onCreate(arguments);
}
}
カスタムtestInstrumentationRunner
を参照するGradle行を1つ追加するだけです。これにより、ビルドシステムがAndroidManifest.xml
このランナーでテストを実行するために必要なプロパティ。
私は同様のエラーがあり、それを修正するのに苦労していました。受け入れられた答えはOPを助けましたが、それは私の問題を解決しませんでしたので、ここで自分の解決策を記録することは良いことであると判断しました。
私の問題は、AndroidJUnit4
、InstrumentationRegistry
、ApplicationProvider
、およびAndroidJUnitRunner
versions /パッケージを混合していたことです。それらがすべて同じ世代であることを確認してください。これらはすべて私のために実行させたクラスです:
androidx.test.runner.AndroidJUnitRunner
androidx.test.platform.app.InstrumentationRegistry
androidx.test.ext.junit.runners.AndroidJUnit4
androidx.test.core.app.ApplicationProvider
これらのために、build.gradleのdependencies
部分に次のものが必要でした。
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
そしてもちろん正しい
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
build.gradle
のdefaultConfig
で
の代わりに:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
別のライブラリを使用する必要がありました。
androidTestImplementation 'com.Android.support.test.espresso:espresso-core:3.0.2'