MockWebServer and Retrofit with Callback

后端 未结 3 1881
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 20:04

I would like to simulate network communication by MockWebServer. Unfortulatelly retrofit callbacks are never invoking. My code:

    MockWebServer server = new M         


        
3条回答
  •  一向
    一向 (楼主)
    2021-02-05 20:42

    Alternatively you could use Mockinizer with MockWebServer:

    OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .mockinize(mocks) // <-- just add this line
            .build()
    

    And the requests/responses that you want to mock you can define in the mocks value. In your case it would look something like:

    package com.appham.mockinizer.demo
    
    import com.appham.mockinizer.RequestFilter
    import okhttp3.mockwebserver.MockResponse
    
    val mocks: Map = mapOf(
    
        RequestFilter("/") to MockResponse().apply {
            setResponseCode(200)
            setBody("""{}""")
        }
    
    )
    

    See https://github.com/donfuxx/Mockinizer

提交回复
热议问题