Skip to content

Commit

Permalink
Merge pull request #4135 from abelgardep/technical/replace_runBlocking
Browse files Browse the repository at this point in the history
Replace runBlocking with runTest for testing
  • Loading branch information
JuancaG05 authored Sep 4, 2023
2 parents 02a321e + b1456e9 commit b6982af
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
1 change: 1 addition & 0 deletions owncloudData/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
testImplementation project(":owncloudTestUtil")
testImplementation libs.androidx.arch.core.testing
testImplementation libs.junit4
testImplementation libs.kotlinx.coroutines.test
testImplementation libs.mockk

// Dependencies for instrumented tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import com.owncloud.android.testutil.OC_APP_REGISTRY_MIMETYPE
import io.mockk.every
import io.mockk.mockkClass
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@ExperimentalCoroutinesApi
class OCLocalAppRegistryDataSourceTest {
private lateinit var ocLocalAppRegistryDataSource: OCLocalAppRegistryDataSource
private val appRegistryDao = mockkClass(AppRegistryDao::class)
Expand Down Expand Up @@ -55,7 +57,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test
fun `getAppRegistryForMimeTypeAsStream returns a flow with AppRegistryMimeType object`() = runBlocking {
fun `getAppRegistryForMimeTypeAsStream returns a flow with AppRegistryMimeType object`() = runTest {

every { appRegistryDao.getAppRegistryForMimeType(any(), any()) } returns flowOf(ocAppRegistryEntity)

Expand All @@ -69,7 +71,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test
fun `getAppRegistryForMimeTypeAsStream returns null when DAO no receive values from db`() = runBlocking {
fun `getAppRegistryForMimeTypeAsStream returns null when DAO no receive values from db`() = runTest {

every { appRegistryDao.getAppRegistryForMimeType(any(), any()) } returns flowOf(null)

Expand All @@ -82,7 +84,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test(expected = Exception::class)
fun `getAppRegistryForMimeTypeAsStream returns an Exception when DAO return an Exception`() = runBlocking {
fun `getAppRegistryForMimeTypeAsStream returns an Exception when DAO return an Exception`() = runTest {

every { appRegistryDao.getAppRegistryForMimeType(any(), any()) } throws Exception()

Expand All @@ -95,7 +97,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test
fun `getAppRegistryWhichAllowCreation returns a flow with a list of AppRegistryMimeType object`() = runBlocking {
fun `getAppRegistryWhichAllowCreation returns a flow with a list of AppRegistryMimeType object`() = runTest {

every { appRegistryDao.getAppRegistryWhichAllowCreation(any()) } returns flowOf(listOf(ocAppRegistryEntity))

Expand All @@ -109,7 +111,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test
fun `getAppRegistryWhichAllowCreation returns empty list when DAO return empty list`() = runBlocking {
fun `getAppRegistryWhichAllowCreation returns empty list when DAO return empty list`() = runTest {

every { appRegistryDao.getAppRegistryWhichAllowCreation(any()) } returns flowOf(emptyList<AppRegistryEntity>())

Expand All @@ -123,7 +125,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test
fun `saveAppRegistryForAccount should save the AppRegistry entities`() = runBlocking {
fun `saveAppRegistryForAccount should save the AppRegistry entities`() = runTest {
val appRegistry = AppRegistry(
OC_ACCOUNT_NAME, mutableListOf(
AppRegistryMimeType("mime_type_1", "ext_1", emptyList(), "name_1", "icon_1", "description_1", true, "default_app_1"),
Expand All @@ -141,7 +143,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test(expected = Exception::class)
fun `saveAppRegistryForAccount should returns an Exception`() = runBlocking {
fun `saveAppRegistryForAccount should returns an Exception`() = runTest {
val appRegistry = AppRegistry(
OC_ACCOUNT_NAME, mutableListOf(
AppRegistryMimeType("mime_type_1", "ext_1", emptyList(), "name_1", "icon_1", "description_1", true, "default_app_1"),
Expand All @@ -159,7 +161,7 @@ class OCLocalAppRegistryDataSourceTest {
}

@Test
fun `deleteAppRegistryForAccount should delete appRegistry`() = runBlocking {
fun `deleteAppRegistryForAccount should delete appRegistry`() = runTest {

every { appRegistryDao.deleteAppRegistryForAccount(OC_ACCOUNT_NAME) } returns Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import java.util.UUID

@ExperimentalCoroutinesApi
class OCLocalFileDataSourceTest {
private lateinit var localDataSource: OCLocalFileDataSource
private lateinit var dao: FileDao
Expand Down Expand Up @@ -98,7 +100,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getFileByIdAsFlow returns a flow of OCFile`() = runBlocking {
fun `getFileByIdAsFlow returns a flow of OCFile`() = runTest {
every { dao.getFileByIdAsFlow(any()) } returns flowOf(DUMMY_FILE_ENTITY)

val result = localDataSource.getFileByIdAsFlow(OC_FILE.id!!)
Expand All @@ -111,7 +113,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getFileByIdAsFlow returns null`() = runBlocking {
fun `getFileByIdAsFlow returns null`() = runTest {
every { dao.getFileByIdAsFlow(any()) } returns flowOf(null)

val result = localDataSource.getFileByIdAsFlow(DUMMY_FILE_ENTITY.id)
Expand All @@ -131,7 +133,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getFileWithSyncInfoByIdAsFlow returns a flow of OCFileWithSyncInfo object`() = runBlocking {
fun `getFileWithSyncInfoByIdAsFlow returns a flow of OCFileWithSyncInfo object`() = runTest {

every { dao.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!) } returns flowOf(ocFileAndFileSync)

Expand All @@ -145,7 +147,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getFileWithSyncInfoByIdAsFlow returns null when DAO is null`() = runBlocking {
fun `getFileWithSyncInfoByIdAsFlow returns null when DAO is null`() = runTest {

every { dao.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!) } returns flowOf(null)

Expand All @@ -159,7 +161,7 @@ class OCLocalFileDataSourceTest {
}

@Test(expected = Exception::class)
fun `getFileWithSyncInfoByIdAsFlow returns an exception when DAO receive a Exception`() = runBlocking {
fun `getFileWithSyncInfoByIdAsFlow returns an exception when DAO receive a Exception`() = runTest {

every { dao.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!) } throws Exception()

Expand Down Expand Up @@ -343,7 +345,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getFolderContentWithSyncInfoAsFlow returns a flow of list OcFileAndFileSync`() = runBlocking {
fun `getFolderContentWithSyncInfoAsFlow returns a flow of list OcFileAndFileSync`() = runTest {

every { dao.getFolderContentWithSyncInfoAsFlow(any()) } returns flowOf(listOf(ocFileAndFileSync))

Expand Down Expand Up @@ -388,7 +390,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getSharedByLinkWithSyncInfoForAccountAsFlow returns a flow of list of OCFileWithSyncInfo`() = runBlocking {
fun `getSharedByLinkWithSyncInfoForAccountAsFlow returns a flow of list of OCFileWithSyncInfo`() = runTest {
every { dao.getFilesWithSyncInfoSharedByLinkAsFlow(any()) } returns flowOf(listOf(ocFileAndFileSync))

val result = localDataSource.getSharedByLinkWithSyncInfoForAccountAsFlow(DUMMY_FILE_ENTITY.owner)
Expand All @@ -412,7 +414,7 @@ class OCLocalFileDataSourceTest {
}

@Test
fun `getFilesWithSyncInfoAvailableOfflineFromAccountAsFlow returns a flow of list of OCFileWithSyncInfo`() = runBlocking {
fun `getFilesWithSyncInfoAvailableOfflineFromAccountAsFlow returns a flow of list of OCFileWithSyncInfo`() = runTest {
every { dao.getFilesWithSyncInfoAvailableOfflineFromAccountAsFlow(any()) } returns flowOf(listOf(ocFileAndFileSync))

val result = localDataSource.getFilesWithSyncInfoAvailableOfflineFromAccountAsFlow(DUMMY_FILE_ENTITY.owner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ import com.owncloud.android.testutil.OC_FOLDER
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Ignore
import org.junit.Test

@Ignore("Ignore temporary, pretty dependant on implementation... Will be reworked when finished")
@ExperimentalCoroutinesApi
class OCFileRepositoryTest {

private val remoteFileDataSource = mockk<RemoteFileDataSource>(relaxed = true)
Expand Down Expand Up @@ -104,7 +106,7 @@ class OCFileRepositoryTest {
}

@Test
fun `getFileWithSyncInfoByIdAsFlow returns OCFileWithSyncInfo`() = runBlocking {
fun `getFileWithSyncInfoByIdAsFlow returns OCFileWithSyncInfo`() = runTest {
every { localFileDataSource.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!) } returns flowOf(OC_FILE_WITH_SYNC_INFO_AND_SPACE)

val ocFile = ocFileRepository.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!)
Expand All @@ -119,7 +121,7 @@ class OCFileRepositoryTest {
}

@Test
fun `getFileWithSyncInfoByIdAsFlow returns null`() = runBlocking {
fun `getFileWithSyncInfoByIdAsFlow returns null`() = runTest {
every { localFileDataSource.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!) } returns flowOf(null)

val ocFile = ocFileRepository.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!)
Expand All @@ -135,7 +137,7 @@ class OCFileRepositoryTest {


@Test(expected = Exception::class)
fun `getFileWithSyncInfoByIdAsFlow returns an exception`() = runBlocking {
fun `getFileWithSyncInfoByIdAsFlow returns an exception`() = runTest {
every { localFileDataSource.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!) } throws Exception()

ocFileRepository.getFileWithSyncInfoByIdAsFlow(OC_FILE.id!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import com.owncloud.android.testutil.OC_FILE_WITH_SYNC_INFO_AND_SPACE
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Test

@ExperimentalCoroutinesApi
class GetFileWithSyncInfoByIdUseCaseTest {

private val repository: FileRepository = spyk()
private val useCase = GetFileWithSyncInfoByIdUseCase(repository)
private val useCaseParams = GetFileWithSyncInfoByIdUseCase.Params(OC_FILE.id!!)

@Test
fun `get file with sync by id returns OCFileWithSyncInfo when no error`() = runBlocking {
fun `get file with sync by id returns OCFileWithSyncInfo when no error`() = runTest {
every { repository.getFileWithSyncInfoByIdAsFlow(useCaseParams.fileId) } returns flowOf(OC_FILE_WITH_SYNC_INFO_AND_SPACE)

val useCaseResult = useCase.execute(useCaseParams)
Expand All @@ -31,7 +33,7 @@ class GetFileWithSyncInfoByIdUseCaseTest {
}

@Test
fun `get file with sync by id returns true when repository is null`() = runBlocking {
fun `get file with sync by id returns true when repository is null`() = runTest {
val useCaseResult = useCase.execute(useCaseParams)

every { repository.getFileWithSyncInfoByIdAsFlow(useCaseParams.fileId) } returns flowOf(null)
Expand All @@ -41,7 +43,7 @@ class GetFileWithSyncInfoByIdUseCaseTest {
}

@Test(expected = Exception::class)
fun `get file with sync by id returns an exception`() = runBlocking {
fun `get file with sync by id returns an exception`() = runTest {
every { repository.getFileWithSyncInfoByIdAsFlow(useCaseParams.fileId) } throws Exception()

useCase.execute(useCaseParams)
Expand Down

0 comments on commit b6982af

Please sign in to comment.