Skip to content

Commit 830ce61

Browse files
committed
test: badword Test
1 parent 400ce28 commit 830ce61

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/test/java/io/crops/warmletter/domain/badword/controller/BadWordControllerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.boot.test.context.SpringBootTest;
1414
import org.springframework.context.annotation.Import;
1515
import org.springframework.http.MediaType;
16+
import static org.mockito.Mockito.doNothing;
1617

1718
import org.springframework.test.context.ActiveProfiles;
1819
import org.springframework.test.context.bean.override.mockito.MockitoBean;
@@ -139,4 +140,18 @@ void updateBadWord_ReturnsCorrectResponse() throws Exception {
139140
.andExpect(jsonPath("$.message").value("금칙어 변경 성공"));
140141
}
141142

143+
@Test
144+
@DisplayName("금칙어 영구삭제 컨트롤러 테스트 - 성공")
145+
void deleteBadWord_Success() throws Exception {
146+
// 테스트용 금칙어 id
147+
Long badwordId = 1L;
148+
149+
// badWordService.deleteBadWord(badwordId) 호출 시 아무 작업도 하지 않도록 설정
150+
doNothing().when(badWordService).deleteBadWord(badwordId);
151+
152+
// DELETE 요청을 보내고, 응답 상태와 메시지 검증
153+
mockMvc.perform(delete("/api/bad-words/{badwordId}", badwordId))
154+
.andExpect(status().isOk())
155+
.andExpect(jsonPath("$.message").value("금칙어 영구삭제"));
156+
}
142157
}

src/test/java/io/crops/warmletter/domain/badword/service/BadWordServiceTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.crops.warmletter.domain.badword.service;
22

33
import io.crops.warmletter.config.TestConfig;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45
import io.crops.warmletter.domain.badword.dto.request.CreateBadWordRequest;
56
import io.crops.warmletter.domain.badword.dto.request.UpdateBadWordRequest;
67
import io.crops.warmletter.domain.badword.dto.request.UpdateBadWordStatusRequest;
@@ -283,4 +284,31 @@ void updateBadWord_success_withoutRedisUpdate() {
283284
assertEquals("newWord", response.getWord());
284285
}
285286

287+
@Test
288+
@DisplayName("deleteBadWord - 성공 케이스: 존재하는 금칙어 삭제")
289+
void deleteBadWord_success() {
290+
// given
291+
Long badWordId = 1L;
292+
BadWord badWord = new BadWord();
293+
// 필요한 경우 badWord에 속성을 추가할 수 있습니다.
294+
when(badWordRepository.findById(badWordId)).thenReturn(Optional.of(badWord));
295+
296+
// when
297+
badWordService.deleteBadWord(badWordId);
298+
299+
// then: 리포지토리에서 delete() 메서드 호출 및 Redis에서 삭제 호출 검증
300+
verify(badWordRepository).delete(badWord);
301+
verify(hashOperations).delete(BAD_WORD_KEY, badWordId.toString());
302+
}
303+
304+
@Test
305+
@DisplayName("deleteBadWord - 실패 케이스: 존재하지 않는 금칙어 삭제 시 예외 발생")
306+
void deleteBadWord_notFound() {
307+
// given
308+
Long badWordId = 2L;
309+
when(badWordRepository.findById(badWordId)).thenReturn(Optional.empty());
310+
311+
// when & then: BadWordNotFoundException 발생 검증
312+
assertThrows(BadWordNotFoundException.class, () -> badWordService.deleteBadWord(badWordId));
313+
}
286314
}

0 commit comments

Comments
 (0)