未解决的浓缩咖啡方法
Posted
技术标签:
【中文标题】未解决的浓缩咖啡方法【英文标题】:Espresso methods unresolved 【发布时间】:2016-04-01 20:32:58 【问题描述】:我在让 Espresso 与 android Studio 一起工作时遇到了一些重大困难。
我有以下测试代码:
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import com.nullpointexecutioners.buzzfilms.activities.WelcomeActivity;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class ApplicationTest
@Rule
public ActivityTestRule<WelcomeActivity> mActivityRule = new ActivityTestRule<>(WelcomeActivity.class);
@Test
public void testTitle()
onView(withText("Buzz Films")).check(matches(isDisplayed()));
不幸的是,onView
、withText
、matches
和 isDisplayed()
都给我错误,说这些方法无法解决。如果我做一堆static import
声明,我可以让它工作——但我不想诉诸于此。 (即我必须写Espresso.onView(ViewMatchers.withText(...)...
)
我想我的build.gradle
文件是正确的——根据我能找到的关于我的问题的信息。
apply plugin: 'com.android.application'
android
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig
applicationId "com.nullpointexecutioners.buzzfilms"
minSdkVersion 23
targetSdkVersion 23
versionCode 7
versionName "2.0" //bump this up per milestone
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled true
debug
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled false
packagingOptions
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
testOptions
unitTests.returnDefaultValues = true
unitTests.all
// All the usual Gradle options.
jvmArgs '-XX:MaxPermSize=256m'
dependencies
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2')
exclude group: 'com.android.support', module: 'support-annotations'
androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2')
exclude group: 'com.android.support', module: 'support-annotations'
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.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.2')
exclude group: 'com.android.support', module: 'support-annotations'
androidTestCompile ('com.android.support.test:runner:0.5')
exclude group: 'com.android.support', module: 'support-annotations'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
compile('com.github.afollestad.material-dialogs:core:0.8.5.5@aar')
transitive = true
compile('com.mikepenz:materialdrawer:4.6.4@aar')
transitive = true
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.mikepenz:iconics-core:2.5.5@aar'
compile 'com.mikepenz:google-material-typeface:2.2.0.1.original@aar'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:palette-v7:23.2.1'
compile 'com.github.florent37:picassopalette:1.0.2@aar'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.github.channguyen:rsv:1.0.1'
【问题讨论】:
你必须使用静态导入。这是标准做法。 @yogurtearl 即使那样它也不起作用。 您能否更新问题以包含静态导入? 现在它似乎可以工作了。您在哪里读过静态导入是标准的?我还没有找到任何关于它的东西。每个教程或文章都可以在没有很多导入的情况下运行。 【参考方案1】:您将需要静态导入。
Google 官方示例代码使用静态导入。静态导入是流畅 API 的标准做法。
Sample Espresso Test
【讨论】:
【参考方案2】:对于 AndroidX:
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.assertion.ViewAssertions.matches
查看下面的软件包,看看还有什么可用的:
https://developer.android.com/reference/androidx/test/espresso/matcher/ViewMatchers
https://developer.android.com/reference/androidx/test/espresso/assertion/ViewAssertions
【讨论】:
以上是关于未解决的浓缩咖啡方法的主要内容,如果未能解决你的问题,请参考以下文章