This repository was archived by the owner on Jun 28, 2019. It is now read-only.
File tree Expand file tree Collapse file tree
src/test/java/jp/studyplus/android/sdk Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -68,10 +68,12 @@ dependencies {
6868 implementation ' org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
6969 implementation ' org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
7070
71- implementation ' com.squareup.okhttp3:okhttp:3.11.0'
71+ def okhttp = " 3.11.0"
72+ implementation " com.squareup.okhttp3:okhttp:$okhttp "
7273
7374 testImplementation ' junit:junit:4.12'
7475 testImplementation " org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version "
76+ testImplementation " com.squareup.okhttp3:mockwebserver:$okhttp "
7577}
7678
7779apply from : ' build.publish.gradle'
Original file line number Diff line number Diff line change @@ -14,10 +14,10 @@ class ApiUnitTest {
1414 val record = StudyRecordBuilder ().build()
1515 runBlocking {
1616 try {
17- val deferred = MockApiClient .apiClient. postStudyRecords(null , record)
18- val result = deferred.await()
17+ val deferred = MockApiClient .postStudyRecords(null , record)
18+ val recordId = deferred.await()
1919
20- assertEquals(result. recordId, 9999L )
20+ assertEquals(recordId, 9999L )
2121 } catch (t: Throwable ) {
2222 assertNull(t)
2323 }
Original file line number Diff line number Diff line change @@ -2,20 +2,8 @@ package jp.studyplus.android.sdk.internal.api
22
33import android.content.Context
44import jp.studyplus.android.sdk.record.StudyRecord
5- import retrofit2.mock.MockRetrofit
6-
7- internal class MockApiClient constructor(retrofit : MockRetrofit ) {
8-
9- companion object {
10- val apiClient by lazy { MockApiClient (MockApiManager .retrofit) }
11- lateinit var apiService: MockApiService
12- }
13-
14- init {
15- val delegate = retrofit.create(ApiService ::class .java)
16- apiService = MockApiService (delegate)
17- }
185
6+ internal object MockApiClient {
197 fun postStudyRecords (context : Context ? , studyRecord : StudyRecord ) =
20- apiService.postStudyRecords( " " , studyRecord)
8+ MockApiService ( ApiManager .client).post( studyRecord.toJson() )
219}
Original file line number Diff line number Diff line change 11package jp.studyplus.android.sdk.internal.api
22
3+ import kotlinx.coroutines.Deferred
4+ import okhttp3.OkHttpClient
5+ import okhttp3.Request
6+ import okhttp3.mockwebserver.MockResponse
7+ import okhttp3.mockwebserver.MockWebServer
8+
9+ internal class MockApiService (private val client : OkHttpClient ) {
10+
11+ fun post (json : String ): Deferred <Long ?> {
12+ val server = MockWebServer ()
13+ server.enqueue(MockResponse ().setBody(""" "{"record_id": 9999L}""" ))
14+ server.start()
15+
16+ val body = createPostBody(json)
17+ val request = Request .Builder ()
18+ .url(server.url(" /v1/study_records" ))
19+ .post(body)
20+ .build()
21+ val call = client.newCall(request)
22+
23+ return execute(call)
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments