Skip to content

Commit

Permalink
test: 단위테스트 수정 (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
choidongkuen committed Feb 13, 2024
1 parent c054ef7 commit 970f8c9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.teumteum.user.domain.request.UserRegisterRequest;
import net.teumteum.user.domain.request.UserWithdrawRequest;
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.service.UserService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -238,27 +239,25 @@ void Register_reviews_with_400_bad_request() throws Exception {
class Get_user_reviews_api_unit {

@Test
@DisplayName("로그인한 회원 id 에 해당하는 회원 리뷰와 200 OK을 반환한다.")
@DisplayName("user id 에 해당하는 회원 리뷰와 200 OK을 반환한다.")
void Get_user_reviews_with_200_ok() throws Exception {
// given
var userId = 1L;

given(securityService.getCurrentUserId()).willReturn(userId);

given(userService.getUserReviews(anyLong()))
.willReturn(List.of(new UserReviewsResponse(별로에요, 2L),
new UserReviewsResponse(최고에요, 3L)));
.willReturn(UserReviewsResponse.of(List.of(new UserReviewResponse(별로에요, 2L),
new UserReviewResponse(최고에요, 3L))));

// when & then
mockMvc.perform(get("/users/reviews")
mockMvc.perform(get("/users/{userId}/reviews", 1)
.with(csrf())
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].count", is(2)))
.andExpect(jsonPath("$[0].review").value("별로에요"));
.andExpect(jsonPath("$.reviews", notNullValue()))
.andExpect(jsonPath("$.reviews", hasSize(2)))
.andExpect(jsonPath("$.reviews[0].count", is(2)))
.andExpect(jsonPath("$.reviews[0].review").value("별로에요"));
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/test/java/net/teumteum/unit/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.teumteum.user.domain.request.UserRegisterRequest;
import net.teumteum.user.domain.request.UserWithdrawRequest;
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import net.teumteum.user.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -251,23 +251,24 @@ class Get_user_reviews_api_unit {
void Return_user_reviews_with_200_ok() {
// given
var userId = 1L;
var existUser = UserFixture.getIdUser();

var response = List.of(new UserReviewsResponse(최고에요, 2L)
, new UserReviewsResponse(별로에요, 3L));
var response = List.of(new UserReviewResponse(최고에요, 2L)
, new UserReviewResponse(별로에요, 3L));

given(userRepository.countUserReviewsByUserId(anyLong())).willReturn(response);
given(userRepository.findById(anyLong())).willReturn(Optional.of(existUser));
given(userRepository.countUserReviewsByUser(any(User.class))).willReturn(response);

// when
var result = userService.getUserReviews(userId);

// then
assertThat(result).hasSize(2);
assertThat(result.get(0).review()).isEqualTo(최고에요);
assertThat(result.get(0).count()).isEqualTo(2L);
assertThat(result.get(1).review()).isEqualTo(별로에요);
assertThat(result.get(1).count()).isEqualTo(3L);
assertThat(result.reviews()).hasSize(2);
assertThat(result.reviews().get(0).review()).isEqualTo(최고에요);
assertThat(result.reviews().get(0).count()).isEqualTo(2L);
assertThat(result.reviews().get(1).review()).isEqualTo(별로에요);

verify(userRepository, times(1)).countUserReviewsByUserId(anyLong());
verify(userRepository, times(1)).countUserReviewsByUser(any(User.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jakarta.persistence.EntityManager;
import java.util.Optional;
import net.teumteum.core.config.AppConfig;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UserReviewResponse;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -85,13 +85,14 @@ void Count_user_reviews_by_user_id() {
entityManager.clear();

// when
var result = userRepository.countUserReviewsByUserId(id);
var result = userRepository.countUserReviewsByUser(existUser);

// then
Assertions.assertThat(result)
.isNotEmpty()
.hasSize(3)
.extracting(UserReviewsResponse::review, UserReviewsResponse::count)
.extracting(UserReviewResponse::review,
UserReviewResponse::count)
.contains(
Assertions.tuple(최고에요, 3L),
Assertions.tuple(별로에요, 1L),
Expand Down

0 comments on commit 970f8c9

Please sign in to comment.