mercredi 2 septembre 2015

Instrumented Tests in Android Studio

I have been trying to run simple instrumented tests in Android Studio, without any success. I followed the guide for Unit Tests and it works fine. Now, I would like to tests components which are difficult to mock. When I follow the guide on instrumented tests, I end up with dependency errors.

What I did:

  • Install the Android Testing Support Library (Android Support Repository, rev 17).

  • Create a directory androidTest in src. Add Java folder + package ending with "test".

  • Add a simple Test class
 @RunWith(AndroidJUnit4.class)
    public class ServerRequestInstrumentationTest {
        @Test
        public void test(){
           LogC.d("Here we go testing !");
        }
    }

  • Add a simple TestSuit.
@RunWith(Suite.class)
@Suite.SuiteClasses({ServerRequestInstrumentationTest.class})
public class TestSuit {

    public TestSuit(){}
}

  • Modify my gradle file to add dependencies.
apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.mydomain.app"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 28
        versionName "2.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            lintOptions {
                disable 'MissingTranslation'
            }
        }
    }
    sourceSets { main { java.srcDirs = ['src/main/java'] } }
}

dependencies {
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    wearApp project(':wear')
    compile project(':moreapps')
    compile project(':lib_repair')
    compile project(':bugreport')
    //testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test:rules:0.3'
}

I just added the line androidTestCompile 'junit:junit:4.12', otherwise the annotation @RunWith(AndroidJUnit4.class) wasn't recognized. Now when I launch the test, I get one Gradle build error.

Error:Execution failed for task ':flashlight:dexDebugAndroidTest'.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/jdkoracle/bin/java'' finished with non-zero exit value 2

Any suggestion ? Did I miss something about the testing library ?

Thank you

Aucun commentaire:

Enregistrer un commentaire