-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from ishanvaghani/unittest/usecases
Unittest/usecases
- Loading branch information
Showing
10 changed files
with
415 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...st/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetProductDetailUseCaseImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.ProductRepository | ||
import com.hieuwu.groceriesstore.domain.models.ProductModel | ||
import com.hieuwu.groceriesstore.domain.usecases.GetProductDetailUseCase | ||
import junit.framework.TestCase | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.whenever | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class GetProductDetailUseCaseImplTest { | ||
|
||
@Mock | ||
lateinit var mockedProductRepository: ProductRepository | ||
|
||
private lateinit var testee: GetProductDetailUseCase | ||
|
||
@Before | ||
fun setup() { | ||
testee = GetProductDetailUseCaseImpl(productRepository = mockedProductRepository) | ||
} | ||
|
||
@Test | ||
fun productDetailsAvailable_whenExecuted_thenReturnCorrectValue() { | ||
val input = "1" | ||
val mockProduct = ProductModel("1") | ||
|
||
whenever(mockedProductRepository.getProductById(input)).thenReturn(flow { | ||
emit(mockProduct) | ||
}) | ||
|
||
runBlocking { | ||
val result = testee.getProductDetail(input) | ||
result.collect { | ||
TestCase.assertEquals(mockProduct, it) | ||
} | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...va/com/hieuwu/groceriesstore/domain/usecases/impl/GetProductsByCategoryUseCaseImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.ProductRepository | ||
import com.hieuwu.groceriesstore.domain.models.ProductModel | ||
import com.hieuwu.groceriesstore.domain.usecases.GetProductsByCategoryUseCase | ||
import junit.framework.TestCase | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.whenever | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class GetProductsByCategoryUseCaseImplTest { | ||
|
||
@Mock | ||
lateinit var mockedProductRepository: ProductRepository | ||
|
||
private lateinit var testee: GetProductsByCategoryUseCaseImpl | ||
|
||
@Before | ||
fun setup() { | ||
testee = GetProductsByCategoryUseCaseImpl(productRepository = mockedProductRepository) | ||
} | ||
|
||
@Test | ||
fun productsAvailable_whenExecute_thenReturnCorrectValue() { | ||
val input = GetProductsByCategoryUseCase.Input("20") | ||
val mockedProducts = listOf(ProductModel("1"), ProductModel("2")) | ||
|
||
whenever(mockedProductRepository.getAllProductsByCategory(input.categoryId)).thenReturn(flow { | ||
emit(mockedProducts) | ||
}) | ||
|
||
runBlocking { | ||
val result = testee.execute(input) | ||
result.result.collect { | ||
TestCase.assertTrue(it.isNotEmpty()) | ||
TestCase.assertEquals(mockedProducts[0], it[0]) | ||
TestCase.assertEquals(mockedProducts[0].id, it[0].id) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun productsNotAvailable_whenExecute_thenReturnCorrectValue() { | ||
val input = GetProductsByCategoryUseCase.Input("20") | ||
val mockedProducts = listOf<ProductModel>() | ||
|
||
whenever(mockedProductRepository.getAllProductsByCategory(input.categoryId)).thenReturn(flow { | ||
emit(mockedProducts) | ||
}) | ||
|
||
runBlocking { | ||
val result = testee.execute(input) | ||
result.result.collect { | ||
TestCase.assertTrue(it.isEmpty()) | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...test/java/com/hieuwu/groceriesstore/domain/usecases/impl/RefreshAppDataUseCaseImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.CategoryRepository | ||
import com.hieuwu.groceriesstore.data.repository.ProductRepository | ||
import com.hieuwu.groceriesstore.data.repository.RecipeRepository | ||
import com.hieuwu.groceriesstore.domain.usecases.RefreshAppDataUseCase | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.verify | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class RefreshAppDataUseCaseImplTest { | ||
|
||
@Mock | ||
lateinit var mockedProductRepository: ProductRepository | ||
|
||
@Mock | ||
lateinit var mockedCategoryRepository: CategoryRepository | ||
|
||
@Mock | ||
lateinit var mockedRecipeRepository: RecipeRepository | ||
|
||
private lateinit var testee: RefreshAppDataUseCase | ||
|
||
@Before | ||
fun setup() { | ||
testee = RefreshAppDataUseCaseImpl( | ||
productRepository = mockedProductRepository, | ||
categoryRepository = mockedCategoryRepository, | ||
recipeRepository = mockedRecipeRepository, | ||
ioDispatcher = Dispatchers.IO | ||
) | ||
} | ||
|
||
@Test | ||
fun whenExecute_thenCallRepositories() { | ||
runBlocking { | ||
testee.execute(Unit) | ||
|
||
verify(mockedProductRepository).refreshDatabase() | ||
verify(mockedCategoryRepository).refreshDatabase() | ||
verify(mockedRecipeRepository).refreshDatabase() | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
.../test/java/com/hieuwu/groceriesstore/domain/usecases/impl/SearchProductUseCaseImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.ProductRepository | ||
import com.hieuwu.groceriesstore.domain.models.ProductModel | ||
import com.hieuwu.groceriesstore.domain.usecases.SearchProductUseCase | ||
import junit.framework.TestCase | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.whenever | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class SearchProductUseCaseImplTest { | ||
|
||
@Mock | ||
lateinit var mockedProductRepository: ProductRepository | ||
|
||
private lateinit var testee: SearchProductUseCase | ||
|
||
@Before | ||
fun setup() { | ||
testee = SearchProductUseCaseImpl( | ||
productRepository = mockedProductRepository, | ||
ioDispatcher = Dispatchers.IO | ||
) | ||
} | ||
|
||
@Test | ||
fun productsAvailable_whenExecute_thenReturnCorrectValue() { | ||
val input = SearchProductUseCase.Input("abc") | ||
val mockProducts = listOf(ProductModel("1"), ProductModel("2")) | ||
|
||
whenever(mockedProductRepository.searchProductsListByName(input.name)).thenReturn(flow { | ||
emit(mockProducts) | ||
}) | ||
|
||
runBlocking { | ||
val result = testee.execute(input) | ||
result.result.collect { | ||
TestCase.assertTrue(it.isNotEmpty()) | ||
TestCase.assertEquals(mockProducts[0], it[0]) | ||
TestCase.assertEquals(mockProducts[1].id, it[1].id) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun productsNotAvailable_whenExecute_thenReturnCorrectValue() { | ||
val input = SearchProductUseCase.Input("abc") | ||
val mockProducts = listOf<ProductModel>() | ||
|
||
whenever(mockedProductRepository.searchProductsListByName(input.name)).thenReturn(flow { | ||
emit(mockProducts) | ||
}) | ||
|
||
runBlocking { | ||
val result = testee.execute(input) | ||
result.result.collect { | ||
TestCase.assertTrue(it.isEmpty()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/SignOutUseCaseImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.hieuwu.groceriesstore.domain.usecases.impl | ||
|
||
import com.hieuwu.groceriesstore.data.repository.UserRepository | ||
import com.hieuwu.groceriesstore.domain.usecases.SignOutUseCase | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.verify | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class SignOutUseCaseImplTest { | ||
|
||
@Mock | ||
lateinit var mockedUserRepository: UserRepository | ||
private lateinit var testee: SignOutUseCase | ||
|
||
@Before | ||
fun setup() { | ||
testee = SignOutUseCaseImpl( | ||
userRepository = mockedUserRepository, | ||
ioDispatcher = Dispatchers.IO | ||
) | ||
} | ||
|
||
@Test | ||
fun whenExecute_thenCallUserRepository() { | ||
val input = SignOutUseCase.Input() | ||
|
||
runBlocking { | ||
testee.execute(input) | ||
|
||
verify(mockedUserRepository).clearUser() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.