Skip to content

Commit

Permalink
๐Ÿ”€ #99 from boostcampwm-2022/test/domain
Browse files Browse the repository at this point in the history
domain layer ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž„์‹œ๋กœ ์ž‘์„ฑ
  • Loading branch information
soopeach authored Dec 16, 2022
2 parents 67269b8 + 3652ec7 commit 37561b9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ buildscript {
hiltCompilerVersion = "1.0.0"
crashlyticsVersion = "2.9.2"
swipeRefreshLayoutVersion = "1.1.0"
mockitoAndroidVersion = "2.24.5"
mockitoInlineVersion = "3.5.13"
}
dependencies {
classpath "com.google.gms:google-services:$googleServiceVersion"
Expand Down
10 changes: 10 additions & 0 deletions domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ dependencies {

// Coroutine
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion"
// coroutineTest
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlinxCoroutinesVersion"

// PagingData - Common
implementation "androidx.paging:paging-common-ktx:$paging3Version"
testImplementation "androidx.paging:paging-common:$paging3Version"

// junit
testImplementation "junit:junit:$junitVersion"

// mockito
testImplementation ("org.mockito:mockito-android:$mockitoAndroidVersion")
// mockito - kotlin
testImplementation "org.mockito:mockito-inline:$mockitoInlineVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.whyranoid.domain.useCase

import com.whyranoid.domain.model.GroupInfo
import com.whyranoid.domain.model.User
import com.whyranoid.domain.repository.GroupRepository
import com.whyranoid.domain.usecase.GetGroupInfoUseCase
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock

class GetGroupInfoUseCaseTest {

@Before
fun setUp() {

mockedGroupRepository = mock(GroupRepository::class.java)

`when`(
mockedGroupRepository.getGroupInfoFlow(
uid = FAKE,
groupId = IS_SUCCESS
)
).thenReturn(flow { emit(MOCKED_GROUP_INFO) })

`when`(
mockedGroupRepository.getGroupInfoFlow(
uid = FAKE,
groupId = IS_FAILURE
)
).thenReturn(flow { })
}

// uid๋Š” ์ƒ๊ด€์—†์ด groupId๊ฐ€ "fake" ์ผ ๋•Œ Leader์˜ name์ด hyunsoo์ธ ๊ทธ๋ฃน์„ flow๋กœ ๋ฐ˜ํ™˜
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun getGroupInfoUseCase_groupIdIsFake_returnGroupInfo() = runTest {
// given
val getGroupInfoUseCase =
GetGroupInfoUseCase(mockedGroupRepository).invoke(uid = FAKE, groupId = IS_SUCCESS)

// when
val groupInfo = getGroupInfoUseCase.firstOrNull()

// then
assertEquals(IS_SUCCESS, (requireNotNull(groupInfo).leader.name))
}

// uid๋Š” ์ƒ๊ด€์—†์ด groupId๊ฐ€ "fake" ๊ฐ€ ์•„๋‹ ๋•Œ ๊ทธ๋ฃน์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun getGroupInfoUseCase_groupIdIsNotFake_returnNothing() = runTest {
// given
val getGroupInfoUseCase =
GetGroupInfoUseCase(mockedGroupRepository).invoke(uid = FAKE, groupId = IS_FAILURE)

// when
val groupInfo = getGroupInfoUseCase.firstOrNull()

// then
assertNull(groupInfo)
}

companion object {

private lateinit var mockedGroupRepository: GroupRepository
private const val FAKE = "fake"
private const val IS_SUCCESS = "isSuccess"
private const val IS_FAILURE = "isFailure"

val MOCKED_GROUP_INFO = GroupInfo(
name = FAKE,
groupId = FAKE,
introduce = FAKE,
rules = emptyList(),
headCount = 0,
leader = User(
uid = FAKE,
name = IS_SUCCESS,
profileUrl = FAKE
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline

0 comments on commit 37561b9

Please sign in to comment.