From 68f02b8917630e98a930a462f16ef9ac7ee538f4 Mon Sep 17 00:00:00 2001 From: "h.malik144@gmail.com" Date: Sat, 26 Aug 2023 22:23:19 +0100 Subject: [PATCH] - RepositoryImplTest.kt added --- app/build.gradle | 28 +++++++++- .../appttude/h_mal/farmr/ExampleUnitTest.java | 17 ------ .../h_mal/farmr/data/RepositoryImplTest.kt | 52 +++++++++++++++++++ .../appttude/h_mal/farmr/utils/testUtils.kt | 39 ++++++++++++++ 4 files changed, 117 insertions(+), 19 deletions(-) delete mode 100644 app/src/test/java/com/appttude/h_mal/farmr/ExampleUnitTest.java create mode 100644 app/src/test/java/com/appttude/h_mal/farmr/data/RepositoryImplTest.kt create mode 100644 app/src/test/java/com/appttude/h_mal/farmr/utils/testUtils.kt diff --git a/app/build.gradle b/app/build.gradle index 8a92332..e7edb61 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,11 +34,33 @@ dependencies { implementation 'androidx.activity:activity-ktx:1.4.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' implementation 'androidx.preference:preference:1.2.1' - implementation 'com.ajts.androidmads.SQLite2Excel:library:1.0.2' - testImplementation 'junit:junit:4.12' + / * Unit testing * / + testImplementation 'junit:junit:4.13.2' + androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" + testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" + implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version" androidTestImplementation 'androidx.test:core-ktx:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0' + / * mockito and livedata testing * / + testImplementation 'org.mockito:mockito-inline:2.13.0' + implementation 'androidx.arch.core:core-testing:2.1.0' + / * MockK * / + def mockk_ver = "1.10.5" + testImplementation "io.mockk:mockk:$mockk_ver" + androidTestImplementation "io.mockk:mockk-android:$mockk_ver" + / * Android Espresso * / + def testJunitVersion = "1.1.5" + def testRunnerVersion = "1.5.2" + def espressoVersion = "3.5.1" + androidTestImplementation "androidx.test.ext:junit:$testJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion" + androidTestImplementation "androidx.test.espresso.idling:idling-concurrent:$espressoVersion" + implementation "androidx.test.espresso:espresso-idling-resource:$espressoVersion" + androidTestImplementation "androidx.test:runner:$testRunnerVersion" + androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion" + androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion" + androidTestImplementation "org.hamcrest:hamcrest:2.2" / * Room database * / def room_version = "2.4.3" implementation "androidx.room:room-runtime:$room_version" @@ -48,4 +70,6 @@ dependencies { def kodein_version = "6.2.1" implementation "org.kodein.di:kodein-di-generic-jvm:$kodein_version" implementation "org.kodein.di:kodein-di-framework-android-x:$kodein_version" + / * SQLite to excel */ + implementation 'com.ajts.androidmads.SQLite2Excel:library:1.0.2' } diff --git a/app/src/test/java/com/appttude/h_mal/farmr/ExampleUnitTest.java b/app/src/test/java/com/appttude/h_mal/farmr/ExampleUnitTest.java deleted file mode 100644 index cfb3bfd..0000000 --- a/app/src/test/java/com/appttude/h_mal/farmr/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.appttude.h_mal.farmr; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/app/src/test/java/com/appttude/h_mal/farmr/data/RepositoryImplTest.kt b/app/src/test/java/com/appttude/h_mal/farmr/data/RepositoryImplTest.kt new file mode 100644 index 0000000..4c53925 --- /dev/null +++ b/app/src/test/java/com/appttude/h_mal/farmr/data/RepositoryImplTest.kt @@ -0,0 +1,52 @@ +package com.appttude.h_mal.farmr.data + +import com.appttude.h_mal.farmr.data.legacydb.LegacyDatabase +import com.appttude.h_mal.farmr.data.legacydb.ShiftObject +import com.appttude.h_mal.farmr.data.prefs.PreferenceProvider +import io.mockk.MockKAnnotations +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.mockk +import org.junit.Before +import org.junit.Test +import org.mockito.ArgumentMatchers.anyLong +import java.util.UUID +import kotlin.test.assertEquals +import kotlin.test.assertIs + +class RepositoryImplTest { + + private lateinit var repository: RepositoryImpl + + @MockK + lateinit var db: LegacyDatabase + + @MockK + lateinit var prefs: PreferenceProvider + + @Before + fun setUp() { + MockKAnnotations.init(this) + repository = RepositoryImpl(db, prefs) + } + + @Test + fun readDatabase_validResponse() { + // Arrange + val elements = listOf( + mockk { every { id } returns anyLong() }, + mockk { every { id } returns anyLong() }, + mockk { every { id } returns anyLong() }, + mockk { every { id } returns anyLong() } + ) + + //Act + every { db.readShiftsFromDatabase() } returns elements + + // Assert + val result = repository.readShiftsFromDatabase() + assertIs>(result) + assertEquals(result.first().id, anyLong()) + } + +} \ No newline at end of file diff --git a/app/src/test/java/com/appttude/h_mal/farmr/utils/testUtils.kt b/app/src/test/java/com/appttude/h_mal/farmr/utils/testUtils.kt new file mode 100644 index 0000000..4d64b8d --- /dev/null +++ b/app/src/test/java/com/appttude/h_mal/farmr/utils/testUtils.kt @@ -0,0 +1,39 @@ +package com.appttude.h_mal.farmr.utils + +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeoutException + +fun LiveData.getOrAwaitValue( + time: Long = 2, + timeUnit: TimeUnit = TimeUnit.SECONDS +): T { + var data: T? = null + val latch = CountDownLatch(1) + val observer = object : Observer { + override fun onChanged(o: T?) { + data = o + latch.countDown() + this@getOrAwaitValue.removeObserver(this) + } + } + + this.observeForever(observer) + + // Don't wait indefinitely if the LiveData is not set. + if (!latch.await(time, timeUnit)) { + throw TimeoutException("LiveData value was never set.") + } + + @Suppress("UNCHECKED_CAST") + return data as T +} + +fun sleep(millis: Long = 1000) { + runBlocking(Dispatchers.Default) { delay(millis) } +} \ No newline at end of file