Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

사용자 관련 테스트 코드 리팩토링 #499

Merged
merged 9 commits into from
Oct 3, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public ReadUserDto updateById(final Long userId, final UpdateUserDto userDto) {
final User user = userRepository.findByIdAndDeletedIsFalse(userId)
.orElseThrow(() -> new UserNotFoundException("사용자 정보를 사용할 수 없습니다."));

updateUserByReqeust(userDto, user);
updateUserByRequest(userDto, user);

return ReadUserDto.from(user);
}

private void updateUserByReqeust(final UpdateUserDto userDto, final User user) {
private void updateUserByRequest(final UpdateUserDto userDto, final User user) {
Comment on lines -34 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

칭찬

오타 발견!! 어떻게 하셨나요!! 대박 뭐가 바뀐건지 한참 봤네요

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?????이거 오타 있었나요?
저 그냥 줄 위치만 변경된줄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

칭찬

아 Reqeust였군요... 엄청납니다
칭찬해요!!!!!!!!!!!!!!!!!!!!!@#!@

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 저지른 오타 같습니다...
인텔리제이가 노란줄 띄워줘서 발견 😂
이것이 인텔리제이주도 개발이다..!

if (userDto.profileImage() != null) {
final StoreImageDto storeImageDto = imageProcessor.storeImageFile(userDto.profileImage());
user.updateProfileImage(storeImageDto.toProfileImageEntity());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.ddang.ddang.user.application;

import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.domain.StoreImageProcessor;
import com.ddang.ddang.image.domain.dto.StoreImageDto;
import com.ddang.ddang.user.application.dto.ReadUserDto;
import com.ddang.ddang.user.application.dto.UpdateUserDto;
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import org.assertj.core.api.SoftAssertions;
import com.ddang.ddang.user.application.fixture.UserServiceFixture;
import org.assertj.core.api.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필수

별을 발견했어요!! 😱😱

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SoftAssertions... 너 나한테 왜그래... 😭

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -25,164 +19,87 @@
@IsolateDatabase
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class UserServiceTest {
class UserServiceTest extends UserServiceFixture {

@Autowired
UserService userService;

@Autowired
JpaUserRepository userRepository;


@MockBean
StoreImageProcessor imageProcessor;

@Test
void 특정_사용자_정보를_조회한다() {
// given
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();

userRepository.save(user);

// when
final ReadUserDto actual = userService.readById(user.getId());
final ReadUserDto actual = userService.readById(사용자.getId());

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual.name()).isEqualTo(user.getName());
softAssertions.assertThat(actual.profileImageId()).isEqualTo(user.getProfileImage().getId());
softAssertions.assertThat(actual.reliability()).isEqualTo(user.getReliability());
softAssertions.assertThat(actual.name()).isEqualTo(사용자.getName());
softAssertions.assertThat(actual.profileImageId()).isEqualTo(사용자.getProfileImage().getId());
softAssertions.assertThat(actual.reliability()).isEqualTo(사용자.getReliability());
});
}

@Test
void 존재하지_않는_사용자_정보_조회시_예외를_반환한다() {
// given
final Long invalidUserId = -999L;

// when & then
assertThatThrownBy(() -> userService.readById(invalidUserId))
assertThatThrownBy(() -> userService.readById(존재하지_않는_사용자_아이디))
.isInstanceOf(UserNotFoundException.class)
.hasMessage("사용자 정보를 사용할 수 없습니다.");
}

@Test
void 사용자_정보를_수정한다() {
// given
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();

userRepository.save(user);

final StoreImageDto storeImageDto = new StoreImageDto("newUpload.png", "newStore.png");
given(imageProcessor.storeImageFile(any())).willReturn(storeImageDto);

final MockMultipartFile updateImage = new MockMultipartFile(
"updateImage.png",
"updateImage.png",
MediaType.IMAGE_PNG.toString(),
new byte[]{1}
);

final UpdateUserDto updateUserDto = new UpdateUserDto("updateName", updateImage);
given(imageProcessor.storeImageFile(any())).willReturn(새로운_프로필_이미지_dto);

// when
userService.updateById(user.getId(), updateUserDto);
userService.updateById(사용자.getId(), 사용자_정보_수정_요청_dto);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(user.getName()).isEqualTo("updateName");
softAssertions.assertThat(user.getProfileImage().getImage().getStoreName()).isEqualTo("newStore.png");
softAssertions.assertThat(user.getReliability()).isEqualTo(4.7d);
softAssertions.assertThat(user.getOauthId()).isEqualTo("12345");
softAssertions.assertThat(사용자.getName()).isEqualTo(사용자_정보_수정_요청_dto.name());
softAssertions.assertThat(사용자.getProfileImage().getImage().getStoreName())
.isEqualTo(새로운_프로필_이미지_dto.storeName());
softAssertions.assertThat(사용자.getProfileImage().getImage().getUploadName())
.isEqualTo(새로운_프로필_이미지_dto.uploadName());
Comment on lines +62 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문

문서화가 아닌 코드에서도 위처럼 개행을 하기로 정해졌었나요? 헷갈려서 질문드립니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분은 아예 정한 기억이 있는지 싶네요.
일단 정한 기억이 없어 원래의 컨벤션인 120자 초과 시 개행에 따랐습니다.

});
}

