11package com.github.yeoli.devlog.domain.memo.service
22
33import com.github.yeoli.devlog.domain.memo.domain.Memo
4+ import com.github.yeoli.devlog.domain.memo.repository.MemoRepository
45import com.intellij.mock.MockFileDocumentManagerImpl
56import com.intellij.openapi.application.ApplicationManager
67import com.intellij.openapi.editor.EditorFactory
@@ -10,10 +11,28 @@ import com.intellij.openapi.util.Disposer
1011import com.intellij.testFramework.fixtures.BasePlatformTestCase
1112import com.intellij.testFramework.replaceService
1213import com.intellij.util.Function
14+ import org.mockito.kotlin.mock
15+ import org.mockito.kotlin.whenever
1316import java.awt.Point
17+ import kotlin.test.assertTrue
1418
1519class MemoServiceTest : BasePlatformTestCase () {
1620
21+ private lateinit var memoRepository: MemoRepository
22+
23+ override fun setUp () {
24+ super .setUp()
25+
26+ // mock 생성
27+ memoRepository = mock()
28+
29+ project.replaceService(
30+ MemoRepository ::class .java,
31+ memoRepository,
32+ testRootDisposable
33+ )
34+ }
35+
1736 fun `test 메모 생성 성공` () {
1837 // given
1938 val psiFile = myFixture.configureByText(
@@ -132,7 +151,6 @@ class MemoServiceTest : BasePlatformTestCase() {
132151 val document = editor.document
133152 val snippet = " val selected = 42"
134153 val selectionStart = document.text.indexOf(snippet)
135- assertTrue(" 선택할 코드 스니펫을 찾지 못했습니다." , selectionStart >= 0 )
136154 val selectionEnd = selectionStart + snippet.length
137155 editor.selectionModel.setSelection(selectionStart, selectionEnd)
138156
@@ -167,4 +185,60 @@ class MemoServiceTest : BasePlatformTestCase() {
167185 }
168186 }
169187
188+ // ========= 메모 조회 기능 =========
189+ fun `test 메모 전체 조회 기능 성공` () {
190+ // given
191+ val memo1 = Memo (
192+ id = System .currentTimeMillis(),
193+ createdAt = java.time.LocalDateTime .now(),
194+ updatedAt = java.time.LocalDateTime .now(),
195+ content = " 메모1" ,
196+ commitHash = null ,
197+ filePath = " /path/to/file1" ,
198+ selectedCodeSnippet = " snippet1" ,
199+ selectionStart = 0 ,
200+ selectionEnd = 5 ,
201+ visibleStart = 1 ,
202+ visibleEnd = 3
203+ )
204+
205+ val memo2 = Memo (
206+ id = System .currentTimeMillis() + 1 ,
207+ createdAt = java.time.LocalDateTime .now(),
208+ updatedAt = java.time.LocalDateTime .now(),
209+ content = " 메모2" ,
210+ commitHash = null ,
211+ filePath = " /path/to/file2" ,
212+ selectedCodeSnippet = " snippet2" ,
213+ selectionStart = 10 ,
214+ selectionEnd = 20 ,
215+ visibleStart = 4 ,
216+ visibleEnd = 10
217+ )
218+
219+ whenever(memoRepository.getAll()).thenReturn(listOf (memo1, memo2))
220+
221+ // when
222+ val result = MemoService (project).getAllMemos()
223+
224+ // then
225+ assertEquals(2 , result.size)
226+ assertEquals(" 메모1" , result[0 ].content)
227+ assertEquals(" 메모2" , result[1 ].content)
228+ assertEquals(" /path/to/file1" , result[0 ].filePath)
229+ assertEquals(" /path/to/file2" , result[1 ].filePath)
230+ }
231+
232+ fun `test 메모 전체 조회 기능 실패 - 예외 발생시 빈 리스트` () {
233+ // given
234+ whenever(memoRepository.getAll()).thenThrow(RuntimeException (" DB error" ))
235+
236+ // when
237+ val result = MemoService (project).getAllMemos()
238+
239+ // then
240+ assertTrue(result.isEmpty(), " 예외 발생 시 빈 리스트를 반환해야 합니다." )
241+ }
242+
170243}
244+
0 commit comments