-
-
Notifications
You must be signed in to change notification settings - Fork 55
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 #214 from Yashraj254/add_test_case_for_GetProducts…
…ListUseCase test case added for GetProductsListUseCase
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...est/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetProductsListUseCaseImplTest.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.GetProductsListUseCase | ||
import junit.framework.TestCase.assertEquals | ||
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 GetProductsListUseCaseImplTest { | ||
|
||
@Mock | ||
lateinit var mockedProductsRepository: ProductRepository | ||
|
||
private lateinit var testee: GetProductsListUseCase | ||
|
||
@Before | ||
fun setUp() { | ||
testee = GetProductsListUseCaseImpl( | ||
productRepository = mockedProductsRepository | ||
) | ||
} | ||
|
||
@Test | ||
fun givenProductsAvailable_whenExecute_thenReturnCorrectValue() { | ||
val mockedProducts = listOf( | ||
ProductModel(id = "1", name = "Apple", price = 1.0, image = "image1.jpg", description = "Fruit", nutrition = "Healthy"), | ||
ProductModel(id = "2", name = "Fries", price = 5.0, image = "image2.jpg", description = "Junk food", nutrition = "Unhealthy"), | ||
ProductModel(id = "3", name = "Potato", price = 10.0, image = "image3.jpg", description = "Vegetable", nutrition = "Healthy"), | ||
) | ||
whenever(mockedProductsRepository.products).thenReturn(flow { | ||
emit(mockedProducts) | ||
}) | ||
runBlocking { | ||
val result = testee.execute(GetProductsListUseCase.Input()) | ||
|
||
result.result.collect { products -> | ||
assertEquals(mockedProducts[0], products[0]) | ||
assertEquals(mockedProducts[1], products[1]) | ||
assertEquals(mockedProducts[2], products[2]) | ||
assertEquals(mockedProducts[1].name, "Fries") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun givenProductsUnavailable_whenExecute_thenReturnCorrectValue() { | ||
whenever(mockedProductsRepository.products).thenReturn(flow { | ||
emit(listOf()) | ||
}) | ||
|
||
runBlocking { | ||
val result = testee.execute(GetProductsListUseCase.Input()) | ||
result.result.collect { | ||
assertEquals(it.isEmpty(), true) | ||
} | ||
} | ||
} | ||
|
||
} |