Skip to content

Commit

Permalink
#23 CheckSyncStateUseCaseTest.kt 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kimny927 committed Jan 29, 2025
1 parent f8b5977 commit c0f3c47
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
11 changes: 11 additions & 0 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ kotlin {
}
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter()
}
}
}

dependencies {
implementation(libs.javax.inject)
implementation(libs.kotlinx.coroutine.core)
implementation(kotlin("stdlib"))
testImplementation(libs.junit.jupiter)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.kotlinx.coroutines.test)
}
81 changes: 81 additions & 0 deletions domain/src/test/kotlin/CheckSyncStateUseCaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import kotlinx.coroutines.test.runTest
import ny.photomap.domain.PhotoRepository
import ny.photomap.domain.Result
import ny.photomap.domain.usecase.CheckSyncStateUseCase
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever

class CheckSyncStateUseCaseTest {

private val repository: PhotoRepository = mock()


private lateinit var useCase: CheckSyncStateUseCase

@BeforeEach
fun setUp() {
useCase = CheckSyncStateUseCase(
repository = repository
)
}

@Test
@DisplayName("한 번도 싱크 업데이트한 기록이 없을 때의 테스트")
fun neverHaveUpdated() = runTest {
whenever(repository.getLatestUpdateTime()).thenReturn(Result.Success(0L))

val result = useCase()

assertTrue(result is Result.Success)
assertEquals(true, (result as Result.Success).data.shouldSync)
assertEquals(0L, result.data.lastSyncTime)
}

@Test
@DisplayName("아직 업데이트할 기간이 아닐 경우")
fun doNotHaveToUpdated() = runTest {
val mockTime = 1738135677000
whenever(repository.getLatestUpdateTime()).thenReturn(Result.Success(mockTime))
whenever(repository.isSyncExpired(7)).thenReturn(Result.Success(false))

val result = useCase()

assertTrue(result is Result.Success)
assertEquals(false, (result as Result.Success).data.shouldSync)
assertEquals(mockTime, result.data.lastSyncTime)
}

@Test
@DisplayName("업데이트 기간이 지난 경우")
fun shouldUpdated() = runTest {
val mockTime = 1738135677000
whenever(repository.getLatestUpdateTime()).thenReturn(Result.Success(mockTime))
whenever(repository.isSyncExpired(7)).thenReturn(Result.Success(true))

val result = useCase()

assertTrue(result is Result.Success)
assertEquals(true, (result as Result.Success).data.shouldSync)
assertEquals(mockTime, result.data.lastSyncTime)
}

@Test
@DisplayName("최신 업데이트 기간 조회 때 에러 발생 테스트")
fun errorThrown() = runTest {
whenever(repository.getLatestUpdateTime()).thenReturn(Result.Failure(Throwable("테스트용 발생 Throwable")))
whenever(repository.isSyncExpired(7)).thenReturn(Result.Failure(Throwable("테스트용 발생 Throwable")))

val result = useCase()

assertTrue(result is Result.Success)
assertEquals(false, (result as Result.Success).data.shouldSync)
assertEquals(null, result.data.lastSyncTime)
}


}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ navigationCompose = "2.8.5"
hiltNavigationCompose = "1.2.0"
playServicesLocation = "21.3.0"
timber = "5.0.1"
junitJupiter = "5.11.4"


[libraries]
Expand Down Expand Up @@ -85,6 +86,7 @@ hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-com
play-services-location = { group = "com.google.android.gms", name = "play-services-location", version.ref = "playServicesLocation" }
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }
kotlinx-coroutines-play-services = { group="org.jetbrains.kotlinx", name="kotlinx-coroutines-play-services", version.ref="kotlinxCoroutines" }
junit-jupiter = { group="org.junit.jupiter", name="junit-jupiter", version.ref="junitJupiter"}

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit c0f3c47

Please sign in to comment.