From f73d910022d07256621c3269ee4bf6c4e443c84c Mon Sep 17 00:00:00 2001 From: stopkite Date: Mon, 6 Feb 2023 19:33:13 +0900 Subject: [PATCH 1/9] =?UTF-8?q?feat:=20mockk=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=88=98=EC=97=85=20=EB=82=B4=EC=9A=A9=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/presentation/sample/TodoManager.kt | 8 +- .../presentation/sample/TodoManagerTest.kt | 114 ++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt diff --git a/app/src/main/java/com/malibin/study/github/presentation/sample/TodoManager.kt b/app/src/main/java/com/malibin/study/github/presentation/sample/TodoManager.kt index 835a922..799ef19 100644 --- a/app/src/main/java/com/malibin/study/github/presentation/sample/TodoManager.kt +++ b/app/src/main/java/com/malibin/study/github/presentation/sample/TodoManager.kt @@ -7,20 +7,20 @@ class TodoManager( ) { private val currentTodos = mutableListOf() - fun getTodos(): List { + suspend fun getTodos(): List { return currentTodos.toList() } - fun getTodoHistories(): List { + suspend fun getTodoHistories(): List { return todoMemory.getHistory() } - fun createTodo(todo: String) { + suspend fun createTodo(todo: String) { currentTodos.add(todo) todoMemory.create(todo) } - fun finishTodo(todo: String) { + suspend fun finishTodo(todo: String) { currentTodos.remove(todo) todoMemory.finish(todo) } diff --git a/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt b/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt new file mode 100644 index 0000000..53cb14c --- /dev/null +++ b/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt @@ -0,0 +1,114 @@ +package com.malibin.study.github.presentation.sample + +import com.google.common.truth.Truth.assertThat +import io.mockk.* +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +/** + * 안에 있는 구현들을 전부 다 아는 테스트 -> 화이트 박스 테스트 + */ +internal class TodoManagerTest { + + @Test + fun ddd() { + // given + // 가짜 객체를 만든다 -> mock + // 그래서 라이브러리 이름도 mockk : 순수 코틀린으로 이루어짐 + val todoMemory = mockk() + + // 모든 todoMemory는 ~을 반환한다 + every { todoMemory.getHistory() } returns listOf("todo1", "todo2") + + + // 어떤 기능을 했는지 내부적으로 구현도 할 수 있다 + // + + /*val fakeTodoMemory = object : TodoMemory { + + private val list = mutableListOf() + + override fun getHistory(): List { + return list + } + + override fun create(todo: String) { + this.list.add(todo) + } + + override fun finish(todo: String) { + + } + }*/ + + //val todoManager = TodoManager(fakeTodoMemory) + //todoManager.createTodo("todo1") + //todoManager.createTodo("todo2") + + val todoManager = TodoManager(todoMemory) + + // when + val actualHistories = todoManager.getTodoHistories() + + // then + assertThat(actualHistories.size).isEqualTo(2) + assertThat(actualHistories) // 똑같은 값들이 들어 있음을 확인 + .containsExactlyElementsIn(listOf("todo1", "todo2")) + .inOrder() + + } + + /** createTodo 할 때 어떤 동작을 해야하는지 mockk에게 알려주어야 한다 + * 하지만 relaxed = true 키워드를 쓰면 안해도 됨! + * */ + @Test + fun ddd3() { + // given + val todoMemory = mockk(relaxed = true) + + val todoManager = TodoManager(todoMemory) + +// every { todoMemory.create("2") } throws java.lang.IllegalArgumentException("") +// every { todoMemory.create("1") } just Runs + + todoManager.createTodo("1") + todoManager.createTodo("2") // 여기서 throw + + // when + val actualTodos = todoManager.getTodos() + + // then + assertAll( + { assertThat(actualTodos).isEqualTo(listOf("1", "2")) }, + { verify(exactly = 1) { todoMemory.create("1") } }, // 1을 넣은 것이 한 번 발생했다 + { verify(exactly = 1) { todoMemory.create(any()) } } // 무적 any !! + ) + } + + + /** 코루틴을 쓴다고 가정해보자 + * suspend 는 코루틴 scope 내에서 실행을 해줘야한다 + * every는 코루틴 scope가 아니기 때문에 못쓴다 ㅠㅠ + * 그러기 때문에 co를 붙이자! coEvery + * */ + @Test + fun ddd5() = runBlocking { + // given + val todoMemory = mockk() + + coEvery { todoMemory.getHistory() } returns listOf("todo1", "todo2") + + val todoManager = TodoManager(todoMemory) + + // when + val actualHistories = todoManager.getTodoHistories() + + // then + assertThat(actualHistories.size).isEqualTo(2) + assertThat(actualHistories) // 똑같은 값들이 들어 있음을 확인 + .containsExactlyElementsIn(listOf("todo1", "todo2")) + .inOrder() + + } +} \ No newline at end of file From d19d1206124e52a1f921be4d98589d014a915169 Mon Sep 17 00:00:00 2001 From: stopkite Date: Mon, 6 Feb 2023 20:28:57 +0900 Subject: [PATCH 2/9] =?UTF-8?q?feat:=20mockk=20ViewModel=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=97=85=20=EB=82=B4=EC=9A=A9=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/sample/TestViewModel.kt | 74 ++++++++++++++++ .../presentation/sample/TestViewModelTest.kt | 86 +++++++++++++++++++ .../presentation/sample/TodoManagerTest.kt | 12 +-- 3 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/malibin/study/github/presentation/sample/TestViewModel.kt create mode 100644 app/src/test/java/com/malibin/study/github/presentation/sample/TestViewModelTest.kt diff --git a/app/src/main/java/com/malibin/study/github/presentation/sample/TestViewModel.kt b/app/src/main/java/com/malibin/study/github/presentation/sample/TestViewModel.kt new file mode 100644 index 0000000..a81449b --- /dev/null +++ b/app/src/main/java/com/malibin/study/github/presentation/sample/TestViewModel.kt @@ -0,0 +1,74 @@ +package com.malibin.study.github.presentation.sample + +import androidx.lifecycle.* +import kotlinx.coroutines.launch + +interface CountMemory { + fun save(history: String) + fun getHistories(): List +} + +class TestViewModel( + private val countMemory: CountMemory, +) : ViewModel() { + + private val _count = MutableLiveData(0) + val count: LiveData = _count + + private val _histories = MutableLiveData>() + val histories: LiveData> = _histories + + fun increase() { + _count.value = _count.value?.plus(1) + countMemory.save("increase") + } + + fun decrease() { + _count.value = _count.value?.minus(1) + countMemory.save("decrease") + } + + fun loadHistories() { + _histories.value = countMemory.getHistories() + } +} + + +//======================================================================================= + + +interface CountMemoryWithCoroutine { + suspend fun save(history: String) + suspend fun getHistories(): List +} + +class TestViewModelWithCoroutine( + private val countMemory: CountMemoryWithCoroutine, +) : ViewModel() { + + private val _count = MutableLiveData(0) + val count: LiveData = _count + + private val _histories = MutableLiveData>() + val histories: LiveData> = _histories + + fun increase() { + viewModelScope.launch { + _count.value = _count.value?.plus(1) + countMemory.save("increase") + } + } + + fun decrease() { + viewModelScope.launch { + _count.value = _count.value?.minus(1) + countMemory.save("decrease") + } + } + + fun loadHistories() { + viewModelScope.launch { + _histories.value = countMemory.getHistories() + } + } +} \ No newline at end of file diff --git a/app/src/test/java/com/malibin/study/github/presentation/sample/TestViewModelTest.kt b/app/src/test/java/com/malibin/study/github/presentation/sample/TestViewModelTest.kt new file mode 100644 index 0000000..d900c72 --- /dev/null +++ b/app/src/test/java/com/malibin/study/github/presentation/sample/TestViewModelTest.kt @@ -0,0 +1,86 @@ +package com.malibin.study.github.presentation.sample + +import com.google.common.truth.Truth.assertThat +import com.malibin.study.github.utils.InstantTaskExecutorExtension +import com.malibin.study.github.utils.MainCoroutineExtension +import com.malibin.study.github.utils.getOrAwaitValue +import io.mockk.coVerify +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import org.junit.jupiter.api.extension.RegisterExtension + +internal class TestViewModelTest { + + companion object { + @JvmStatic + @RegisterExtension + val instantTaskExecutorExtension = InstantTaskExecutorExtension() + } + + private lateinit var countMemory: CountMemory + private lateinit var testViewModel: TestViewModel + + @BeforeEach + fun setUp() { + countMemory = mockk(relaxed = true) + testViewModel = TestViewModel(countMemory) + } + + @Test + fun test1() { + // when + testViewModel.increase() + + // then + assertAll( + { assertThat(testViewModel.count.value).isEqualTo(1) }, + { verify(exactly = 1) { countMemory.save("increase") } } // increase 가 한 번 호출되었다 + ) + + } + + +} + +@OptIn(ExperimentalCoroutinesApi::class) +internal class TestViewModelWithCoroutineTest { + + companion object { + @JvmStatic + @RegisterExtension + val instantTaskExecutorExtension = InstantTaskExecutorExtension() + + @JvmStatic + @RegisterExtension + val mce = MainCoroutineExtension() + } + + private lateinit var countMemory: CountMemoryWithCoroutine + private lateinit var testViewModel: TestViewModelWithCoroutine + + @BeforeEach + fun setUp() { + countMemory = mockk(relaxed = true) + testViewModel = TestViewModelWithCoroutine(countMemory) + } + + @Test + fun test1() = runBlocking { + // when + testViewModel.increase() + + // then + assertAll( + { assertThat(testViewModel.count.getOrAwaitValue()).isEqualTo(1) }, + { coVerify(exactly = 1) { countMemory.save("increase") } } // increase 가 한 번 호출되었다 + ) + + } + + +} \ No newline at end of file diff --git a/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt b/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt index 53cb14c..c6ebebe 100644 --- a/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt +++ b/app/src/test/java/com/malibin/study/github/presentation/sample/TodoManagerTest.kt @@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test */ internal class TodoManagerTest { - @Test + /*@Test fun ddd() { // given // 가짜 객체를 만든다 -> mock @@ -25,7 +25,7 @@ internal class TodoManagerTest { // 어떤 기능을 했는지 내부적으로 구현도 할 수 있다 // - /*val fakeTodoMemory = object : TodoMemory { + val fakeTodoMemory = object : TodoMemory { private val list = mutableListOf() @@ -40,7 +40,7 @@ internal class TodoManagerTest { override fun finish(todo: String) { } - }*/ + } //val todoManager = TodoManager(fakeTodoMemory) //todoManager.createTodo("todo1") @@ -57,7 +57,7 @@ internal class TodoManagerTest { .containsExactlyElementsIn(listOf("todo1", "todo2")) .inOrder() - } + }*/ /** createTodo 할 때 어떤 동작을 해야하는지 mockk에게 알려주어야 한다 * 하지만 relaxed = true 키워드를 쓰면 안해도 됨! @@ -65,7 +65,7 @@ internal class TodoManagerTest { @Test fun ddd3() { // given - val todoMemory = mockk(relaxed = true) + /*val todoMemory = mockk(relaxed = true) val todoManager = TodoManager(todoMemory) @@ -83,7 +83,7 @@ internal class TodoManagerTest { { assertThat(actualTodos).isEqualTo(listOf("1", "2")) }, { verify(exactly = 1) { todoMemory.create("1") } }, // 1을 넣은 것이 한 번 발생했다 { verify(exactly = 1) { todoMemory.create(any()) } } // 무적 any !! - ) + )*/ } From 2c67049b2d88296594ebc0781a28554bcaad285a Mon Sep 17 00:00:00 2001 From: stopkite Date: Mon, 6 Feb 2023 20:54:44 +0900 Subject: [PATCH 3/9] =?UTF-8?q?docs:=20step1=20readme=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++++++ .../data/repository/DefaultGithubProfileRepositoryTest.kt | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 README.md create mode 100644 app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt diff --git a/README.md b/README.md new file mode 100644 index 0000000..6ecdf50 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# 2주차 미션 + +## 🎲 1단계 +### GithubProfileRepository ++ 로컬에 유저 정보가 이미 존재할 때 유저 정보를 반환할 수 있다 ++ 로컬에 유저 정보가 없을 때 서버 통신을 시도할 수 있다 ++ 서버 통신을 시도한 후에 로컬에 유저 정보를 저장할 수 있다 \ No newline at end of file diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt new file mode 100644 index 0000000..b52e0ef --- /dev/null +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -0,0 +1,7 @@ +package com.malibin.study.github.data.repository + +import org.junit.jupiter.api.Assertions.* + +internal class DefaultGithubProfileRepositoryTest { + +} \ No newline at end of file From 374f8f59d9ef2473b40de455444f32e5dc0ac269 Mon Sep 17 00:00:00 2001 From: stopkite Date: Tue, 7 Feb 2023 02:58:51 +0900 Subject: [PATCH 4/9] =?UTF-8?q?test:=20[GithubProfileRepository]=20?= =?UTF-8?q?=EB=A1=9C=EC=BB=AC=EC=97=90=20=EC=9C=A0=EC=A0=80=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=EA=B0=80=20=EC=9E=88=EC=9D=8C=EC=9D=84=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../DefaultGithubProfileRepositoryTest.kt | 44 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ecdf50..36eb270 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ## 🎲 1단계 ### GithubProfileRepository ++ 로컬에 유저 정보가 있음을 확인할 수 있다 + 로컬에 유저 정보가 이미 존재할 때 유저 정보를 반환할 수 있다 + 로컬에 유저 정보가 없을 때 서버 통신을 시도할 수 있다 + 서버 통신을 시도한 후에 로컬에 유저 정보를 저장할 수 있다 \ No newline at end of file diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt index b52e0ef..0a22150 100644 --- a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -1,7 +1,49 @@ package com.malibin.study.github.data.repository -import org.junit.jupiter.api.Assertions.* +import com.google.common.truth.Truth.assertThat +import com.malibin.study.github.data.source.GithubProfileSource +import com.malibin.study.github.domain.profile.GithubProfile +import com.malibin.study.github.utils.InstantTaskExecutorExtension +import io.mockk.coEvery +import io.mockk.mockk +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension internal class DefaultGithubProfileRepositoryTest { + private lateinit var fakeLocalSource: GithubProfileSource + private lateinit var fakeRemoteSource: GithubProfileSource + + companion object { + @JvmStatic + @RegisterExtension + val instantTaskExecutorExtension = InstantTaskExecutorExtension() + } + + @BeforeEach + fun setUp() { + fakeLocalSource = mockk(relaxed = true) + fakeRemoteSource = mockk(relaxed = true) + } + + @Test + fun `로컬에 유저 정보가 있음을 확인할 수 있다`() = runBlocking { + // given + val expectedSearch = fakeLocalSource.getGithubProfile("stopkite").isSuccess + + coEvery { + fakeLocalSource.getGithubProfile("stopkite").isSuccess + } returns true + + // when + val actualSearch = DefaultGithubProfileRepository( + fakeLocalSource, + fakeRemoteSource + ).getGithubProfile("stopkite").isSuccess + + // then + assertThat(actualSearch).isEqualTo(expectedSearch) + } } \ No newline at end of file From 7d56fd430778a882d7347b5dd819450d186e082f Mon Sep 17 00:00:00 2001 From: stopkite Date: Tue, 7 Feb 2023 02:59:11 +0900 Subject: [PATCH 5/9] =?UTF-8?q?test:=20[GithubProfileRepository]=20?= =?UTF-8?q?=EB=A1=9C=EC=BB=AC=EC=97=90=20=EC=9C=A0=EC=A0=80=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=EA=B0=80=20=EC=9D=B4=EB=AF=B8=20=EC=A1=B4=EC=9E=AC?= =?UTF-8?q?=ED=95=A0=20=EB=95=8C=20=EC=9C=A0=EC=A0=80=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultGithubProfileRepositoryTest.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt index 0a22150..99be08d 100644 --- a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -46,4 +46,27 @@ internal class DefaultGithubProfileRepositoryTest { // then assertThat(actualSearch).isEqualTo(expectedSearch) } + + @Test + fun `로컬에 유저 정보가 이미 존재할 때 유저 정보를 반환할 수 있다`() = runBlocking { + // given + val githubProfile = + GithubProfile( + 0, + "stopkite", + "https://avatars.githubusercontent.com/u/62979643?v=4", + "Ji-Yeon", + "1", + 10, + 100 + ) + + coEvery { fakeLocalSource.getGithubProfile("stopkite") } returns runCatching { githubProfile } + + // when + val actualLocalProfile = fakeLocalSource.getGithubProfile("stopkite").getOrThrow() + + // then + assertThat(actualLocalProfile).isEqualTo(githubProfile) + } } \ No newline at end of file From a87ea2553ce04615b378de55858517d842c17763 Mon Sep 17 00:00:00 2001 From: stopkite Date: Tue, 7 Feb 2023 04:12:19 +0900 Subject: [PATCH 6/9] =?UTF-8?q?test:=20[GithubProfileRepository]=20?= =?UTF-8?q?=EB=A1=9C=EC=BB=AC=EC=97=90=20=EC=9C=A0=EC=A0=80=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=EA=B0=80=20=EC=97=86=EC=9D=8C=EC=9D=84=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- .../DefaultGithubProfileRepositoryTest.kt | 44 +++++++++++-------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 36eb270..751020b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## 🎲 1단계 ### GithubProfileRepository -+ 로컬에 유저 정보가 있음을 확인할 수 있다 ++ 로컬에 유저 정보가 있음을 확인할 수 있다 ++ 로컬에 유저 정보가 없음을 확인할 수 있다 + 로컬에 유저 정보가 이미 존재할 때 유저 정보를 반환할 수 있다 -+ 로컬에 유저 정보가 없을 때 서버 통신을 시도할 수 있다 + 서버 통신을 시도한 후에 로컬에 유저 정보를 저장할 수 있다 \ No newline at end of file diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt index 99be08d..c9e4adf 100644 --- a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -3,6 +3,7 @@ package com.malibin.study.github.data.repository import com.google.common.truth.Truth.assertThat import com.malibin.study.github.data.source.GithubProfileSource import com.malibin.study.github.domain.profile.GithubProfile +import com.malibin.study.github.domain.repository.GithubProfileRepository import com.malibin.study.github.utils.InstantTaskExecutorExtension import io.mockk.coEvery import io.mockk.mockk @@ -31,35 +32,39 @@ internal class DefaultGithubProfileRepositoryTest { @Test fun `로컬에 유저 정보가 있음을 확인할 수 있다`() = runBlocking { // given - val expectedSearch = fakeLocalSource.getGithubProfile("stopkite").isSuccess - - coEvery { - fakeLocalSource.getGithubProfile("stopkite").isSuccess - } returns true + val expectedSearch = fakeLocalSource.getGithubProfile("stopkite") // when - val actualSearch = DefaultGithubProfileRepository( - fakeLocalSource, - fakeRemoteSource - ).getGithubProfile("stopkite").isSuccess + val actualSearch = fakeLocalSource.getGithubProfile("stopkite") // then assertThat(actualSearch).isEqualTo(expectedSearch) } + @Test + fun `로컬에 유저 정보가 없음을 확인할 수 있다`() = runBlocking { + // given + val expectedSearch = fakeLocalSource.getGithubProfile("stopkite") + + // when + val actualSearch = fakeLocalSource.getGithubProfile("malibinYun") + + // then + assertThat(actualSearch).isNotEqualTo(expectedSearch) + } + @Test fun `로컬에 유저 정보가 이미 존재할 때 유저 정보를 반환할 수 있다`() = runBlocking { // given - val githubProfile = - GithubProfile( - 0, - "stopkite", - "https://avatars.githubusercontent.com/u/62979643?v=4", - "Ji-Yeon", - "1", - 10, - 100 - ) + val githubProfile = GithubProfile( + 0, + "stopkite", + "https://avatars.githubusercontent.com/u/62979643?v=4", + "Ji-Yeon", + "1", + 10, + 100 + ) coEvery { fakeLocalSource.getGithubProfile("stopkite") } returns runCatching { githubProfile } @@ -69,4 +74,5 @@ internal class DefaultGithubProfileRepositoryTest { // then assertThat(actualLocalProfile).isEqualTo(githubProfile) } + } \ No newline at end of file From e26d4a086699718f4e6804f1c99d9dd8c92239ea Mon Sep 17 00:00:00 2001 From: stopkite Date: Tue, 7 Feb 2023 04:57:45 +0900 Subject: [PATCH 7/9] =?UTF-8?q?test:=20[GithubProfileRepository]=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20=EC=A0=95=EB=B3=B4=EA=B0=80=20=EC=97=86?= =?UTF-8?q?=EC=9D=84=20=EB=95=8C=20=EC=84=9C=EB=B2=84=EB=A1=9C=EB=B6=80?= =?UTF-8?q?=ED=84=B0=20=EC=9C=A0=EC=A0=80=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=9B=EC=95=84=EC=98=A4=EB=8A=94=20=EA=B2=83=EC=9D=84=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../DefaultGithubProfileRepositoryTest.kt | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 751020b..0787711 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ + 로컬에 유저 정보가 있음을 확인할 수 있다 + 로컬에 유저 정보가 없음을 확인할 수 있다 + 로컬에 유저 정보가 이미 존재할 때 유저 정보를 반환할 수 있다 -+ 서버 통신을 시도한 후에 로컬에 유저 정보를 저장할 수 있다 \ No newline at end of file ++ 유저 정보가 없을 때 서버로부터 유저 정보를 받아오는 것을 확인할 수 있다 \ No newline at end of file diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt index c9e4adf..cf20346 100644 --- a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -5,6 +5,7 @@ import com.malibin.study.github.data.source.GithubProfileSource import com.malibin.study.github.domain.profile.GithubProfile import com.malibin.study.github.domain.repository.GithubProfileRepository import com.malibin.study.github.utils.InstantTaskExecutorExtension +import io.mockk.Runs import io.mockk.coEvery import io.mockk.mockk import kotlinx.coroutines.runBlocking @@ -75,4 +76,28 @@ internal class DefaultGithubProfileRepositoryTest { assertThat(actualLocalProfile).isEqualTo(githubProfile) } + @Test + fun `유저 정보가 없을 때 서버로부터 유저 정보를 받아오는 것을 확인할 수 있다`() = runBlocking { + // given + val expectedGithubProfile = GithubProfile( + 0, + "stopkite", + "https://avatars.githubusercontent.com/u/62979643?v=4", + "Ji-Yeon", + "1", + 10, + 100 + ) + + coEvery { + fakeRemoteSource.getGithubProfile("stopkite") + } returns runCatching { expectedGithubProfile } + + // when + val actualSavedLocalProfile = fakeRemoteSource.getGithubProfile("stopkite").getOrThrow() + + // then + assertThat(actualSavedLocalProfile).isEqualTo(expectedGithubProfile) + } + } \ No newline at end of file From 198c0fb45903c7f58e5760c08d85f4ac43ff9106 Mon Sep 17 00:00:00 2001 From: stopkite Date: Tue, 7 Feb 2023 05:23:25 +0900 Subject: [PATCH 8/9] =?UTF-8?q?fix:=20[GithubProfileRepository]=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20=EC=B0=BE=EA=B8=B0=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultGithubProfileRepositoryTest.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt index cf20346..9ff3a60 100644 --- a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -33,25 +33,29 @@ internal class DefaultGithubProfileRepositoryTest { @Test fun `로컬에 유저 정보가 있음을 확인할 수 있다`() = runBlocking { // given - val expectedSearch = fakeLocalSource.getGithubProfile("stopkite") + val expectedName = "stopkite" + + coEvery { fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName } returns "stopkite" // when - val actualSearch = fakeLocalSource.getGithubProfile("stopkite") + val actualSearch = fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName // then - assertThat(actualSearch).isEqualTo(expectedSearch) + assertThat(actualSearch).isEqualTo(expectedName) } @Test fun `로컬에 유저 정보가 없음을 확인할 수 있다`() = runBlocking { // given - val expectedSearch = fakeLocalSource.getGithubProfile("stopkite") + val expectedName = "stopkite" + + coEvery { fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName } returns "malibin" // when - val actualSearch = fakeLocalSource.getGithubProfile("malibinYun") + val actualSearch = fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName // then - assertThat(actualSearch).isNotEqualTo(expectedSearch) + assertThat(actualSearch).isNotEqualTo(expectedName) } @Test From b865223c0e6d75ac9dff296cc0c202ba75d41947 Mon Sep 17 00:00:00 2001 From: stopkite Date: Tue, 7 Feb 2023 05:25:49 +0900 Subject: [PATCH 9/9] =?UTF-8?q?fix:=20[GithubProfileRepository]=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20=EC=97=86=EC=9D=84=20=EB=95=8C=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/repository/DefaultGithubProfileRepositoryTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt index 9ff3a60..2ec2c92 100644 --- a/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt +++ b/app/src/test/java/com/malibin/study/github/data/repository/DefaultGithubProfileRepositoryTest.kt @@ -49,7 +49,7 @@ internal class DefaultGithubProfileRepositoryTest { // given val expectedName = "stopkite" - coEvery { fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName } returns "malibin" + coEvery { fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName } returns "" // when val actualSearch = fakeLocalSource.getGithubProfile("stopkite").getOrThrow().userName