2015-08-24 44 views
16

我試圖在新的Android項目中創建Espresso UI測試,但遇到以下問題。Android Espresso:無法解析符號AndroidJUnit4.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> { 

} 

我總是收到此錯誤信息:

cannot resolve symbol AndroidJUnit4.class 

而且幾乎所有的進口庫標記爲未使用。

的build.gradle文件包含以下內容:

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

如果我把這些設置我的其他測試項目它的作品,所以我不知道什麼可以是錯的?

我已經按照這個教程:」

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

而且我已經嘗試通過以下SO問題來解決它: Cannot resolve symbol 'AndroidJUnit4'

但是,如果沒有運氣

許多感謝您的任何意見。

+0

的Android { testBuildType「調試」 }爲我工作就像一個魅力。 – suprith

回答

4

我解決了它使用改變常數

minSdkVersion 

的build.gradle文件版本18。

以下gradle.file工作:

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

改變sdk版本也爲我解決了它,但有趣的是,它曾經工作過一次,我把它設置回19,它仍然有效! – bluehallu

+0

這可以工作,但我不強烈推薦此解決方案。例如,我掙扎着乞丐,而通過這種解決方案,我將失去很大一部分市場份額。 :/ –

14

我試着從vogella相同教程太跑進許多問題。我碰到的第一個問題是v23庫的註釋版本和Espresso庫之間的依賴衝突。

然後我發現另一個Roger Hu「UI Testting with Espresso」最近更新的教程。我注意到Espresso還沒有支持棉花糖。

的依賴中添加如下:

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

這解決了我的依賴性衝突,我沒有看到任何發生問題的休息。

+0

你是一個拯救生命的人。爲了讓別人清楚,只需使用上面的依賴關係即可。沒有其他的。否則,你仍然會得到依賴衝突。 –

4

按所給的上述變化的gradle:

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

您需要更改爲

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

,對我來說甚至沒有與上述變化的工作,所以我注意到的是我錯過了下列內容:

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

它對我來說工作得很好。

完整的build.gradle可以如下發現:

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:[email protected]' 
    compile 'com.google.android.gms:play-services-analytics:8.1.0' 

    releaseCompile 'co.test.dialersdk:dialersdk:[email protected]'; 
    debugCompile 'co.test.dialersdk:dialersdk-debug:[email protected]';  
    internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:[email protected]'; 

    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' 


} 

希望這肯定會幫助別人,因爲我一直在努力了半天甚至以下vogella教程的步驟完成後進行修復。

7

我解決它通過手動導入下面,我認爲應該把它自動導入但事與願違:

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; 
1

您可以參考這個answer

「我犯了錯誤,把測試類放在src/test,把它們移動到src/androidTest/java/...之後,依賴已解決,也許這也是你的問題。

2

我有同樣的問題,我解決了改變我的構建變種。我在發佈版本中運行測試。

When I changed to "debug", it works

+0

哦哇我只花了一個小時試圖解決我的依賴關係,什麼不是!在這種情況下,Intellij應該給予更好的反饋,而不是「找不到類」。 –

3

你會得到錯誤信息可能是因爲在測試你的居住文件夾不符合規範的原因。該文件夾必須是src/androidTest/java

Take a look at this article它說...

潤儀表單元測試來運行你的儀表測試,請按照 下列步驟操作:

確保您的項目可以通過點擊同步 與項目同步搖籃在工具欄中。以下列方式之一運行測試: 要運行單個測試,請打開「項目」窗口,然後右鍵單擊 測試,然後單擊運行。要測試類中的所有方法,請右鍵單擊測試文件中的 類或方法,然後單擊運行。要在 目錄中運行所有測試,請右鍵單擊該目錄並選擇運行測試。 Gradle Android插件編譯位於默認目錄(src/androidTest/java /)中的位於 的測試代碼,構建測試APK 和生產APK,在連接的設備或 模擬器上安裝這兩個APK,並運行測試。 Android Studio然後在「運行」窗口中顯示儀器化測試執行的結果 。

因此鄉親,爲儀器測試的文件夾必須是(不要忘記的情況下)

的src/androidTest/JAVA

本地測試的文件夾必須be

src/test/jav一個

然後你可以有你的包文件夾(一個或多個)以滿足您的應用程序包

希望,這有助於爲社會!

+0

儘管如此,在其他情況下也會發生此錯誤,例如測試/產品相關性的衝突版本控制。 –

+0

始終是文件夾androidTest!謝謝! – rubdottocom

0

也許你可能有多個生成類型,默認的Android項目創建兩個構建類型(調試/釋放),改變構建變量調試或設置值,如下

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" 
} 
+0

testBuildType「調試」爲我做了竅門。 – suprith