Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso Idling Resources using OkHttp3IdlingResource - Type mismatch error

I'm using espresso for UI test While registering IdlingResource with OkHttp3Idresources, I'm getting a type mismatch error (Type mismatch: Required: IdlingResource, Found:OkHttp3IdlingResource)

@UninstallModules(
    NetworkModule::class,
)
@ExperimentalCoroutinesApi
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class FragmentTest {

    @get:Rule
    val hiltRule = HiltAndroidRule(this)
    
    private lateinit var mockWebServer: MockWebServer

    @Inject
    lateinit var okHttp: OkHttpClient
    
   val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
    
    @Before
     fun setUp() {
        hiltRule.inject()
        mockWebServer = MockWebServer()
        mockWebServer.dispatcher = MockServerDispatcher().RequestDispatcher()
        mockWebServer.start(8080)
        IdlingRegistry.getInstance().register(OkHttp3IdlingResource.create("okhttp", okHttp)) // Type mismatch.Required:IdlingResource!Found:OkHttp3IdlingResource
        
    }

Here are my dependencies

androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
androidTestImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation "org.mockito:mockito-core:3.0.6"
androidTestImplementation "org.mockito:mockito-android:3.2.0"
androidTestImplementation "androidx.test:core:1.4.0"
androidTestImplementation "androidx.test:runner:1.4.0"
androidTestImplementation "androidx.test:rules:1.4.0"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "androidx.test.espresso:espresso-contrib:3.4.0"
androidTestImplementation "androidx.test.espresso:espresso-intents:3.4.0"
androidTestImplementation  "com.google.dagger:hilt-android-testing:2.38.1"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.38.1"
androidTestImplementation "com.squareup.okhttp3:mockwebserver:4.9.3"
androidTestImplementation "com.jakewharton.espresso:okhttp3-idling-resource:1.0.0"
like image 341
Cherishxoxo Avatar asked Jan 22 '26 03:01

Cherishxoxo


1 Answers

The reason you're getting the error is because Jake Wharton's original library hasn't been updated since 2014 and is currently incompatible with AndroidX. An issue has already been raised on the repo (view issue) and a pull request has been made to fix the issue but it has not been accepted yet as of the time of the posting of this answer. In the meantime, I adapted the OkHttp3IdlingResource.java class in the library and converted it to Kotlin. You can view it on this GitHub gist and use it as is before the pull request is merged.

In case the link is broken, the code is below;

package com.mypackage

import androidx.annotation.CheckResult
import androidx.test.espresso.IdlingResource
import okhttp3.Dispatcher
import okhttp3.OkHttpClient

class OkHttp3IdlingResource private constructor(
    private val name: String,
    private val dispatcher: Dispatcher
) : IdlingResource {
    @Volatile
    var callback: IdlingResource.ResourceCallback? = null

    init {
        dispatcher.setIdleCallback {
            val callback = callback
            callback?.onTransitionToIdle()
        }
    }

    override fun getName(): String {
        return name
    }

    override fun isIdleNow(): Boolean {
        return dispatcher.runningCallsCount() == 0
    }

    override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback) {
        this.callback = callback
    }

    companion object {
        @CheckResult  // Extra guards as a library.
        fun create(name: String, client: OkHttpClient): OkHttp3IdlingResource {
            return OkHttp3IdlingResource(name, client.dispatcher())
        }
    }
}
like image 179
Kenneth Murerwa Avatar answered Jan 27 '26 00:01

Kenneth Murerwa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!