From 1e816a43d31e83913cb6b4c9a53f50fa9918dff9 Mon Sep 17 00:00:00 2001 From: Satish Prasad Date: Fri, 29 Nov 2019 12:37:18 +0530 Subject: [PATCH] Feature/mockito setup (#48) * mockito set up * added mockito basic setup * handled teminated cases --- app/build.gradle | 5 ++ .../registration/viewmodel/SplashVM.kt | 6 ++ .../viewmodel/UploadPanVM.kt | 9 ++- .../registration/viewmodel/LoginPageVMTest.kt | 36 +++++++++++ .../registration/viewmodel/SplashVMTest.kt | 39 ++++++++++++ .../viewmodel/UploadPanVMTest.kt | 63 +++++++++++++++++++ 6 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/LoginPageVMTest.kt create mode 100644 app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVMTest.kt create mode 100644 app/src/test/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVMTest.kt diff --git a/app/build.gradle b/app/build.gradle index e3eb72b2c0..ba3ec9029d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -75,4 +75,9 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + testImplementation 'org.mockito:mockito-core:2.19.0' + androidTestImplementation 'android.arch.core:core-testing:1.1.1' + testImplementation 'android.arch.core:core-testing:1.0.0' + testImplementation 'org.mockito:mockito-inline:2.13.0' + } diff --git a/app/src/main/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVM.kt b/app/src/main/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVM.kt index eeb2c2057d..c437bb09ef 100644 --- a/app/src/main/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVM.kt +++ b/app/src/main/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVM.kt @@ -10,6 +10,8 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.navi.medici.androidCustomerApp.models.request.RegisterRequest +import com.navi.medici.androidCustomerApp.models.response.RegisterResponse +import com.navi.medici.androidCustomerApp.network.RepoResult import com.navi.medici.androidCustomerApp.registration.repositories.RegisterRepository import com.navi.medici.androidCustomerApp.utils.Constants.CHANNEL_ID import kotlinx.coroutines.CoroutineScope @@ -24,12 +26,16 @@ class SplashVM : ViewModel() { val isBlacklisted: LiveData get() = _isBlacklisted + val result = MutableLiveData>() + fun checkDevice(imei: String) { Timber.i("IMEI $imei") val registerRequest = RegisterRequest(imei, CHANNEL_ID) coroutineScope.launch { val response = registerRepository.checkDevice(registerRequest) Timber.i("Register response $response") + + result.value = response if (response.exception == null) { _isBlacklisted.value = response.response.data?.success?.not() } else { diff --git a/app/src/main/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVM.kt b/app/src/main/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVM.kt index 0dc070fb26..feeda7b18e 100644 --- a/app/src/main/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVM.kt +++ b/app/src/main/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVM.kt @@ -12,6 +12,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.navi.medici.androidCustomerApp.app.NaviApplication import com.navi.medici.androidCustomerApp.common.PreferenceManager +import com.navi.medici.androidCustomerApp.models.response.UploadPanResponse import com.navi.medici.androidCustomerApp.repositories.UploadPanRepository import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -29,7 +30,7 @@ class UploadPanVM : ViewModel() { private val coroutineScope = CoroutineScope(Dispatchers.Main) private val uploadPanRepository = UploadPanRepository() - val imageBitmap = MutableLiveData< Bitmap>() + val imageBitmap = MutableLiveData() fun uploadPan(file: File) { val reqFile = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), file) @@ -45,8 +46,12 @@ class UploadPanVM : ViewModel() { ) Timber.i("Upload PAN response $response") if (response.exception == null) { - _panUploaded.value = true + setUploadPan(true) } } } + + fun setUploadPan(res: Boolean) { + _panUploaded.value = res + } } \ No newline at end of file diff --git a/app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/LoginPageVMTest.kt b/app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/LoginPageVMTest.kt new file mode 100644 index 0000000000..dc50b09d41 --- /dev/null +++ b/app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/LoginPageVMTest.kt @@ -0,0 +1,36 @@ +/* + * * + * * Copyright (c) 2019 . All rights reserved @Navi + * + */ + +package com.navi.medici.androidCustomerApp.registration.viewmodel + +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.junit.MockitoJUnitRunner + + +@RunWith(MockitoJUnitRunner::class) +class LoginPageVMTest { + + @Mock + lateinit var loginPageVm: LoginPageVM + + @Before + fun setUp() { + loginPageVm = LoginPageVM() + } + + @After + fun tearDown() { + } + + @Test + fun onChangePhoneNumber() { + + } +} \ No newline at end of file diff --git a/app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVMTest.kt b/app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVMTest.kt new file mode 100644 index 0000000000..c721117a2c --- /dev/null +++ b/app/src/test/java/com/navi/medici/androidCustomerApp/registration/viewmodel/SplashVMTest.kt @@ -0,0 +1,39 @@ +/* + * * + * * Copyright (c) 2019 . All rights reserved @Navi + * + */ + +package com.navi.medici.androidCustomerApp.registration.viewmodel + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Before +import org.junit.Test + +import org.junit.runner.RunWith +import org.junit.Rule +import org.mockito.junit.MockitoJUnitRunner + + +@RunWith(MockitoJUnitRunner::class) +class SplashVMTest { + + @get: Rule + var instantExecutorRule = InstantTaskExecutorRule() + + @Before + fun setUp() { + } + + @Test + fun checkDevice() = runBlocking { + + } + + @After + fun tearDown() { + + } +} \ No newline at end of file diff --git a/app/src/test/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVMTest.kt b/app/src/test/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVMTest.kt new file mode 100644 index 0000000000..f809c3ea80 --- /dev/null +++ b/app/src/test/java/com/navi/medici/androidCustomerApp/useridentityupload/viewmodel/UploadPanVMTest.kt @@ -0,0 +1,63 @@ +/* + * * + * * Copyright (c) 2019 . All rights reserved @Navi + * + */ + +package com.navi.medici.androidCustomerApp.useridentityupload.viewmodel + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.lifecycle.Observer +import com.navi.medici.androidCustomerApp.utils.log +import org.junit.After +import org.junit.Before +import org.junit.Test + +import org.junit.Assert.* +import org.junit.Rule +import org.junit.runner.RunWith +import org.mockito.ArgumentCaptor +import org.mockito.Mockito.* +import org.mockito.junit.MockitoJUnitRunner + + +@RunWith(MockitoJUnitRunner::class) +class UploadPanVMTest { + + inline fun mock(): T = mock(T::class.java) + + @get:Rule + val rule = InstantTaskExecutorRule() + + private lateinit var uploadPanVm: UploadPanVM + private val uploadPanObserver: Observer = mock() + + + @Before + fun setUp() { + uploadPanVm = UploadPanVM() + uploadPanVm.panUploaded.observeForever(uploadPanObserver) + } + + + @Test + fun setPanUploadRes() { + val expectedResponse = true + uploadPanVm.setUploadPan(true) + + val captor = ArgumentCaptor.forClass(Boolean::class.java) + captor.run { + verify(uploadPanObserver, times(1)).onChanged(capture()) + assertEquals(expectedResponse, value) + } + } + + @After + fun tearDown() { + try { + uploadPanVm.panUploaded.removeObserver(uploadPanObserver) + } catch (e: Exception) { + e.log() + } + } +} \ No newline at end of file