Skip to content

Commit

Permalink
test: Refactor tests for About page
Browse files Browse the repository at this point in the history
This commit adds tests for AboutViewModel handling issue #53

Other changes:
- Update dependencies
  • Loading branch information
michaelbukachi committed Oct 3, 2019
1 parent 872a92b commit 60f3058
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 133 deletions.
49 changes: 32 additions & 17 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ android {
unitTests {
returnDefaultValues = true
includeAndroidResources = true
animationsDisabled
}
}

Expand All @@ -55,23 +56,28 @@ android {
experimental = true
}

kotlinOptions {
jvmTarget = "1.8"
}

}

dependencies {
def coroutines_version = '1.2.1'
def coroutines_version = '1.3.1'
def koin_version = '2.0.1'
def room_version = '2.1.0'

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-beta01'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat-resources:1.1.0'
implementation 'com.google.android.material:material:1.1.0-alpha07'
// Maintain version 2.0.0-alpha2 till issue is resolved (https://issuetracker.google.com/issues/121395935)
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-beta02'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation "org.apache.commons:commons-lang3:3.5"
implementation 'androidx.media:media:1.1.0-rc01'
implementation 'androidx.media:media:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.browser:browser:1.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
Expand All @@ -87,19 +93,19 @@ dependencies {
kapt 'com.jakewharton:butterknife-compiler:10.1.0'

//firebase dependencies
implementation 'com.google.firebase:firebase-core:17.0.1'
implementation 'com.google.firebase:firebase-core:17.2.0'
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
implementation 'com.google.firebase:firebase-auth:18.1.0'
implementation 'com.google.firebase:firebase-database:18.0.1'
implementation 'com.google.firebase:firebase-config:18.0.0'
implementation 'com.google.firebase:firebase-auth:19.1.0'
implementation 'com.google.firebase:firebase-database:19.1.0'
implementation 'com.google.firebase:firebase-config:19.0.2'
implementation 'com.google.firebase:firebase-crash:16.2.1'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
implementation 'com.google.firebase:firebase-firestore:20.2.0'
implementation 'com.google.firebase:firebase-messaging:20.0.0'
implementation 'com.google.firebase:firebase-firestore:21.1.1'
implementation "com.firebaseui:firebase-ui-firestore:3.3.0"
implementation 'com.google.firebase:firebase-common-ktx:18.0.0'
implementation 'com.google.firebase:firebase-firestore-ktx:20.2.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.2.1'
implementation 'com.google.firebase:firebase-perf:18.0.1'
implementation 'com.google.firebase:firebase-common-ktx:19.1.0'
implementation 'com.google.firebase:firebase-firestore-ktx:21.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.1'
implementation 'com.google.firebase:firebase-perf:19.0.0'

//architecture components dependencies
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-alpha01"
Expand Down Expand Up @@ -127,25 +133,34 @@ dependencies {

testImplementation 'junit:junit:4.12'
testImplementation "org.koin:koin-test:$koin_version"
testImplementation "androidx.arch.core:core-testing:2.0.1"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation 'androidx.test:rules:1.2.0'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0'
testImplementation("io.mockk:mockk:1.9.3")
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation "androidx.room:room-testing:$room_version"
testImplementation "androidx.test.espresso:espresso-core:3.2.0"
testImplementation "androidx.test.espresso:espresso-contrib:3.2.0"
testImplementation "androidx.test.espresso:espresso-intents:3.2.0"
testImplementation "org.robolectric:robolectric:4.3"
testImplementation "com.agoda.kakao:kakao:2.1.0"
implementation("androidx.fragment:fragment-testing:1.1.0") {
exclude group: "androidx.test", module: "core"
}

androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation "androidx.arch.core:core-testing:2.0.1"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation 'com.bartoszlipinski:disable-animations-rule:1.0.0'
implementation('com.crashlytics.sdk.android:crashlytics:2.9.9@aar') {
transitive = true
}
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ val appModule = module {

val dataModule = module {
// Repos
single { AboutDetailsRepo(get()) }
single<AboutDetailsRepo> { AboutDetailsRepoImpl(get()) }
single { SessionsRepo(get(), get()) }
single { SessionDataRepo(get(), get()) }
single { SpeakersRepo(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class InstanceIdService : FirebaseMessagingService() {

private val sharedPreferences: SharedPreferences by inject { parametersOf(this) }

override fun onNewToken(token: String?) {
override fun onNewToken(token: String) {
super.onNewToken(token)
saveToken(token)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.android254.droidconke19.repository

import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.toObjects
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.models.AboutDetailsModel
import com.android254.droidconke19.utils.await
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.toObjects

interface AboutDetailsRepo {
suspend fun getAboutDetails(aboutType: String): Result<List<AboutDetailsModel>>
}


class AboutDetailsRepo(val firestore: FirebaseFirestore) {
class AboutDetailsRepoImpl(val firestore: FirebaseFirestore) : AboutDetailsRepo {

suspend fun getAboutDetails(aboutType: String): Result<List<AboutDetailsModel>> {
override suspend fun getAboutDetails(aboutType: String): Result<List<AboutDetailsModel>> {
return try {
val snapshot = firestore.collection(aboutType)
.orderBy("id", Query.Direction.ASCENDING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import com.android254.droidconke19.utils.loadImage
import com.android254.droidconke19.utils.nonNull
import com.android254.droidconke19.utils.observe
import com.android254.droidconke19.viewmodels.AboutViewModel
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.fragment_about.*
import org.jetbrains.anko.toast
import org.koin.android.ext.android.inject
Expand Down
16 changes: 0 additions & 16 deletions app/src/test/java/com/android254/droidconke19/ModulesTest.kt

This file was deleted.

27 changes: 0 additions & 27 deletions app/src/test/java/com/android254/droidconke19/utils.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
package com.android254.droidconke19

import android.content.Context
import android.content.SharedPreferences
import androidx.lifecycle.*
import com.android254.droidconke19.di.appModule
import com.android254.droidconke19.di.dataModule
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.nhaarman.mockitokotlin2.mock
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
Expand All @@ -20,22 +10,7 @@ import kotlinx.coroutines.test.setMain
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module

val testContext = module {
single { mock<Context>() }
}

val testApp = module {
single(override = true) { mock<FirebaseFirestore>() }
single(override = true) { mock<FirebaseAuth>() }
single(override = true) { mock<FirebaseRemoteConfig>() }
single(override = true) { mock<FirebaseDatabase>() }
single(override = true) { mock<FirebaseMessaging>() }
single(override = true) { mock<SharedPreferences>() }
}

@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
Expand All @@ -46,11 +21,9 @@ class CoroutinesRule : TestRule {

override fun evaluate() {
Dispatchers.setMain(mainThreadSurrogate)
startKoin { modules(listOf(testContext, appModule, dataModule, testApp)) }
base?.evaluate()
Dispatchers.resetMain() // reset main dispatcher to the original Main dispatcher
mainThreadSurrogate.close()
stopKoin()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,118 @@ package com.android254.droidconke19.viewmodels

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.android254.droidconke19.CoroutinesRule
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.observeOnce
import com.android254.droidconke19.repository.AboutDetailsRepo
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.empty
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.koin.test.KoinTest
import org.koin.test.inject
import org.koin.test.mock.declareMock

@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class AboutViewModelTest : KoinTest {
class AboutViewModelTest {

private val aboutDetailsRepo: AboutDetailsRepo by inject()
private val aboutViewModel: AboutViewModel by inject()

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

@get:Rule
val coroutinesRule = CoroutinesRule()

@MockK
lateinit var aboutDetailsRepo: AboutDetailsRepo

@InjectMockKs
lateinit var aboutViewModel: AboutViewModel

@Before
fun setup() {
declareMock<AboutDetailsRepo>()
MockKAnnotations.init(this)
}

@Test
fun `test fetchAboutDetails`() = runBlocking {
fun `test fetchAboutDetails`() {

// `when`(aboutDetailsRepo.getAboutDetails(any())).thenReturn(Result<List<AboutDetailsModel>>())
coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList())

aboutViewModel.fetchAboutDetails("value")

aboutViewModel.getAboutDetailsResponse().observeOnce {
Assert.assertTrue(it.isEmpty() ?: false)
assertThat(it, `is`(empty()))
}

}

@Test
fun `test fetchAboutDetails error `() {

coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error")

aboutViewModel.fetchAboutDetails("value")

aboutViewModel.getAboutDetailsError().observeOnce {
assertThat(it, `is`("Some error"))
}

}

@Test
fun `test getOrganizers`() {

coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList())

aboutViewModel.getOrganizers("value")

aboutViewModel.getAboutDetailsResponse().observeOnce {
assertThat(it, `is`(empty()))
}

}

@Test
fun `test getOrganizers error `() {

coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error")

aboutViewModel.getOrganizers("value")

aboutViewModel.getAboutDetailsError().observeOnce {
assertThat(it, `is`("Some error"))
}

}

@Test
fun `test getSponsors`() {

coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Success(emptyList())

aboutViewModel.getSponsors("value")

aboutViewModel.getAboutDetailsResponse().observeOnce {
assertThat(it, `is`(empty()))
}

}

@Test
fun `test getSponsors error `() {

coEvery { aboutDetailsRepo.getAboutDetails(any()) } returns Result.Error("Some error")

aboutViewModel.getSponsors("value")

aboutViewModel.getAboutDetailsError().observeOnce {
assertThat(it, `is`("Some error"))
}

}
Expand Down
Loading

0 comments on commit 60f3058

Please sign in to comment.