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

domain layer 테스트 코드 임시로 작성 #99

Merged
merged 5 commits into from
Dec 16, 2022
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 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