@Test
void 사용자_정보를_수정시_이름만_수정한다() {
// given
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();

userRepository.save(user);

final UpdateUserDto updateUserDto = new UpdateUserDto("updateName", null);

// when
userService.updateById(user.getId(), updateUserDto);
userService.updateById(사용자.getId(), 사용자_이름만_수정_요청_dto);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(user.getName()).isEqualTo("updateName");
softAssertions.assertThat(user.getProfileImage().getImage().getStoreName()).isEqualTo("store.png");
softAssertions.assertThat(user.getReliability()).isEqualTo(4.7d);
softAssertions.assertThat(user.getOauthId()).isEqualTo("12345");
softAssertions.assertThat(사용자.getName()).isEqualTo(사용자_이름만_수정_요청_dto.name());
softAssertions.assertThat(사용자.getProfileImage()).isEqualTo(프로필_이미지);
});
}

@Test
void 사용자_정보를_수정시_이미지만_수정한다() {
// given
final User user = User.builder()
.name("사용자")
.profileImage(new ProfileImage("upload.png", "store.png"))
.reliability(4.7d)
.oauthId("12345")
.build();

userRepository.save(user);

final StoreImageDto storeImageDto = new StoreImageDto("newUpload.png", "newStore.png");
given(imageProcessor.storeImageFile(any())).willReturn(storeImageDto);

final MockMultipartFile updateImage = new MockMultipartFile(
"updateImage.png",
"updateImage.png",
MediaType.IMAGE_PNG.toString(),
new byte[]{1}
);

final UpdateUserDto updateUserDto = new UpdateUserDto(null, updateImage);
given(imageProcessor.storeImageFile(any())).willReturn(새로운_프로필_이미지_dto);

// when
userService.updateById(user.getId(), updateUserDto);
userService.updateById(사용자.getId(), 사용자_이미지만_수정_요청_dto);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(user.getName()).isEqualTo("사용자");
softAssertions.assertThat(user.getProfileImage().getImage().getStoreName()).isEqualTo("newStore.png");
softAssertions.assertThat(user.getReliability()).isEqualTo(4.7d);
softAssertions.assertThat(user.getOauthId()).isEqualTo("12345");
softAssertions.assertThat(사용자.getName()).isEqualTo(사용자_이름);
softAssertions.assertThat(사용자.getProfileImage().getImage().getStoreName())
.isEqualTo(새로운_프로필_이미지_dto.storeName());
softAssertions.assertThat(사용자.getProfileImage().getImage().getUploadName())
.isEqualTo(새로운_프로필_이미지_dto.uploadName());
});
}

@Test
void 사용자_정보_수정시_존재하지_않는_사용자라면_예외가_발생한다() {
// given
final Long invalidUserId = -999L;

final MockMultipartFile updateImage = new MockMultipartFile(
"updateImage.png",
"updateImage.png",
MediaType.IMAGE_PNG.toString(),
new byte[]{1}
);

// when & then
assertThatThrownBy(() -> userService.updateById(invalidUserId, new UpdateUserDto("updateName", updateImage)))
assertThatThrownBy(() -> userService.updateById(존재하지_않는_사용자_아이디, 사용자_정보_수정_요청_dto))
.isInstanceOf(UserNotFoundException.class)
.hasMessage("사용자 정보를 사용할 수 없습니다.");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.ddang.ddang.user.application.fixture;

import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.domain.dto.StoreImageDto;
import com.ddang.ddang.user.application.dto.UpdateUserDto;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;

@SuppressWarnings("NonAsciiCharacters")
public class UserServiceFixture {

@Autowired
JpaUserRepository userRepository;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필수

private으로 변경 부탁드립니다!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 굿 감사합니다!


protected Long 존재하지_않는_사용자_아이디 = -999L;

protected String 사용자_이름 = "사용자";
protected ProfileImage 프로필_이미지;
protected User 사용자;
protected StoreImageDto 새로운_프로필_이미지_dto;
protected UpdateUserDto 사용자_정보_수정_요청_dto;
protected UpdateUserDto 사용자_이름만_수정_요청_dto;
protected UpdateUserDto 사용자_이미지만_수정_요청_dto;

@BeforeEach
void setUp() {
프로필_이미지 = new ProfileImage("upload.png", "store.png");
사용자 = User.builder()
.name(사용자_이름)
.profileImage(프로필_이미지)
.reliability(4.7d)
.oauthId("12345")
.build();

userRepository.save(사용자);

final MockMultipartFile 새로운_이미지_파일 = new MockMultipartFile(
"profileImage",
"updateImage.png",
MediaType.IMAGE_PNG.toString(),
new byte[]{1}
);
새로운_프로필_이미지_dto = new StoreImageDto(새로운_이미지_파일.getOriginalFilename(), "newStore.png");

사용자_정보_수정_요청_dto = new UpdateUserDto("updateName", 새로운_이미지_파일);
사용자_이름만_수정_요청_dto = new UpdateUserDto("updateName", null);
사용자_이미지만_수정_요청_dto = new UpdateUserDto(null, 새로운_이미지_파일);
}
}
Loading
Loading