Android Espresso : cannot resolve symbol AndroidJU

2020-05-22 02:04发布

问题:

I'm trying to create Espresso UI test inside the new Android project but I faced with the following problem.

If I tried to create a empty test class:

import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {

}

I always get this error message:

cannot resolve symbol AndroidJUnit4.class

And almost all imported libraries are marked as unused.

build.gradle file is containing the following:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.some.thing.xxx"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url "https://jitpack.io" }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'com.mcxiaoke.volley:library:1.0.18'
    compile 'com.orhanobut:logger:1.11'
    // App dependencies
    compile 'com.android.support:support-annotations:23.0.0'
    // TESTING DEPENDENCIES
    androidTestCompile 'com.android.support.test:runner:0.3'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.3'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    // add this for intent mocking support
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
    // add this for webview testing support
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}

If I put these setting on my other test project it works, so I don't know what can be wrong?

I've followed this tutorial:"

http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html

And I've tried to resolve it by following SO question: Cannot resolve symbol 'AndroidJUnit4'

But without the luck.

Many thanks for any advice.

回答1:

I've tried the same tutorial from vogella too and ran into many issues. One of the first issues I ran into was a dependency clash between the annotation versions of v23 libs and the Espresso libs.

Then I found another recently updated tutorial from Roger Hu "UI Testting with Espresso". I noticed a remark that Espresso is not supporting Marshmallow yet.

The dependencies were added as follows:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
    // Necessary if your app targets Marshmallow (since the test runner
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}

This solved my dependency conflict and I didn't see any of the rest of the issues occurring.



回答2:

I solved it by manually importing the following, I thought it should be imported automatically but it didn't :

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


回答3:

The reason you get that error message may be because your folder where the test is residing is not matching the spec. The folder must be src/androidTest/java.

Take a look at this article which says...

Run Instrumented Unit Tests To run your instrumented tests, follow these steps:

Be sure your project is synchronized with Gradle by clicking Sync Project in the toolbar. Run your test in one of the following ways: To run a single test, open the Project window, and then right-click a test and click Run . To test all methods in a class, right-click a class or method in the test file and click Run . To run all tests in a directory, right-click on the directory and select Run tests . The Android Plugin for Gradle compiles the instrumented test code located in the default directory (src/androidTest/java/), builds a test APK and production APK, installs both APKs on the connected device or emulator, and runs the tests. Android Studio then displays the results of the instrumented test execution in the Run window.

Therefore folks, for instrumentation test the folder must be (do not forget the case)

src/androidTest/java

and for local tests the folder must be

src/test/java

You can then have your package folder(s) to match your app package

Hope, this helps for the community!



回答4:

I solved it using change the constant

minSdkVersion 

to version 18 in the build.gradle file.

Following gradle.file is working:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.something.xxx"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url "https://jitpack.io" }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'com.mcxiaoke.volley:library:1.0.18'
    compile 'com.orhanobut:logger:1.11'

    // TESTING DEPENDENCIES
    androidTestCompile 'com.android.support:support-annotations:23.0.0'
    androidTestCompile 'com.android.support.test:runner:0.3'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.3'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    // add this for intent mocking support
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
    // add this for webview testing support
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}


回答5:

As per the above gradle changes given:

androidTestCompile 'com.android.support.test:runner:0.3'

you need to change to

androidTestCompile('com.android.support.test:runner:0.3') {
    exclude group: 'com.android.support', module: 'support-annotations'
}

and for me it wasn't working even with above change, so what I noticed was I was missing the below inclusion:

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

and it worked fine for me.

The complete build.gradle can be found as below:

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 23
    buildToolsVersion "21.1.2"

    lintOptions {
        // IMPORTANT: We are disabling this rule to avoid build errors on PrettyTime. Although
        //pretty time references an InvalidPackage it does not do it in the code sections we use
        //given how easy this library is to use I would prefer not to replace it with something
        //like Joda-Time which is overkill for such a small section of the app.
        disable 'InvalidPackage'
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }

    defaultConfig {
        applicationId "co.test.dialer"
        minSdkVersion 18
        targetSdkVersion 22
        versionCode 15
        versionName "0.6.15."
        renderscriptTargetApi 22
        renderscriptSupportModeEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        production {
            storeFile file("keystore.jks")
            storePassword "hello"
            keyAlias "production"
            keyPassword "android"
        }

        debug {
            storeFile file("keystore.jks")
            storePassword "hello"
            keyAlias "debug"
            keyPassword "android"
        }

    }

    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.production
        }

        debug {
            minifyEnabled false
            debuggable true
            applicationIdSuffix ".debug"
            signingConfig signingConfigs.debug
        }

        internal_test {
            minifyEnabled false
            debuggable true
            applicationIdSuffix ".test"
            signingConfig signingConfigs.debug
        }
    }
}

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:support-v13:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.afollestad:material-dialogs:0.7.8.0'
    compile 'com.googlecode.libphonenumber:libphonenumber:3.1'
    compile 'com.mcxiaoke.volley:library:1.0.15'
    compile 'squizbit.com.jsonobjectified:jetjson:1.0.3@aar'
    compile 'com.google.android.gms:play-services-analytics:8.1.0'

    releaseCompile 'co.test.dialersdk:dialersdk:1.0.8@aar';
    debugCompile 'co.test.dialersdk:dialersdk-debug:1.0.8@aar';    
    internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:1.0.8@aar';

    androidTestCompile('com.android.support.test:runner:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:rules:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-web:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }

    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'


}

Hope this will surely help someone as I have struggled for half a day to fix it even after following complete steps of vogella tutorial.



回答6:

Probably you may have multiple Build types, Default Android Project create two build type (debug/release), change build variant to debug or set the value as below

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing

Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:
android {
    ...
    testBuildType "staging"
}


回答7:

I had the same problem and I resolved changing my Build Variant. I was running the test in the release build.

When I changed to "debug", it works



回答8:

You can refer to this answer.

"I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/... the dependency was resolved. Maybe this is your problem too."