1
+ package com.example.network
2
+
3
+ import com.example.network.api.RetrofitPlaceAPI
4
+ import com.example.network.constant.Constants
5
+ import junit.framework.TestCase.assertEquals
6
+ import kotlinx.coroutines.test.runTest
7
+ import okhttp3.OkHttpClient
8
+ import org.junit.Before
9
+ import org.junit.Test
10
+ import retrofit2.Retrofit
11
+ import retrofit2.converter.gson.GsonConverterFactory
12
+ import java.util.concurrent.TimeUnit
13
+
14
+ /* *
15
+ * Network local unit test, which will execute on the development machine (host).
16
+ */
17
+ class NetworkUnitTest {
18
+ private lateinit var network: RetrofitPlaceAPI
19
+
20
+ // Default Latitude, Longitude
21
+ private val defaultLatLng = " 0.0, 0.0"
22
+ // Default Place ID
23
+ private val defaultPlaceID = " ChIJN1t_tDeuEmsRUsoyG83frY4"
24
+
25
+ @Before
26
+ fun init () {
27
+ val okHttpClient = OkHttpClient ()
28
+ .newBuilder()
29
+ .callTimeout(Constants .TIMEOUT_SEC , TimeUnit .SECONDS )
30
+ .readTimeout(Constants .TIMEOUT_SEC , TimeUnit .SECONDS )
31
+ .writeTimeout(Constants .TIMEOUT_SEC , TimeUnit .SECONDS )
32
+ .connectTimeout(Constants .TIMEOUT_SEC , TimeUnit .SECONDS )
33
+ .build()
34
+
35
+ val retrofitInstance = Retrofit
36
+ .Builder ()
37
+ .baseUrl(Constants .PLACE_API_BASE_URL )
38
+ .client(okHttpClient)
39
+ .addConverterFactory(GsonConverterFactory .create())
40
+ .build()
41
+
42
+ network = retrofitInstance.create(RetrofitPlaceAPI ::class .java)
43
+ }
44
+
45
+
46
+ @Test
47
+ fun call_NearBySearch_GET_RESULT () = runTest {
48
+ val response = network.getNearBySearch(
49
+ latLng = defaultLatLng,
50
+ radius = Constants .LOCATION_RADIUS ,
51
+ type = Constants .LOCATION_TYPE ,
52
+ apiKey = BuildConfig .PLACE_API_KEY
53
+ )
54
+
55
+ assertEquals(200 , response.code())
56
+ }
57
+
58
+ @Test
59
+ fun call_Detail_GET_RESULT () = runTest {
60
+ val response = network.getDetails(
61
+ placeID = defaultPlaceID,
62
+ apiKey = BuildConfig .PLACE_API_KEY
63
+ )
64
+
65
+ assertEquals(200 , response.code())
66
+ }
67
+ }
0 commit comments