0

我使用MockWebServer如下代码。但MockWebServer没有完成。测试正在无限进行中。我错过了什么?

结果: Jul 02, 2018 7:30:59 PM okhttp3.mockwebserver.MockWebServer$2 execute 정보: MockWebServer[49728] starting to accept connections

interface Webservice {
    @GET("users/{user}/missions")
    fun getMissions(@Path("user") user: String): Call<Missions>
}

class MockWebTest {

    private lateinit var webservice: Webservice

    @Before
    fun setUp() {
        val baseUrl = MyURL.API_DOMAIN
        val okHttpClient = OkHttpClient.Builder()
                .connectTimeout(2, TimeUnit.SECONDS)
                .readTimeout(2, TimeUnit.SECONDS)
                .writeTimeout(2, TimeUnit.SECONDS)
                .build()
        val retrofit = Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build()

        webservice = retrofit.create(Webservice::class.java)
    }

    @Test
    fun test3() {
        val server = MockWebServer()
        server.start()

        val missionsJson = """
            {
                "message": "walk"
            }
        """.trimIndent()

        server.enqueue(MockResponse().setBody(missionsJson).throttleBody(1024, 1, TimeUnit.SECONDS))


        val call = webservice.getMissions("1")
        val entity = call.execute().body()!!

        assertEquals("walk", entity.message)

        val request1 = server.takeRequest()
        assertEquals("GET", request.method)
        assertEquals("/users/1/missions", request.path)

        server.shutdown()
    }
}
4

1 回答 1

2

我检查了这个 repo GithubBrowserSample Google 示例。改造 baseUrl 需要由MockWebServer#url(String)

像这样,不是我的网址。

val retrofit = Retrofit.Builder()
        .baseUrl(server.url("/"))
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
于 2018-07-03T02:24:12.553 回答