【问题标题】:Espresso intents. NoSuchMethodError UsageTracker意式浓缩咖啡的意图。 NoSuchMethodError UsageTracker
【发布时间】:2017-10-22 12:08:32
【问题描述】:

我正在编写一个 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。关于如何解决这个问题的任何想法?这绝对看起来像一个依赖问题。

【问题讨论】:

    标签: java android testing gradle android-espresso


    【解决方案1】:

    解决方案,以防万一它对任何人都有帮助:

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

    而不是

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

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

    【讨论】:

    • 谢谢。它解决了这个问题。但我收到警告“警告:配置 'androidTestCompile' 已过时......它将在 2018 年底被删除”
    • 为避免该警告,只需将 'androidTestCompile' 替换为 'androidTestImplementation'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    相关资源
    最近更新 更多