Kaynağa Gözat

add dependencies for testing
add test for serializing json to object

MrOzOn 4 yıl önce
ebeveyn
işleme
492fe80657

+ 6 - 1
app/build.gradle

@@ -48,11 +48,16 @@ dependencies {
     implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
     implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
     implementation "com.google.code.gson:gson:$gson_version"
-    //
+    //Coroutines
     implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
     implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
 
     testImplementation 'junit:junit:4.13.2'
+    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
+    testImplementation "androidx.arch.core:core-testing:2.1.0"
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+    //testing network
+    testImplementation 'org.mockito:mockito-core:2.19.0'
+    testImplementation "com.squareup.retrofit2:retrofit-mock:$retrofit_version"
 }

+ 30 - 0
app/src/test/java/com/mrozon/currencyconverter/CoroutineTestRule.kt

@@ -0,0 +1,30 @@
+package com.mrozon.currencyconverter
+
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.*
+import org.junit.rules.TestWatcher
+import org.junit.runner.Description
+import org.junit.runners.model.Statement
+
+@ExperimentalCoroutinesApi
+class CoroutineTestRule(val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : TestWatcher() {
+
+    private val testCoroutinesScope = TestCoroutineScope(testDispatcher)
+
+    override fun apply(base: Statement, description: Description?) = object : Statement() {
+        override fun evaluate() {
+            Dispatchers.setMain(testDispatcher)
+            base.evaluate()
+            Dispatchers.resetMain()
+            testCoroutinesScope.cleanupTestCoroutines()
+        }
+    }
+
+    fun runBlockingTest(block: suspend TestCoroutineScope.() -> Unit) {
+        testCoroutinesScope.runBlockingTest{
+            block()
+        }
+    }
+}

+ 42 - 0
app/src/test/java/com/mrozon/currencyconverter/data/network/CurrencyServiceTest.kt

@@ -0,0 +1,42 @@
+package com.mrozon.currencyconverter.data.network
+
+import androidx.arch.core.executor.testing.InstantTaskExecutorRule
+import com.mrozon.currencyconverter.CoroutineTestRule
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.runBlockingTest
+import kotlinx.coroutines.withContext
+import org.junit.Assert
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import retrofit2.Retrofit
+import retrofit2.converter.gson.GsonConverterFactory
+
+
+@ExperimentalCoroutinesApi
+class CurrencyServiceTest {
+
+    @get:Rule
+    var coroutinesTestRule = CoroutineTestRule()
+
+    @get:Rule
+    val instantExecutorRule = InstantTaskExecutorRule()
+
+    lateinit var service: CurrencyService
+
+    @Before
+    fun setUp(){
+        val retrofit = Retrofit.Builder()
+            .baseUrl("https://www.cbr-xml-daily.ru/")
+            .addConverterFactory(GsonConverterFactory.create())
+            .build()
+
+        service = retrofit.create(CurrencyService::class.java)
+    }
+
+    @Test
+    fun `get currencies from real network request`()  = coroutinesTestRule.runBlockingTest {
+        val response = service.getCurrencies()
+        Assert.assertEquals(response.valute?.size ?: 0, 34)
+    }
+}

+ 5 - 16
app/src/test/java/com/mrozon/currencyconverter/ExampleUnitTest.kt → app/src/test/java/com/mrozon/currencyconverter/data/network/MapValutesDeserializerTest.kt

@@ -1,20 +1,10 @@
-package com.mrozon.currencyconverter
+package com.mrozon.currencyconverter.data.network
 
-import CurrenciesResponse
-import Valute
-import com.google.gson.*
-import com.mrozon.currencyconverter.data.network.MapValutesDeserializer
-import org.junit.Assert.assertEquals
+import com.google.gson.GsonBuilder
+import org.junit.Assert.*
 import org.junit.Test
-import java.lang.reflect.Type
 
-
-/**
- * Example local unit test, which will execute on the development machine (host).
- *
- * See [testing documentation](http://d.android.com/tools/testing).
- */
-class ExampleUnitTest {
+class MapValutesDeserializerTest {
     @Test
     fun `check correct json to POJO`() {
         val json = """
@@ -339,5 +329,4 @@ class ExampleUnitTest {
         val response = gson.fromJson(json, CurrenciesResponse::class.java)
         assertEquals(response.valute?.size?:0, 34)
     }
-
-}
+}