【问题标题】:Can't start Espresso test with mockwebserver: Could not resolve com.squareup.okhttp3:okhttp无法使用 mockwebserver 开始 Espresso 测试:无法解析 com.squareup.okhttp3:okhttp
【发布时间】:2019-09-10 21:50:36
【问题描述】:

Android Studio 3.3

在 build.gradle 中:

buildscript {
    ext.KOTLIN_VERSION = '1.3.21'
    ext.ESPRESSO_VERSION = '3.2.0-alpha02'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

在我的 app/build.gradle 中:

android {
    dataBinding {
        enabled = true
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myproject.android"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 6
        versionName "0.0.8"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
        //androidTest.assets.srcDirs += 'src/androidTest/assets'
    }

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }

    implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03'
    implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha03'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.yuyh.json:jsonviewer:1.0.6'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"

    implementation project(':common')

    androidTestImplementation "androidx.test.espresso:espresso-core:$ESPRESSO_VERSION"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$ESPRESSO_VERSION"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$ESPRESSO_VERSION"
    androidTestImplementation 'androidx.test:rules:1.1.2-alpha02'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
    androidTestImplementation "com.squareup.okhttp3:mockwebserver:3.14.1"

    testImplementation 'junit:junit:4.12'
}

这里是 Espresso 的测试:

@RunWith(AndroidJUnit4::class)
class TradersActivityTest {
     val context = InstrumentationRegistry.getInstrumentation().getContext()
    val targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext()
    var listItemCount = 0
    var checkItemCount = 0;
    val mockServer = MockWebServer()

    @Rule
    @JvmField
    var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java)

    @Before
    fun setup() {
        mockServer.start()
        val recyclerView = tradersIntentTestRule.activity.findViewById<View>(R.id.tradersRecyclerView) as RecyclerView
        listItemCount = recyclerView.adapter!!.itemCount
        checkItemCount = (0..listItemCount - 1).random()
    }

但我得到错误:

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:preDebugAndroidTestBuild'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
   > Could not resolve com.squareup.okhttp3:okhttp:{strictly 3.12.0}.
     Required by:
         project :app
      > Cannot find a version of 'com.squareup.okhttp3:okhttp' that satisfies the version constraints: 
           Dependency path 'TM:app:unspecified' --> 'com.squareup.okhttp3:mockwebserver:3.14.1' --> 'com.squareup.okhttp3:okhttp:3.14.1'
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0
           Constraint path 'TM:app:unspecified' --> 'com.squareup.okhttp3:okhttp:{strictly 3.12.0}' because of the following reason: debugRuntimeClasspath uses version 3.12.0

【问题讨论】:

    标签: android android-espresso mockwebserver


    【解决方案1】:

    发生这种情况是因为您没有在应用程序中指定特定的 OkHttp 依赖项。如果您自己没有指定一个,那么 gradle 将使用 Retrofit 指定的默认版本。如果您检查Retrofit POM file,您可以看到它指定了3.12.0,这是您在错误中看到的这个版本的来源。

    解决方案是为你要使用的 OkHttp 版本添加特定的 gradle 依赖:

    implementation 'com.squareup.okhttp3:okhttp:3.14.1'

    然后将使用此版本而不是默认的改造版本,并符合您的模拟服务器版本的要求

    【讨论】:

    • 现在出现另一个错误:任务 ':app:mergeExtDexDebug' 执行失败。 > 无法解析配置“:app:debugRuntimeClasspath”的所有文件。 > 无法转换工件 'okhttp.jar (com.squareup.okhttp3:okhttp:3.14.1)' 以匹配属性 {artifactType=android-dex, dexing-is-debuggable=true, dexing-min-sdk=18, org .gradle.usage=java-runtime-jars} > DexingTransform 执行失败:C:\Users\Alexei\.gradle\caches\transforms-2\files-2.1\f941c32b9727fea784aedf1d9d9f5a1f\jetified-okhttp-3.14.1.jar。 > dexing 时出错。
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2016-04-21
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多