Skip to content

Commit

Permalink
feat: added coroutine rule
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 authored and therajanmaurya committed Aug 15, 2023
1 parent bbebdcf commit d120e10
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.mifos.mobile.repositories

import kotlinx.coroutines.Dispatchers
import CoroutineTestRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import okhttp3.ResponseBody
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mifos.mobile.FakeRemoteDataSource
Expand All @@ -22,8 +22,12 @@ import org.mockito.junit.MockitoJUnitRunner
import retrofit2.Response

@RunWith(MockitoJUnitRunner::class)
@ExperimentalCoroutinesApi
class UserAuthRepositoryImpTest {

@get:Rule
val coroutineTestRule = CoroutineTestRule()

@Mock
lateinit var dataManager: DataManager

Expand All @@ -41,7 +45,6 @@ class UserAuthRepositoryImpTest {
@Test
fun testRegisterUser_SuccessResponseReceivedFromDataManager_ReturnSuccessfulRegistration() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val successResponse: Response<ResponseBody?> =
Response.success(Mockito.mock(ResponseBody::class.java))
val registerPayload = RegisterPayload().apply {
Expand Down Expand Up @@ -70,13 +73,11 @@ class UserAuthRepositoryImpTest {

Mockito.verify(dataManager).registerUser(registerPayload)
Assert.assertEquals(result, successResponse)
Dispatchers.resetMain()
}

@Test
fun testRegisterUser_ErrorResponseReceivedFromDataManager_ReturnsUnsuccessfulRegistration() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val error: Response<ResponseBody?> =
Response.error(404, ResponseBody.create(null, "error"))
val registerPayload = RegisterPayload().apply {
Expand Down Expand Up @@ -105,12 +106,10 @@ class UserAuthRepositoryImpTest {

Mockito.verify(dataManager).registerUser(registerPayload)
Assert.assertEquals(result, error)
Dispatchers.resetMain()
}

@Test
fun testLogin_SuccessResponseReceivedFromDataManager_ReturnsUserSuccessfully() = runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val mockLoginPayload = LoginPayload().apply {
this.username = "username"
this.password = "password"
Expand All @@ -124,12 +123,10 @@ class UserAuthRepositoryImpTest {

Mockito.verify(dataManager).login(mockLoginPayload)
Assert.assertEquals(result, successResponse)
Dispatchers.resetMain()
}

@Test
fun testLogin_ErrorResponseReceivedFromDataManager_ReturnsError() = runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val mockLoginPayload = LoginPayload().apply {
this.username = "username"
this.password = "password"
Expand All @@ -143,13 +140,11 @@ class UserAuthRepositoryImpTest {

Mockito.verify(dataManager).login(mockLoginPayload)
Assert.assertEquals(result, errorResponse)
Dispatchers.resetMain()
}

@Test
fun testVerifyUser_SuccessResponseReceivedFromDataManager_ReturnsSuccessfulRegistrationVerification() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val successResponse: Response<ResponseBody?> =
Response.success(Mockito.mock(ResponseBody::class.java))
Mockito.`when`(
Expand All @@ -164,13 +159,11 @@ class UserAuthRepositoryImpTest {

Mockito.verify(dataManager).verifyUser(userVerify)
Assert.assertEquals(result, successResponse)
Dispatchers.resetMain()
}

@Test
fun testVerifyUser_ErrorResponseReceivedFromDataManager_ReturnsUnsuccessfulRegistrationVerification() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val errorResponse: Response<ResponseBody?> =
Response.error(404, ResponseBody.create(null, "error"))
Mockito.`when`(
Expand All @@ -184,6 +177,5 @@ class UserAuthRepositoryImpTest {
)
Mockito.verify(dataManager).verifyUser(userVerify)
Assert.assertEquals(result, errorResponse)
Dispatchers.resetMain()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.mifos.mobile.viewModels

import CoroutineTestRule
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import okhttp3.ResponseBody
import org.junit.*
import org.junit.runner.RunWith
Expand All @@ -25,6 +24,10 @@ class RegistrationViewModelTest {
@Rule
val mOverrideSchedulersRule = RxSchedulersOverrideRule()

@OptIn(ExperimentalCoroutinesApi::class)
@get:Rule
val coroutineTestRule = CoroutineTestRule()

@get:Rule
val rule = InstantTaskExecutorRule()

Expand Down Expand Up @@ -114,7 +117,6 @@ class RegistrationViewModelTest {
@Test
fun testRegisterUser_SuccessfulRegistrationReceivedFromRepository_ReturnsRegistrationSuccessful() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val responseBody = Mockito.mock(ResponseBody::class.java)
Mockito.`when`(
userAuthRepositoryImp.registerUser(
Expand Down Expand Up @@ -143,13 +145,11 @@ class RegistrationViewModelTest {
Mockito.verify(registrationUiStateObserver).onChanged(RegistrationUiState.Loading)
Mockito.verify(registrationUiStateObserver).onChanged(RegistrationUiState.Success)
Mockito.verifyNoMoreInteractions(registrationUiStateObserver)
Dispatchers.resetMain()
}

@Test
fun testRegisterUser_UnsuccessfulRegistrationReceivedFromRepository_ReturnsRegistrationUnsuccessful() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
Mockito.`when`(
userAuthRepositoryImp.registerUser(
Mockito.anyString(),
Expand Down Expand Up @@ -178,13 +178,11 @@ class RegistrationViewModelTest {
Mockito.verify(registrationUiStateObserver)
.onChanged(Mockito.any(RegistrationUiState.Error::class.java))
Mockito.verifyNoMoreInteractions(registrationUiStateObserver)
Dispatchers.resetMain()
}

@Test
fun testVerifyUser_SuccessfulRegistrationVerificationReceivedFromRepository_ReturnsRegistrationVerificationSuccessful() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
Mockito.`when`(
userAuthRepositoryImp.verifyUser(Mockito.anyString(), Mockito.anyString())
).thenReturn(Response.success(Mockito.mock(ResponseBody::class.java)))
Expand All @@ -196,13 +194,11 @@ class RegistrationViewModelTest {
Mockito.verify(registrationVerificationUiStateObserver)
.onChanged(RegistrationUiState.Success)
Mockito.verifyNoMoreInteractions(registrationUiStateObserver)
Dispatchers.resetMain()
}

@Test
fun testVerifyUser_UnsuccessfulRegistrationVerificationReceivedFromRepository_ReturnsRegistrationVerificationUnsuccessful() =
runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
Mockito.`when`(
userAuthRepositoryImp.verifyUser(Mockito.anyString(), Mockito.anyString())
).thenReturn(Response.error(404, ResponseBody.create(null, "error")))
Expand All @@ -212,7 +208,6 @@ class RegistrationViewModelTest {
Mockito.verify(registrationVerificationUiStateObserver)
.onChanged(RegistrationUiState.Loading)
Mockito.verifyNoMoreInteractions(registrationUiStateObserver)
Dispatchers.resetMain()
}

@After
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.mifos.mobile.viewModels

import CoroutineTestRule
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import okhttp3.ResponseBody
import org.junit.*
import org.junit.runner.RunWith
Expand All @@ -26,6 +25,10 @@ class UpdatePasswordViewModelTest {
@Rule
val mOverrideSchedulersRule = RxSchedulersOverrideRule()

@OptIn(ExperimentalCoroutinesApi::class)
@get:Rule
val coroutineTestRule = CoroutineTestRule()

@get:Rule
val rule = InstantTaskExecutorRule()

Expand Down Expand Up @@ -86,7 +89,6 @@ class UpdatePasswordViewModelTest {

@Test
fun testUpdateAccountPassword_SuccessReceivedFromRepository_ReturnsSuccess() = runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
val responseBody = Mockito.mock(ResponseBody::class.java)
Mockito.`when`(
userAuthRepositoryImp.updateAccountPassword(Mockito.anyString(), Mockito.anyString())
Expand All @@ -97,12 +99,10 @@ class UpdatePasswordViewModelTest {
Mockito.verify(updatePasswordUiStateObserver).onChanged(RegistrationUiState.Success)
Mockito.verify(clientRepositoryImp).updateAuthenticationToken("newPassword")
Mockito.verifyNoMoreInteractions(updatePasswordUiStateObserver)
Dispatchers.resetMain()
}

@Test
fun testUpdateAccountPassword_ErrorReceivedFromRepository_ReturnsError() = runBlocking {
Dispatchers.setMain(Dispatchers.Unconfined)
Mockito.`when`(
userAuthRepositoryImp.updateAccountPassword(Mockito.anyString(), Mockito.anyString())
).thenReturn(Response.error(404, ResponseBody.create(null, "error")))
Expand All @@ -113,7 +113,6 @@ class UpdatePasswordViewModelTest {
Mockito.verify(updatePasswordUiStateObserver)
.onChanged(Mockito.any(RegistrationUiState.Error::class.java))
Mockito.verifyNoMoreInteractions(updatePasswordUiStateObserver)
Dispatchers.resetMain()
}

@After
Expand Down

0 comments on commit d120e10

Please sign in to comment.