-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ test(api): add post recommendation sentence
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...drop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |