java - 意式 Espresso 。 NoSuchMethodError UsageTracker

标签 java android testing gradle android-espresso

我正在编写一个启动 Intent 的 Espresso 测试,但我收到以下错误:

      java.lang.NoSuchMethodError: No interface methodtrackUsage(Ljava/lang/String;)V in class Landroid/support/test/internal/runner/tracker/UsageTracker; or its super classes (declaration of 'android.support.test.internal.runner.tracker.UsageTracker' appears in /data/app/com.app.test-2/base.apk:classes3.dex)
at android.support.test.espresso.intent.Intents.<clinit>(Intents.java:93)
at com.app.features.main.MainActivityTest.when_logout_button_is_clicked_then_view_is_not_visible(MainActivityTest.java:70)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1970)

这是我的 gradle 文件的样子:

ext {
    support_lib_version = '25.3.1'
    play_services_lib_version = "11.0.4"
    okhttp_version = '3.4.1'
}

configurations.all {
resolutionStrategy {
    force "com.android.support:support-annotations:$support_lib_version"
    force "com.android.support:support-v4:$support_lib_version"
    force "com.android.support:design:$support_lib_version"
    force "com.android.support:appcompat-v7:$support_lib_version"
    force "com.squareup.okhttp3:okhttp:$okhttp_version"
}


//Testing
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.2.0'
androidTestCompile 'com.github.andrzejchm.RESTMock:android:0.2.2'
androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

测试代码(应该不相关但仍然...)

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void when_logout_button_is_clicked_then_view_is_not_visible() {
    Intents.init();
    openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
    onView(withText("Logout")).perform(click());
    intended(hasComponent(LoginSignUpActivity.class.getName()));
    Intents.release();
}

万一它可能是相关的,我没有使用 proguard。关于如何解决这个问题的任何想法?它看起来绝对像是一个依赖关系问题。

最佳答案

解决方案,以防对任何人有帮助:

androidTestCompile ('com.android.support.test.espresso:espresso-intents:3.0.1'

代替

androidTestCompile ('com.android.support.test.espresso:espresso-intents:2.2.2'

不敢相信我之前没有想到这一点......

关于java - 意式 Espresso 。 NoSuchMethodError UsageTracker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46873885/

相关文章:

Java 在我的 Java 代码中出现 .setText 错误

java - 如何在 Android 中使用 kotlin 枚举修复 'java.lang.InstantiationError'

unit-testing - 通过排序测试集合相等性

unit-testing - ngxs:如何测试动态选择器

java - 提取隐藏在 DIV id 下的值

java - 在java中实现双端队列的问题

JavaFX : How to fill a ComboBox with changing values and refrech it

android - 关闭新 Activity 的 Activity

java - AES加密后的Hex.encodeHexString?

testing - Groovy 断言脚本在 SoapUI 中执行两次