-
Notifications
You must be signed in to change notification settings - Fork 4
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
디바이스 토큰 관련 테스트 코드 리팩토링 #491
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
191bdf2
test: DeviceTokenTest Fixture 추가 및 테스트 케이스 리팩토링
kwonyj1022 4f25ea8
test: JpaDeviceTokenRepositoryTest Fixture 추가 및 테스트 케이스 리팩토링
kwonyj1022 f6df315
test: DeviceTokenServiceTest Fixture 추가 및 테스트 케이스 리팩토링
kwonyj1022 2cf23ab
test: DeviceTokenControllerTest Fixture 추가 및 테스트 케이스 리팩토링
kwonyj1022 812c6e9
test: 모든 픽스처 객체를 `@BeforeEach`로 세팅하도록 변경
kwonyj1022 161f39c
test: 테스트 픽스처 추가, 네이밍 변경, 코드 스타일 개선
kwonyj1022 6cd0b64
style: 불필요한 공백 제거
kwonyj1022 cb2a7c5
rename: 테스트하려는 결과의 변수명을 actual로 통일
kwonyj1022 abd8912
ci: 브랜치 최신화
kwonyj1022 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 18 additions & 63 deletions
81
backend/ddang/src/test/java/com/ddang/ddang/device/application/DeviceTokenServiceTest.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 |
---|---|---|
@@ -1,113 +1,68 @@ | ||
package com.ddang.ddang.device.application; | ||
|
||
import com.ddang.ddang.configuration.IsolateDatabase; | ||
import com.ddang.ddang.device.application.dto.PersistDeviceTokenDto; | ||
import com.ddang.ddang.device.application.fixture.DeviceTokenServiceFixture; | ||
import com.ddang.ddang.device.domain.DeviceToken; | ||
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository; | ||
import com.ddang.ddang.image.domain.ProfileImage; | ||
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.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@IsolateDatabase | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
@SuppressWarnings("NonAsciiCharacters") | ||
class DeviceTokenServiceTest { | ||
class DeviceTokenServiceTest extends DeviceTokenServiceFixture { | ||
|
||
@Autowired | ||
DeviceTokenService deviceTokenService; | ||
|
||
@Autowired | ||
JpaDeviceTokenRepository deviceTokenRepository; | ||
|
||
@Autowired | ||
JpaUserRepository userRepository; | ||
|
||
@Test | ||
void 사용자의_디바이스_토큰이_존재하지_않는다면_저장한다() { | ||
// given | ||
final String deviceTokenValue = "deviceToken"; | ||
final User user = User.builder() | ||
.name("사용자") | ||
.profileImage(new ProfileImage("upload.png", "store.png")) | ||
.reliability(4.7d) | ||
.oauthId("12345") | ||
.build(); | ||
userRepository.save(user); | ||
|
||
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(deviceTokenValue); | ||
|
||
// when & then | ||
assertThatNoException().isThrownBy(() -> deviceTokenService.persist(user.getId(), persistDeviceTokenDto)); | ||
assertThatNoException().isThrownBy( | ||
() -> deviceTokenService.persist(디바이스_토큰이_없는_사용자.getId(), 디바이스_토큰_저장을_위한_DTO) | ||
); | ||
} | ||
|
||
@Test | ||
void 사용자의_디바이스_토큰이_이미_존재하고_새로운_토큰이_주어진다면_토큰을_갱신한다() { | ||
// given | ||
final String deviceTokenValue = "deviceToken"; | ||
final User user = User.builder() | ||
.name("사용자") | ||
.profileImage(new ProfileImage("upload.png", "store.png")) | ||
.reliability(4.7d) | ||
.oauthId("12345") | ||
.build(); | ||
userRepository.save(user); | ||
|
||
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue); | ||
deviceTokenRepository.save(deviceToken); | ||
|
||
final String newDeviceTokenValue = "newDeviceToken"; | ||
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(newDeviceTokenValue); | ||
|
||
// when | ||
deviceTokenService.persist(user.getId(), persistDeviceTokenDto); | ||
deviceTokenService.persist(디바이스_토큰이_있는_사용자.getId(), 디바이스_토큰_갱신을_위한_DTO); | ||
|
||
// then | ||
assertThat(deviceToken.getDeviceToken()).isEqualTo(newDeviceTokenValue); | ||
final Optional<DeviceToken> deviceTokenResult = deviceTokenRepository.findByUserId(디바이스_토큰이_있는_사용자.getId()); | ||
final String actual = deviceTokenResult.get().getDeviceToken(); | ||
|
||
assertThat(actual).isEqualTo(갱신된_디바이스_토큰_값); | ||
} | ||
|
||
@Test | ||
void 사용자의_디바이스_토큰이_이미_존재하고_동일한_토큰이_주어진다면_토큰을_갱신하지_않는다() { | ||
// given | ||
final String deviceTokenValue = "deviceToken"; | ||
final User user = User.builder() | ||
.name("사용자") | ||
.profileImage(new ProfileImage("upload.png", "store.png")) | ||
.reliability(4.7d) | ||
.oauthId("12345") | ||
.build(); | ||
userRepository.save(user); | ||
|
||
final DeviceToken deviceToken = new DeviceToken(user, deviceTokenValue); | ||
deviceTokenRepository.save(deviceToken); | ||
|
||
final String newDeviceTokenValue = deviceTokenValue; | ||
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(newDeviceTokenValue); | ||
|
||
// when | ||
deviceTokenService.persist(user.getId(), persistDeviceTokenDto); | ||
deviceTokenService.persist(디바이스_토큰이_있는_사용자.getId(), 존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 질문저는 지토 코드를 참고해 Dto를 Dto라는 이름 없이 설명만 작성해주었는데 dto를 붙일지 여부도 통일시키면 좋을까요? 엔초가 작성해주신 방식이 가독성이 더 좋은 것 같다는 의견이긴 합니다..! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 통일하면 좋을 것 같아서 내일 데일리에 물어보도록 하죠! |
||
|
||
// then | ||
assertThat(deviceToken.getDeviceToken()).isEqualTo(deviceTokenValue); | ||
final Optional<DeviceToken> userDeviceToken = deviceTokenRepository.findByUserId(디바이스_토큰이_있는_사용자.getId()); | ||
final String actual = userDeviceToken.get().getDeviceToken(); | ||
|
||
assertThat(actual).isEqualTo(사용_중인_디바이스_토큰_값); | ||
} | ||
|
||
@Test | ||
void 사용자를_찾을_수_없다면_예외가_발생한다() { | ||
// given | ||
final Long invalidUserId = -999L; | ||
final String deviceTokenValue = "deviceToken"; | ||
final PersistDeviceTokenDto persistDeviceTokenDto = new PersistDeviceTokenDto(deviceTokenValue); | ||
|
||
// when & then | ||
assertThatThrownBy(() -> deviceTokenService.persist(invalidUserId, persistDeviceTokenDto)) | ||
assertThatThrownBy(() -> deviceTokenService.persist(존재하지_않는_사용자_아이디, 디바이스_토큰_저장을_위한_DTO)) | ||
.isInstanceOf(UserNotFoundException.class) | ||
.hasMessage("해당 사용자를 찾을 수 없습니다."); | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
...g/src/test/java/com/ddang/ddang/device/application/fixture/DeviceTokenServiceFixture.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,56 @@ | ||
package com.ddang.ddang.device.application.fixture; | ||
|
||
import com.ddang.ddang.device.application.dto.PersistDeviceTokenDto; | ||
import com.ddang.ddang.device.domain.DeviceToken; | ||
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository; | ||
import com.ddang.ddang.image.domain.ProfileImage; | ||
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 java.util.List; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
public class DeviceTokenServiceFixture { | ||
|
||
@Autowired | ||
private JpaDeviceTokenRepository deviceTokenRepository; | ||
|
||
@Autowired | ||
private JpaUserRepository userRepository; | ||
private String 초기_디바이스_토큰_값 = "initialDeviceToken"; | ||
private DeviceToken 사용자의_디바이스_토큰; | ||
|
||
protected String 사용_중인_디바이스_토큰_값 = "usingDeviceToken"; | ||
protected String 갱신된_디바이스_토큰_값 = "newDeviceToken"; | ||
protected Long 존재하지_않는_사용자_아이디 = -999L; | ||
protected User 디바이스_토큰이_있는_사용자; | ||
protected User 디바이스_토큰이_없는_사용자; | ||
protected PersistDeviceTokenDto 디바이스_토큰_저장을_위한_DTO; | ||
protected PersistDeviceTokenDto 디바이스_토큰_갱신을_위한_DTO; | ||
protected PersistDeviceTokenDto 존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
디바이스_토큰이_있는_사용자 = User.builder() | ||
.name("디바이스 토큰이 있는 사용자") | ||
.profileImage(new ProfileImage("upload.png", "store.png")) | ||
.reliability(4.7d) | ||
.oauthId("12345") | ||
.build(); | ||
디바이스_토큰이_없는_사용자 = User.builder() | ||
.name("디바이스 토큰이 없는 사용자") | ||
.profileImage(new ProfileImage("upload.png", "store.png")) | ||
.reliability(4.7d) | ||
.oauthId("12346") | ||
.build(); | ||
사용자의_디바이스_토큰 = new DeviceToken(디바이스_토큰이_있는_사용자, 사용_중인_디바이스_토큰_값); | ||
userRepository.saveAll(List.of(디바이스_토큰이_있는_사용자, 디바이스_토큰이_없는_사용자)); | ||
deviceTokenRepository.save(사용자의_디바이스_토큰); | ||
|
||
디바이스_토큰_저장을_위한_DTO = new PersistDeviceTokenDto(초기_디바이스_토큰_값); | ||
디바이스_토큰_갱신을_위한_DTO = new PersistDeviceTokenDto(갱신된_디바이스_토큰_값); | ||
존재하는_디바이스_토큰과_동일한_토큰을_저장하려는_DTO = new PersistDeviceTokenDto(사용_중인_디바이스_토큰_값); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
backend/ddang/src/test/java/com/ddang/ddang/device/domain/fixture/DeviceTokenFixture.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,23 @@ | ||
package com.ddang.ddang.device.domain.fixture; | ||
|
||
import com.ddang.ddang.image.domain.ProfileImage; | ||
import com.ddang.ddang.user.domain.User; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
public class DeviceTokenFixture { | ||
|
||
protected String 디바이스_토큰 = "deviceToken"; | ||
protected String 새로운_디바이스_토큰 = "newDeviceToken"; | ||
protected User 사용자; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
사용자 = User.builder() | ||
.name("사용자") | ||
.profileImage(new ProfileImage("upload.png", "store.png")) | ||
.reliability(4.7d) | ||
.oauthId("12345") | ||
.build(); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
질문
검사하는 대상의 변수명은 actual로 하기로 했던 것 같은데 조금 헷갈리네요,,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테스트 하고자 하는 메서드는 when 절에 있는데 해당 메서드는 반환값이 없기 때문에 then 절에서 직접 다시 조회해 온 후 테스트를 진행하는지라 actual을 사용하지 않았습니다. 이런 부분에도 actual을 사용하는 것이 좋을까요? 다른 분들의 의견도 궁금하군요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그런데 생각해보니 actual이 맞는 것 같네요. 수정하도록 하겠습니다!