File tree Expand file tree Collapse file tree 4 files changed +31
-18
lines changed
src/test/java/jp/studyplus/android/sdk Expand file tree Collapse file tree 4 files changed +31
-18
lines changed Original file line number Diff line number Diff line change @@ -68,10 +68,12 @@ dependencies {
68
68
implementation ' org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
69
69
implementation ' org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
70
70
71
- implementation ' com.squareup.okhttp3:okhttp:3.11.0'
71
+ def okhttp = " 3.11.0"
72
+ implementation " com.squareup.okhttp3:okhttp:$okhttp "
72
73
73
74
testImplementation ' junit:junit:4.12'
74
75
testImplementation " org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version "
76
+ testImplementation " com.squareup.okhttp3:mockwebserver:$okhttp "
75
77
}
76
78
77
79
apply from : ' build.publish.gradle'
Original file line number Diff line number Diff line change @@ -14,10 +14,10 @@ class ApiUnitTest {
14
14
val record = StudyRecordBuilder ().build()
15
15
runBlocking {
16
16
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()
19
19
20
- assertEquals(result. recordId, 9999L )
20
+ assertEquals(recordId, 9999L )
21
21
} catch (t: Throwable ) {
22
22
assertNull(t)
23
23
}
Original file line number Diff line number Diff line change @@ -2,20 +2,8 @@ package jp.studyplus.android.sdk.internal.api
2
2
3
3
import android.content.Context
4
4
import 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
- }
18
5
6
+ internal object MockApiClient {
19
7
fun postStudyRecords (context : Context ? , studyRecord : StudyRecord ) =
20
- apiService.postStudyRecords( " " , studyRecord)
8
+ MockApiService ( ApiManager .client).post( studyRecord.toJson() )
21
9
}
Original file line number Diff line number Diff line change 1
1
package jp.studyplus.android.sdk.internal.api
2
2
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