From 7c1376b554b1e3f0eaab8a5586044a0f024fa9d0 Mon Sep 17 00:00:00 2001 From: patthhar Date: Fri, 6 Oct 2023 23:50:52 +0530 Subject: [PATCH 1/3] Unit test for GetCategoriesListUseCase done --- .../impl/GetCategoriesListUseCaseImplTest.kt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt diff --git a/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt new file mode 100644 index 00000000..c8de395a --- /dev/null +++ b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt @@ -0,0 +1,69 @@ +package com.hieuwu.groceriesstore.domain.usecases.impl + +import com.hieuwu.groceriesstore.data.repository.CategoryRepository +import com.hieuwu.groceriesstore.domain.models.CategoryModel +import com.hieuwu.groceriesstore.domain.usecases.GetCategoriesListUseCase +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertNull +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 GetCategoriesListUseCaseImplTest { + + @Mock + lateinit var mockedCategoryRepository: CategoryRepository + + private lateinit var testee: GetCategoriesListUseCaseImpl + + @Before + fun setUp() { + testee = GetCategoriesListUseCaseImpl( + categoryRepository = mockedCategoryRepository + ) + } + + @Test + fun givenCategoriesAvailable_whenExecute_thenReturnCorrectValue() { + val mockCategories = listOf( + CategoryModel(id = "1", name = "Category 1", image = "image1.jpg"), + CategoryModel(id = "2", name = "Category 2", image = "image2.jpg"), + CategoryModel(id = "3", name = "Category 3", image = "image3.jpg"), + CategoryModel(id = "4", name = "Category 4", image = "image4.jpg") + ) + whenever(mockedCategoryRepository.getFromLocal()).thenReturn(flow { + emit(mockCategories) + }) + runBlocking { + val result = testee.execute(GetCategoriesListUseCase.Input()) + + result.result.collect { categories -> + assertEquals(mockCategories[0], categories[0]) + assertEquals(mockCategories[1], categories[1]) + assertEquals(mockCategories[2], categories[2]) + assertEquals(mockCategories[1].name, "Category 2") + assertEquals(mockCategories[3].image, "image4.jpg") + } + } + } + + @Test + fun givenCategoriesUnavailable_whenExecute_thenReturnCorrectValue() { + whenever(mockedCategoryRepository.getFromLocal()).thenReturn(flow { + null + }) + runBlocking { + val result = testee.execute(GetCategoriesListUseCase.Input()) + result.result.collect { + assertNull(it) + } + } + } +} \ No newline at end of file From 810a21897da8a9e5b9afed63d40345b42af39755 Mon Sep 17 00:00:00 2001 From: patthhar Date: Fri, 6 Oct 2023 23:55:38 +0530 Subject: [PATCH 2/3] Updated test for when categories aren't available --- .idea/gradle.xml | 1 + .../domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index dd415e7e..cd342bb1 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -13,6 +13,7 @@ + diff --git a/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt index c8de395a..7e844fdf 100644 --- a/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt +++ b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCategoriesListUseCaseImplTest.kt @@ -4,7 +4,6 @@ import com.hieuwu.groceriesstore.data.repository.CategoryRepository import com.hieuwu.groceriesstore.domain.models.CategoryModel import com.hieuwu.groceriesstore.domain.usecases.GetCategoriesListUseCase import junit.framework.TestCase.assertEquals -import junit.framework.TestCase.assertNull import kotlinx.coroutines.flow.flow import kotlinx.coroutines.runBlocking import org.junit.Before @@ -57,12 +56,12 @@ class GetCategoriesListUseCaseImplTest { @Test fun givenCategoriesUnavailable_whenExecute_thenReturnCorrectValue() { whenever(mockedCategoryRepository.getFromLocal()).thenReturn(flow { - null + emit(listOf()) }) runBlocking { val result = testee.execute(GetCategoriesListUseCase.Input()) result.result.collect { - assertNull(it) + assertEquals(it.isEmpty(), true) } } } From b0d9ed34b2b043aa77eeef8ce1de316377bb3522 Mon Sep 17 00:00:00 2001 From: patthhar Date: Sat, 7 Oct 2023 10:00:19 +0530 Subject: [PATCH 3/3] Unit test for GetCurrentCartUseCase done --- .../impl/GetCurrentCartUseCaseImplTest.kt | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCurrentCartUseCaseImplTest.kt diff --git a/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCurrentCartUseCaseImplTest.kt b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCurrentCartUseCaseImplTest.kt new file mode 100644 index 00000000..2eda357e --- /dev/null +++ b/app/src/test/java/com/hieuwu/groceriesstore/domain/usecases/impl/GetCurrentCartUseCaseImplTest.kt @@ -0,0 +1,71 @@ +package com.hieuwu.groceriesstore.domain.usecases.impl + +import com.hieuwu.groceriesstore.data.repository.OrderRepository +import com.hieuwu.groceriesstore.domain.models.OrderModel +import com.hieuwu.groceriesstore.domain.usecases.GetCurrentCartUseCase +import com.hieuwu.groceriesstore.utilities.OrderStatus +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertNull +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 GetCurrentCartUseCaseImplTest { + + @Mock + lateinit var mockedOrderRepository: OrderRepository + + private lateinit var testee: GetCurrentCartUseCaseImpl + + @Before + fun setUp() { + testee = GetCurrentCartUseCaseImpl( + orderRepository = mockedOrderRepository + ) + } + + @Test + fun givenCartNotEmpty_whenExecute_thenReturnOrdersInCart() { + val mockOrder = OrderModel( + id = "", + status = OrderStatus.IN_CART.value, + address = null, + lineItemList = mutableListOf(), + createdAt = "" + ) + whenever(mockedOrderRepository.getOneOrderByStatus(OrderStatus.IN_CART)).thenReturn(flow { + emit(mockOrder) + }) + runBlocking { + val result = testee.execute(GetCurrentCartUseCase.Input()) + + result.result.collect { order -> + assertEquals(order?.id, "") + assertEquals(order?.status, "cart") + assertEquals(order?.address, null) + assertEquals(order?.lineItemList!!.isEmpty(), true) + assertEquals(order.createdAt, "") + } + } + } + + @Test + fun givenCartEmpty_whenExecute_thenReturnNull() { + whenever(mockedOrderRepository.getOneOrderByStatus(OrderStatus.IN_CART)).thenReturn(flow { + null + }) + runBlocking { + val result = testee.execute(GetCurrentCartUseCase.Input()) + result.result.collect { + assertNull(it) + } + } + } +} \ No newline at end of file