Skip to content

Commit f8fb2c3

Browse files
committed
Testの更新
TODO : Roborectric対応
1 parent 696d769 commit f8fb2c3

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

studyplus-android-sdk2/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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

7779
apply from: 'build.publish.gradle'

studyplus-android-sdk2/src/test/java/jp/studyplus/android/sdk/ApiUnitTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff 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
}

studyplus-android-sdk2/src/test/java/jp/studyplus/android/sdk/internal/api/MockApiClient.kt

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,8 @@ package jp.studyplus.android.sdk.internal.api
22

33
import android.content.Context
44
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-
}
185

6+
internal object MockApiClient {
197
fun postStudyRecords(context: Context?, studyRecord: StudyRecord) =
20-
apiService.postStudyRecords("", studyRecord)
8+
MockApiService(ApiManager.client).post(studyRecord.toJson())
219
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
package 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+
}

0 commit comments

Comments
 (0)