-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test : #168 도서별 리뷰 목록 조회 api controller 단위 테스트 작성
- Loading branch information
1 parent
4b5bef0
commit 6eb524b
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/test/java/com/t3t/bookstoreapi/review/controller/ReviewControllerUnitTest.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,76 @@ | ||
package com.t3t.bookstoreapi.review.controller; | ||
|
||
import com.t3t.bookstoreapi.model.response.PageResponse; | ||
import com.t3t.bookstoreapi.review.model.response.ReviewResponse; | ||
import com.t3t.bookstoreapi.review.service.ReviewService; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
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; | ||
|
||
/** | ||
* {@link ReviewController} 클래스의 단위 테스트 | ||
* | ||
* @author Yujin-nKim(김유진) | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@WebMvcTest(value = ReviewController.class) | ||
public class ReviewControllerUnitTest { | ||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private ReviewService reviewService; | ||
|
||
@DisplayName("도서별 리뷰 목록 조회 테스트") | ||
@Test | ||
public void testFindReviewsByBookId() throws Exception { | ||
|
||
Long bookId = 1L; | ||
|
||
PageResponse<ReviewResponse> dummyResponse = PageResponse.<ReviewResponse>builder() | ||
.content(Collections.singletonList(ReviewResponse.builder().name("test").build())) | ||
.pageNo(0) | ||
.pageSize(10) | ||
.totalElements(1) | ||
.totalPages(1) | ||
.last(true) | ||
.build(); | ||
|
||
when(reviewService.findReviewsByBookId(any(), any())).thenReturn(dummyResponse); | ||
|
||
mockMvc.perform(get("/reviews/books/{bookId}", bookId)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.data").exists()); | ||
} | ||
|
||
@DisplayName("도서별 리뷰 목록 조회 테스트 | 리뷰 목록이 없는 경우 204 status code 반환 테스트") | ||
@Test | ||
public void testFindReviewsByBookId_BookNotFound_ReturnsNoContent() throws Exception { | ||
|
||
Long bookId = 1L; | ||
|
||
when(reviewService.findReviewsByBookId(any(), any())).thenReturn( | ||
PageResponse.<ReviewResponse>builder() | ||
.content(Collections.emptyList()) | ||
.pageSize(0) | ||
.totalElements(0) | ||
.totalPages(0) | ||
.last(true) | ||
.build()); | ||
|
||
mockMvc.perform(get("/reviews/books/{bookId}", bookId)) | ||
.andExpect(status().isNoContent()); | ||
} | ||
} |