8

Tôi đang cố gắng thực hiện bài kiểm tra funcional này với công việc Espresso trên Android. Ứng dụng của tôi là một ứng dụng Multdex nên tôi đang làm theo hướng dẫn được viết tại liên kết này (https://developer.android.com/tools/building/multidex.html).Android IllegalStateException Không có thiết bị nào được đăng ký! Phải chạy theo công cụ đăng ký

Tôi đã cấu hình build.gradle của tôi như thế này:

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' 
     } 
    } 

tôi cấu hình hồ sơ cá nhân Run từ Android Studio thiết lập các classe testInstrumentationRunner để com.android.test.runner.MultiDexTestRunner.

Khi tôi thực hiện lệnh "adb shell pm danh sách thiết bị đo đạc" Tôi có đầu ra này:

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) 

Nhưng tôi vẫn nhận ra điều này khi tôi cố gắng để thực hiện các bài kiểm tra của tôi:

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 

Và đây là thử nghiệm duy nhất mà tôi đang cố gắng chạy:

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()); 
    } 
} 

Lỗi xảy ra ngay sau lệnh gọi super.setUp(), trên phương thức thiết lập.

Tôi có thiết bị đo đạc đăng ký vào file AndroidManifest:

<instrumentation 
     android:label="Multdex" 
     android:name="com.android.test.runner.MultiDexTestRunner" 
     android:targetPackage="br.com.foo" 
     android:functionalTest="true"/> 

Và vấn đề tôi phải đối mặt là không giống như mô tả ở đây NullPointer Exception when using Espresso

+0

Tôi đang đối mặt với cùng một vấn đề. Bạn đã tìm ra một giải pháp chưa? –

+0

bạn đang sử dụng phiên bản plugin android nào? –

+0

Tôi đang sử dụng plugin này: classpath 'com.android.tools.build:gradle:1.3.0' @RicardoBelchior –

Trả lời

9

Vấn đề là với những dòng gradle:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner" 

Bạn cần MultiDexTestRunner, nhưng lớp này có nguồn gốc từ InstrumentationTestRunner. Mã của bạn cần có một Á hậu kiểm tra đó có nguồn gốc từ AndroidJUnitRunner

Một giải pháp là tạo ra một tùy chỉnh TestRunner kéo dài AndroidJUnitRunner và thực hiện mã MultiDex:

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); 
    } 
} 

Bạn chỉ cần thêm một dòng Gradle duy nhất mà tài liệu tham khảo tùy chỉnh của bạn testInstrumentationRunner. Điều này sẽ đảm bảo rằng hệ thống xây dựng tạo ra các thuộc tính AndroidManifest.xml cần thiết để chạy thử nghiệm với nhân vật này.

+0

Bạn vẫn cần phải có cả testInstrumetationRunner? và cũng là khai báo trong manifest.xml? – Anthony

+0

Không, bạn chỉ cần TestRunner tùy chỉnh. Tôi chỉ tuyên bố nó trong build.gradle của tôi. Khi xây dựng, tệp kê khai được tạo trong 'Application/build/intermediates/manifest/androidTest/debug /' với khai báo trong đó. Bộ công cụ Gradle sẽ quan tâm đến điều đó. –

+0

Không cần thiết phải tạo một TestRunner tùy chỉnh –

Các vấn đề liên quan