Skip to content

Commit

Permalink
✅ test(api): add post recommendation sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
siyeonSon committed Oct 17, 2024
1 parent 6180213 commit 4e0bb03
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package unit.domains.recommend.controller;

import com.depromeet.common.error.dto.CommonErrorCode;
import com.depromeet.common.error.exception.internal.NotFoundException;
import com.depromeet.domains.recommend.controller.PostRecommendController;
import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto;
import com.depromeet.domains.recommend.service.PostRecommendService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ContextConfiguration(classes = PostRecommendController.class)
@WebMvcTest(controllers = {PostRecommendController.class}, excludeAutoConfiguration = {SecurityAutoConfiguration.class})
@Import(PostRecommendController.class)
@DisplayName("[API][Controller] PostRecommendController 테스트")
public class PostRecommendControllerTest {

@Autowired
MockMvc mvc;

@MockBean
PostRecommendService postRecommendService;

@DisplayName("[GET] 홈 화면 드랍 유도 - 무작위 문장 추천")
@Nested
class GetRandomSentenceTest {
@Nested
@DisplayName("성공")
class Success {
@DisplayName("무작위 추천 문장 1개 조회")
@Test
void getOneRandomSentenceSuccess() throws Exception {
var randomSentence = new PostRecommendSentenceResponseDto("random sentence");
when(postRecommendService.getOneRandomSentence()).thenReturn(randomSentence);

var response = mvc.perform(get("/post-recommend/random-sentence"));
response.andExpect(status().isOk())
.andExpect(jsonPath("$.sentence").value("random sentence"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package unit.domains.recommend.service;

import com.depromeet.common.error.exception.internal.NotFoundException;
import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository;
import com.depromeet.domains.recommend.service.PostRecommendService;
import com.depromeet.recommend.post.PostRecommendSentence;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
@DisplayName("[Service] PostRecommendService 테스트")
public class PostRecommendServiceTest {

@InjectMocks
private PostRecommendService postRecommendService;

@Mock
private PostRecommendSentenceRepository postRecommendSentenceRepository;

@DisplayName("무작위 문장 추천")
@Nested
class GetOneRandomSentenceTest {
@Nested
@DisplayName("성공")
class Success {
@DisplayName("무작위 추천 문장 1개 조회")
@Test
void getOneRandomSentenceSuccess() {
List<PostRecommendSentence> sentences = List.of(
new PostRecommendSentence("First sentence"),
new PostRecommendSentence("Second sentence"),
new PostRecommendSentence("Third sentence")
);

given(postRecommendSentenceRepository.findAll())
.willReturn(sentences);
var result = postRecommendService.getOneRandomSentence();

assertThat(result).isNotNull();
assertThat(result.sentence()).isIn(
"First sentence",
"Second sentence",
"Third sentence"
);
}
}

@Nested
@DisplayName("실패")
class Fail {
@DisplayName("저장소에 추천 문장이 없는 경우")
@Test
void getOneRandomSentenceFail() {
given(postRecommendSentenceRepository.findAll())
.willReturn(List.of());

assertThatThrownBy(() -> postRecommendService.getOneRandomSentence())
.isInstanceOf(NotFoundException.class);
}
}
}

}

0 comments on commit 4e0bb03

Please sign in to comment.