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