MockWebServer and Retrofit with Callback

后端 未结 3 1915
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  猫巷女王i
    2021-02-05 20:40

    By having a Callback you are telling Retrofit to invoke the request and call the callback asynchronously. This means that your test is exiting before anything happens.

    There are two ways to get this to work:

    • Use a lock at the end of the test and wait until one of the callback methods are invoked.
    • Pass an instance of a synchronous Executor (one that just calls .run() immediately) to setExecutors on the RestAdapter.Builder so that the background invocations and callback invocations happen synchronously.

提交回复
热议问题