Skip to content

Commit

Permalink
feat: tests for viewmodels and repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 committed Aug 17, 2023
1 parent cc3f8c6 commit 312046c
Show file tree
Hide file tree
Showing 6 changed files with 615 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package org.mifos.mobile.repositories

import io.reactivex.Observable
import org.junit.Assert

import CoroutineTestRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.client.ClientAccounts
import org.mifos.mobile.utils.Constants
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner
import java.io.IOException

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

@get:Rule
val coroutineTestRule = CoroutineTestRule()

@Mock
lateinit var dataManager: DataManager

private lateinit var accountsRepositoryImp : AccountsRepository
private lateinit var accountsRepositoryImp: AccountsRepository

@Before
fun setUp() {
Expand All @@ -29,59 +36,31 @@ class AccountsRepositoryImpTest {
}

@Test
fun testLoadClientAccounts_SuccessResponseReceivedFromDataManager_ReturnsSuccess() {
val mockClientAccounts : ClientAccounts? = Mockito.mock(ClientAccounts::class.java)
val successResponse = Observable.just(mockClientAccounts)
Mockito.`when`(
dataManager.clientAccounts
).thenReturn(successResponse)
fun loadAccounts_Success() = runBlocking {
val mockAccountType = "savings"
val mockClientAccounts = mock(ClientAccounts::class.java)
`when`(dataManager.getAccounts(mockAccountType)).thenReturn((mockClientAccounts))

val result = accountsRepositoryImp.loadClientAccounts()
val resultFlow = accountsRepositoryImp.loadAccounts(mockAccountType)
val resultAccounts = resultFlow.first()

Mockito.verify(dataManager).clientAccounts
Assert.assertEquals(result, successResponse)
assert(resultAccounts == mockClientAccounts)
}

@Test
fun testLoadClientAccounts_ErrorResponseReceivedFromDataManager_ReturnsError() {
val errorResponse : Observable<ClientAccounts?> = Observable.error(Throwable("loading client error"))
Mockito.`when`(
dataManager.clientAccounts
).thenReturn(errorResponse)

val result = accountsRepositoryImp.loadClientAccounts()

Mockito.verify(dataManager).clientAccounts
Assert.assertEquals(result, errorResponse)
fun loadAccounts_Error() = runBlocking {
val mockAccountType = "savings"
val mockError = IOException("Network error")
`when`(dataManager.getAccounts(mockAccountType)).thenThrow(mockError)

val resultFlow = accountsRepositoryImp.loadAccounts(mockAccountType)
var isErrorThrown = false
try {
resultFlow.first()
} catch (e: Exception) {
isErrorThrown = true
assert(e is IOException)
}
assert(isErrorThrown)
}

@Test
fun testLoadAccounts_SuccessResponseReceivedFromDataManager_ReturnsSuccess() {
val mockClientAccounts : ClientAccounts? = Mockito.mock(ClientAccounts::class.java)
val successResponse = Observable.just(mockClientAccounts)
val mockAccountType = Constants.SAVINGS_ACCOUNTS
Mockito.`when`(
dataManager.getAccounts(mockAccountType)
).thenReturn(successResponse)

val result = accountsRepositoryImp.loadAccounts(mockAccountType)

Mockito.verify(dataManager).getAccounts(mockAccountType)
Assert.assertEquals(result, successResponse)
}

@Test
fun testLoadAccounts_ErrorResponseReceivedFromDataManager_ReturnsError() {
val errorResponse : Observable<ClientAccounts?> = Observable.error(Throwable("loading client error"))
val mockAccountType = Constants.SAVINGS_ACCOUNTS
Mockito.`when`(
dataManager.getAccounts(mockAccountType)
).thenReturn(errorResponse)

val result = accountsRepositoryImp.loadAccounts(mockAccountType)

Mockito.verify(dataManager).getAccounts(mockAccountType)
Assert.assertEquals(result, errorResponse)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package org.mifos.mobile.repositories

import CoroutineTestRule
import junit.framework.Assert.assertEquals
import junit.framework.Assert.fail
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import okhttp3.ResponseBody
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mifos.mobile.api.DataManager
import org.mifos.mobile.models.client.Client
import org.mifos.mobile.models.client.ClientAccounts
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.MockitoAnnotations
import org.mockito.junit.MockitoJUnitRunner

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

@get:Rule
val coroutineTestRule = CoroutineTestRule()

@Mock
lateinit var dataManager: DataManager

private lateinit var homeRepositoryImp: HomeRepositoryImp

@Before
fun setUp() {
MockitoAnnotations.openMocks(this)
homeRepositoryImp = HomeRepositoryImp(dataManager)
}

@Test
fun testClientAccounts_Successful() = runBlocking {
val mockClientAccounts: ClientAccounts = mock(ClientAccounts::class.java)

`when`(dataManager.clientAccounts()).thenReturn(mockClientAccounts)

val flow = homeRepositoryImp.clientAccounts()

flow.collect { result ->
assertEquals(mockClientAccounts, result)
}
}

@Test
fun testCurrentClient_Successful() = runBlocking {
val mockClient: Client = mock(Client::class.java)

`when`(dataManager.currentClient()).thenReturn(mockClient)

val flow = homeRepositoryImp.currentClient()

flow.collect { result ->
assertEquals(mockClient, result)
}
}

@Test
fun testClientImage_Successful() = runBlocking {
val mockResponseBody: ResponseBody = mock(ResponseBody::class.java)

`when`(dataManager.clientImage()).thenReturn(mockResponseBody)

val flow = homeRepositoryImp.clientImage()

flow.collect { result ->
assertEquals(mockResponseBody, result)
}
}

@Test
fun testUnreadNotificationsCount_Successful() = runBlocking {
val mockUnreadCount = 5

`when`(dataManager.unreadNotificationsCount()).thenReturn(mockUnreadCount)

val flow = homeRepositoryImp.unreadNotificationsCount()

flow.collect { result ->
assertEquals(mockUnreadCount, result)
}
}

@Test
fun testClientAccounts_Error() = runBlocking {
val errorMessage = "Failed to fetch client accounts"
val mockErrorResponse: ClientAccounts = mock(ClientAccounts::class.java)

`when`(dataManager.clientAccounts()).thenReturn(mockErrorResponse)

val flow = homeRepositoryImp.clientAccounts()

try {
flow.collect {
fail("Expected an exception")
}
} catch (e: Exception) {
assertEquals(errorMessage, e.message)
}
}

@Test
fun testCurrentClient_Error() = runBlocking {
val errorMessage = "Failed to fetch current client"
val mockErrorResponse: Client = mock(Client::class.java)

`when`(dataManager.currentClient()).thenReturn(mockErrorResponse)

val flow = homeRepositoryImp.currentClient()

try {
flow.collect {
fail("Expected an exception")
}
} catch (e: Exception) {
assertEquals(errorMessage, e.message)
}
}

@Test
fun testClientImage_Error() = runBlocking {
val errorMessage = "Failed to fetch client image"
val mockErrorResponse: ResponseBody = mock(ResponseBody::class.java)

`when`(dataManager.clientImage()).thenReturn(mockErrorResponse)

val flow = homeRepositoryImp.clientImage()

try {
flow.collect {
fail("Expected an exception")
}
} catch (e: Exception) {
assertEquals(errorMessage, e.message)
}
}

@Test
fun testUnreadNotificationsCount_Error() = runBlocking {
val errorMessage = "Failed to fetch unread notifications count"

`when`(dataManager.unreadNotificationsCount()).thenReturn(502)

val flow = homeRepositoryImp.unreadNotificationsCount()

try {
flow.collect {
fail("Expected an exception")
}
} catch (e: Exception) {
assertEquals(errorMessage, e.message)
}
}
}
Loading

0 comments on commit 312046c

Please sign in to comment.