安卓 - Espresso中的未解决引用:all方法

5

我在使用Espresso时遇到了很多错误,感到非常困惑。

这是我的导入文件:

import android.os.SystemClock
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import org.junit.runner.RunWith
import android.support.test.espresso.Espresso
import android.support.test.espresso.action.ViewActions
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.*
import org.junit.Rule
import org.junit.Test
import org.junit.Before
import sayurbox.com.oms.view.LoginActivity
import android.support.test.espresso.intent.Intents.intended
import android.support.test.espresso.intent.Intents
import android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent
import sayurbox.com.oms.view.HomeActivity

我已经添加了所有的依赖项:

testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:4.0.2'
    testImplementation "org.robolectric:shadows-multidex:4.0.2"
    testImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    testImplementation 'com.android.support.test:runner:1.0.2'
    testImplementation 'com.android.support.test:rules:1.0.2'
    testImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'

这是错误信息:

Unresolved reference: test
Unresolved reference: junit
Unresolved reference: AndroidJUnit4
Unresolved reference: Test
Unresolved reference: assertEquals
Unresolved reference: InstrumentationRegistry
...

所有的引用都未解决。你们能帮帮我吗?

implementationtestImplementation 有什么区别?那么你的 Java 文件放在哪里了? - Geno Chen
应用程序 -> src -> androidTest -> java -> mypackagename.com.oms -> 这里 - Galih laras prakoso
1个回答

12

我找到了解决方案,我认为是因为我将我的测试放在了默认的Instrumentation测试目录中,所以我需要将我的Gradle'testImplementation'语法更改为'androidTestImplementation':

更改前:

testImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
testImplementation 'com.android.support.test:runner:1.0.2'
testImplementation 'com.android.support.test:rules:1.0.2'
testImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'

修复后:

androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接