Alexei
Alexei

Reputation: 15716

Can't start Espresso test with mockwebserver: Could not resolve com.squareup.okhttp3:okhttp

Android Studio 3.3

in 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
    }
}

In my 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'
}

Here Espresso's test:

@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()
    }

But I get error:

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

Upvotes: 1

Views: 2857

Answers (1)

Jahnold
Jahnold

Reputation: 7683

This is happening because you are not specifying a specific OkHttp dependency in your app. When you don't specify one yourself then gradle will use the default version specified by Retrofit. If you check Retrofit POM file you can see that it specifies 3.12.0 which is where this version you are seeing in the error is coming from.

The solution is to add a specific gradle dependency for the version of OkHttp that you want to use:

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

This version would then be used instead of the default retrofit one and matches the requirement for your mock server version

Upvotes: 4

Related Questions