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

[리팩토링] Post, Service 코드 개선 및 테스트 코드 추가 #129

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ application-oauth.yaml
application-aws.yaml
./src/main/resources/static

### test ###
src/test/resources/dbunit

HELP.md
.gradle
build/
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/yapp18/retrospect/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import com.sun.istack.NotNull;
import com.yapp18.retrospect.domain.BaseTimeEntity;
import com.yapp18.retrospect.security.oauth2.AuthProvider;

import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Getter @Setter
@Entity
Expand Down Expand Up @@ -60,10 +57,10 @@ public User updateNickname(String nickname){
return this;
}

public User updateProfile(String profile, String name, String nickname, String job, String intro){
public User updateProfile(String name, String nickname, String profile, String job, String intro){
this.name = name;
this.profile = profile;
this.nickname = nickname;
this.profile = profile;
this.job = job;
this.intro = intro;
return this;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/yapp18/retrospect/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -67,7 +66,7 @@ public UserDto.ProfileResponse updateUserProfiles(Long userIdx, UserDto.UpdateRe
List<String> list = Arrays.asList(request.getProfile());
imageService.deleteImageList(list, userIdx, s3ProfileImagePathSuffix); // list에 없는 s3 가비지 데이터를 추출하여 삭제
}
return existingUser.updateProfile(request.getProfile(), request.getName(), request.getNickname(), request.getJob(), request.getIntro());
return existingUser.updateProfile(request.getName(), request.getNickname(), request.getProfile(), request.getJob(), request.getIntro());
})
.orElseThrow(() -> new EntityNullException(ErrorInfo.USER_NULL));
userRepository.save(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static User createUserEntity(){
.nickname("테스트닉네임")
.profile("프로필URL")
.provider(AuthProvider.kakao)
.providerId("12345")
.providerId("1")
.role(Role.MEMBER)
.job("테스트직업")
.intro("테스트자기소개")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,90 @@
package com.yapp18.retrospect.domain.user;

import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.ExpectedDatabase;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import com.yapp18.retrospect.annotation.RetrospectDataTest;
import com.yapp18.retrospect.common.EntityCreator;
import com.yapp18.retrospect.config.ErrorInfo;
import com.yapp18.retrospect.web.advice.EntityNullException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;

import static org.assertj.core.api.Assertions.assertThat;

@RetrospectDataTest
@DatabaseSetup({
"classpath:dbunit/entity/user.xml"
})
public class UserRepositoryTest {
@Autowired
UserRepository userRepository;

@Autowired
EntityManager entityManager;

private final long USER_IDX = 1L;

@Test
@DatabaseSetup({
"classpath:dbunit/User/빈_사용자_테이블.xml"
})
@ExpectedDatabase(value = "classpath:dbunit/User/사용자_한명_존재.xml",
assertionMode= DatabaseAssertionMode.NON_STRICT)
public void 사용자_정보_등록(){
User user = EntityCreator.createUserEntity();

userRepository.save(user);
}

@Test
@DatabaseSetup({
"classpath:dbunit/User/사용자_한명_존재.xml"
})
public void 사용자_정보_조회(){
User expected = EntityCreator.createUserEntity();

User user = userRepository.findById(1L)
.orElseThrow(() -> new EntityNullException(ErrorInfo.USER_NULL));

assertThat(user.getUserIdx()).isEqualTo(expected.getUserIdx());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

import static org.junit.jupiter.api.Assertions.assertEquals;

assertEquals(user.getUserIdx(), expected.getUserIdx());
-> 요거 쓰면 훨씬 간결하고 깔끔하게 비교할 수 있음
만약 boolean 값을 비교하고 싶다면 assertTrue(예상값); 이런 식으로도 활용 가능!

Copy link
Contributor

@ybell1028 ybell1028 Jul 22, 2021

Choose a reason for hiding this comment

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

코드가 짧아지는것도 좋지만 기댓값, 예상값이 뚜렷하게 보여서 실수도 미연에 방지할 수 있고 가독성이 더 높은 Aseertj(AssertThat)를 이용함!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오호 나는 오히려 assertEquals가 가독성이 좋다고 생각했는데 그런 차이가 있었구먼👍

Copy link
Contributor

Choose a reason for hiding this comment

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

https://itcoin.tistory.com/500
대충 이러이러한 장점이 있다네여

assertThat(user.getEmail()).isEqualTo(expected.getEmail());
assertThat(user.getName()).isEqualTo(expected.getName());
assertThat(user.getNickname()).isEqualTo(expected.getNickname());
assertThat(user.getProfile()).isEqualTo(expected.getProfile());
assertThat(user.getJob()).isEqualTo(expected.getJob());
assertThat(user.getIntro()).isEqualTo(expected.getIntro());
assertThat(user.getProvider()).isEqualTo(expected.getProvider());
assertThat(user.getProviderId()).isEqualTo(expected.getProviderId());
assertThat(user.getRole()).isEqualTo(expected.getRole());
}

@Test
public void 멤버_조회(){
@DatabaseSetup({
"classpath:dbunit/User/사용자_한명_존재.xml"
})
@ExpectedDatabase(value = "classpath:dbunit/User/수정된_사용자_정보.xml",
assertionMode= DatabaseAssertionMode.NON_STRICT)
public void 사용자_정보_수정(){
User user = userRepository.findById(1L)
.orElseThrow(() -> new EntityNullException(ErrorInfo.USER_NULL));

assertThat(user.getUserIdx()).isEqualTo(1L);
user.updateProfile("수정된이름", "수정된닉네임", "수정된프로필URL", "수정된직업", "수정된자기소개");

entityManager.flush(); // flush 연산을 통해 데이터베이스에 강제 반영
}

@Test
@DatabaseSetup({
"classpath:dbunit/User/사용자_한명_존재.xml"
})
@ExpectedDatabase(value = "classpath:dbunit/User/빈_사용자_테이블.xml",
assertionMode= DatabaseAssertionMode.NON_STRICT)
public void 사용자_정보_삭제(){
User user = userRepository.findById(USER_IDX)
.orElseThrow(() -> new EntityNullException(ErrorInfo.USER_NULL));

userRepository.delete(user);

entityManager.flush(); // flush 연산을 통해 데이터베이스에 강제 반영
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;

@EncodingConfig
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureMockMvc
public abstract class AbstractControllerTest {
@Autowired
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/dbunit/User/빈_사용자_테이블.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<user_tb/>
</dataset>
25 changes: 25 additions & 0 deletions src/test/resources/dbunit/User/사용자_두명_존재.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version='1.0' encoding='UTF-8'?>

<dataset>
<user_tb USER_IDX="1"
EMAIL="[email protected]"
NAME="테스트이름"
NICKNAME="테스트닉네임"
PROFILE="프로필URL"
JOB="테스트직업"
INTRO="테스트자기소개"
PROVIDER="kakao"
PROVIDER_ID="1"
ROLE="MEMBER"/>

<user_tb USER_IDX="2"
EMAIL="[email protected]"
NAME="테스트이름2"
NICKNAME="테스트닉네임2"
PROFILE="프로필URL2"
JOB="테스트직업2"
INTRO="테스트자기소개2"
PROVIDER="google"
PROVIDER_ID="2"
ROLE="MEMBER"/>
</dataset>
14 changes: 14 additions & 0 deletions src/test/resources/dbunit/User/사용자_한명_존재.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version='1.0' encoding='UTF-8'?>

<dataset>
<user_tb USER_IDX="1"
EMAIL="[email protected]"
NAME="테스트이름"
NICKNAME="테스트닉네임"
PROFILE="프로필URL"
JOB="테스트직업"
INTRO="테스트자기소개"
PROVIDER="kakao"
PROVIDER_ID="1"
ROLE="MEMBER"/>
</dataset>
14 changes: 14 additions & 0 deletions src/test/resources/dbunit/User/수정된_사용자_정보.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version='1.0' encoding='UTF-8'?>

<dataset>
<user_tb USER_IDX="1"
EMAIL="[email protected]"
NAME="수정된이름"
NICKNAME="수정된닉네임"
PROFILE="수정된프로필URL"
JOB="수정된직업"
INTRO="수정된자기소개"
PROVIDER="kakao"
PROVIDER_ID="1"
ROLE="MEMBER"/>
</dataset>