Skip to content

Commit

Permalink
Setup test and write sample test
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuwu committed Oct 6, 2023
1 parent 55970c0 commit 126afc3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ dependencies {
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")
implementation "com.github.bumptech.glide:compose:1.0.0-alpha.1"
implementation "androidx.compose.material3:material3:1.1.1"
testImplementation("org.mockito:mockito-core:2.1.0")

debugImplementation "androidx.compose.ui:ui-tooling:1.4.2"
implementation "androidx.compose.ui:ui-tooling-preview:1.4.2"
Expand All @@ -125,6 +126,7 @@ dependencies {
implementation "androidx.compose.material:material-icons-extended:1.0.0"

implementation libs.caruilib
testImplementation "org.mockito.kotlin:mockito-kotlin:5.1.0"

testImplementation libs.junit
androidTestImplementation libs.androidx.test.runner
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.UserRepository
import com.hieuwu.groceriesstore.domain.models.UserModel
import com.hieuwu.groceriesstore.domain.usecases.GetProfileUseCase
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNotNull
import junit.framework.TestCase.assertNull
import junit.framework.TestCase.assertTrue
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 GetProfileUseCaseImplTest {

@Mock
lateinit var mockedUserRepository: UserRepository

private lateinit var testee: GetProfileUseCase

@Before
fun setUp() {
testee = GetProfileUseCaseImpl(
userRepository = mockedUserRepository
)
}

@Test
fun givenUserAvailable_whenExecute_thenReturnCorrectValue() {
whenever(mockedUserRepository.getCurrentUser()).thenReturn(flow {
UserModel(
id = "",
name = "",
email = "",
phone = "",
address = "",
isOrderCreatedNotiEnabled = true,
isPromotionNotiEnabled = true,
isDataRefreshedNotiEnabled = true
)
})
runBlocking {
val result = testee.execute(GetProfileUseCase.Input())

result.result.collect {
assertEquals("", it?.id)
assertTrue(it!!.isDataRefreshedNotiEnabled)
assertTrue(it.isOrderCreatedNotiEnabled)
assertTrue(it.isPromotionNotiEnabled)
}
}
}


@Test
fun givenUserUnavailable_whenExecute_thenReturnCorrectValue() {
whenever(mockedUserRepository.getCurrentUser()).thenReturn(flow {
null
})

runBlocking {
val result = testee.execute(GetProfileUseCase.Input())
result.result.collect {
assertNull(it)
}
}
}

}

0 comments on commit 126afc3

Please sign in to comment.