24

I'm running Android Studio 0.5.0 with Gradle 1.11. I'm trying to install Espresso library from com.jakewharton.espresso:espresso:1.1-r2. For some reason AS couldn't recognize Espresso classes after project synchronization. So every time I try to import import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView; inside androidTest folder files it marks it as invalid.

Here's my build.gradle:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.2'

    defaultConfig {
        minSdkVersion 14

        targetSdkVersion 19
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.squareup.dagger:dagger-compiler:1.2.1'
    compile 'com.squareup.dagger:dagger:1.2.1'

    androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
        exclude group: 'com.squareup.dagger'
    }
}

External libraries:

external libraries

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
midnight
  • 3,420
  • 3
  • 36
  • 58

9 Answers9

28

So this is basically a bug with Android Studio (i'm guessing).

Reference:

  1. Issue raised in adt-dev groups
  2. Actual bug - #66841

Workaround (until the bug is fixed):

Add a duplicate provided dependency in your gradle file like so:

dependencies {
    // ...

    provided 'com.jakewharton.espresso:espresso:1.1-r2'
    androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
        exclude group: 'com.squareup.dagger'
    }
}
KG -
  • 7,130
  • 12
  • 56
  • 72
  • This workaround is really helpful. I use r3 version of the double-espresso and this bug still exists. In addition there is no need to create `androidTest` folder - I created `test/java/...` and it works like a charm ;) – mmBs May 07 '14 at 22:25
  • 1
    Not working well anymore on aar dependencies since gradle android plugin version 1.1. Reason is that it does not allow provided aar dependencies. – Patrick Boos Mar 03 '15 at 15:13
  • Warning:Project app: provided dependencies can only be jars. com.android.support.test.espresso:espresso-core:aar:2.0 is an Android Library. – Korniltsev Anatoly Apr 09 '15 at 12:19
13

This issue was driving me crazy. And seems like it's a known bug in Android Studio. In my case it resolved after I've changed Build Type from Release to Debug for the parent app. Hope this may be helpful for someone

Max Ch
  • 1,247
  • 10
  • 13
11

Espresso 2.0

Recently Espresso 2.0 was released making it now part of the Android Support Library. This was announced on the android dev blog.

Setup Guide

With that they also linked an updated setup guide. There you can find instructions to configure from scratch or to update your existing espresso config for 2.0.

Other Tips

Changes are the above 2 links contain all information you need. If not I have listed some common mistakes below

Upgrade Android Studio to 1.0.*

Start by upgrading your android Studio build. You should be able to get at least 1.0 from the stable builds channels (=default). So just use the menu option Android Studio > Check for updates... .

To get the latest from the latest you can also go into Preferences, search for updates and change the channel to canary channel.

Update Android Support Library to v 11+

Espresso was included in the Support library from version 11 so you have to get at least that version. Check for updates using the Android SDK manager. The Support Library is within the Extras tree at the bottom.

New dependencies and namespace

If upgading from an older espresso release you'll have to update the dependencies and the namespace. For new projects just add these to the dependencies in your build.gradle file.

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

And since the namespace changed you'll have to update all imports:

android.support.test.espresso

Note that it's easier to use static imports. Some commonly used imports as an example:

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;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;

For asserts use hamcrest, again some examples:

import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;

Instrumentation runner

The test runner needs to be configured in both your build.gradle file within defaultConfig and the run configuration used to launch your tests from Android Studio.

defaultConfig {
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

And in your run configuration use this as instrumentation runner (full class name only):

android.support.test.runner.AndroidJUnitRunner

Example test case

And an example test case to finish with. Note that MainActivity is your actvitiy you want to test. The tests themselves are public methods that start with test, like testListGoesOverTheFold in the below example.

@LargeTest
public class HelloWorldEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public HelloWorldEspressoTest() {
            super(MainActivity.class);
        }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testListGoesOverTheFold() {
        onView(withText("Hello world")).check(isDisplayed());
    }
}

For more information on writing tests visit the espresso start guide.

0xMatthewGroves
  • 3,181
  • 3
  • 26
  • 43
hcpl
  • 17,382
  • 7
  • 72
  • 73
4

Six months later this is still an issue and the bug referenced by the original responder has been reopened: https://code.google.com/p/android/issues/detail?id=66841 and given higher priority. I have not yet been able to get Android Studio to recognize Espresso classes and using the "provided" scope for dependencies didn't work to fix the issue for me. (Using AS 0.8.6 and Gradle 0.12.2)

  • interesting. the "provided" dependency definitely should work. it's non-ideal, but works. i suspect you might be facing possibly a different issue. Using offline mode/or something? – KG - Dec 01 '14 at 21:18
  • Try changing the release type to debug as mentioned in one of the answers – Dheeraj Bhaskar Mar 31 '15 at 13:04
3

according to http://tools.android.com/tech-docs/new-build-system/user-guide only one build type is tested, by default it is debug Build Type.

So check that you are using debug build variant and rebuild the application. On other build types all your androidTest dependencies will not be visible.

If you need to test your current build type you can do something like this:

android { ... testBuildType "staging" }

Rostyslav Roshak
  • 3,859
  • 2
  • 35
  • 56
  • Thank you very much ! It was exactly my issue, I do not use debug but my own custom buildTypes and we need to specify which one we want to use. – Kerwan Feb 05 '16 at 15:48
  • 2
    WTF Google??? Really? Why the heck does the NAME matter? Can't you simply pick up the currently selected build type from the IDE. Wasted 2 days of mine to figure out that you should select the build variant with the exact NAME of "debug" in the Android Studio (from the build variant selector) even if you don't have such build type. Setting the "debuggable" flag in a build type with a different name doesn't work. If you have defined build config variables for your build types, you have to create a build type literally named "debug" and redefine all of those vars in the "debug" build type too. – Javad Jul 02 '16 at 19:32
3

Android Studio 1.5.1 didn't realize onView() or onData() methods in my case. I just did a static import of the Espresso class and all it's methods.

I added the below line and everything worked perfectly.

import static android.support.test.espresso.Espresso.*;
Henry
  • 17,490
  • 7
  • 63
  • 98
1

You're not specific about what source file you're seeing the error in, but based on my testing, I think you're trying to access the Espresso classes from one of your main application classes (inside src/main/java/). If so, that won't work, because you've included Espresso via an androidTestCompile dependency include, which makes it accessible only to your test classes, which must be under src/androidTest/java.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • 1
    I'm sry not to mention it but I actually tried accessing it inside of the androidTest folder classes – midnight Mar 11 '14 at 06:56
1

I've tried every solution what guys guessed above and still got the class not found error.

I figured out my solution, it saved my day. So if you open the project tab on left to your project folders in Studio and check the build variants you can see that, your project is set to unit test. You have to re-set it to Android Instrumentation Tests and make sure your test.java file is under **src\androidTest\java**

narancs
  • 5,234
  • 4
  • 41
  • 60
1

try these dependencies:

implementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

I was having this issue and I went through and added every espresso library I could add to my gradle file. Wait for the yellow lightbulb to come on and then click add library and search for the espresso libraries you don't have. I don't know if this will work for everyone but it worked for me.