Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup test and write sample test #211

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}

}
Loading