Conversation
twoosky
commented
Nov 10, 2022
| assertThat(userWithoutLevelRead.getLevel(), is(Level.BASIC)); | ||
| } | ||
|
|
||
| static class TestUserService extends UserService { |
Collaborator
Author
There was a problem hiding this comment.
- 모든 사용자 레벨을 업그레이드 하는 도중 예외가 발생해서 작업이 중단되는 경우 변경된 작업도 모두 취소시킨다.
- 그럼, 현재 코드에서 중간에 예외가 발생하면 이미 변경된 사용자의 레벨은 작업 이전 상태로 돌아갈까?
아니면 바뀐 채로 남아 있을까?
TestUserService 는 강제로 예외를 발생시키기 위한 테스트용 클래스를 정의한 것이다.
- UserService를 상속해서 테스트에 필요한 기능을 추가하도록 일부 메소드를 오버라이딩한다.
- 테스트에서만 사용할 클래스라면 번거롭게 파일을 따로 만들지 말고 테스트 클래스 내부에 static 클래스로 만드는 것이 간편하다.
| if (user.getId().equals(this.id)) throw new TestUserServiceException(); | ||
| super.upgradeLevel(user); | ||
| } | ||
| } |
Collaborator
Author
There was a problem hiding this comment.
UserService의 upgradeLevel을 오버라이딩 했다.
- 미리 지정된 id를 가진 사용자가 발견되면 강제로 예외를 던진다.
- 테스트 목적의 예외인
TestUserServiceException도 테스트 클래스 내 static 멤버 클래스로 만든다.
| @Test | ||
| public void upgradeAllOrNothing() { | ||
| UserService testUserService = new TestUserService(users.get(3).getId()); | ||
| testUserService.setUserDao(this.userDao); |
Collaborator
Author
There was a problem hiding this comment.
user id가 3인 사용자가 발견되면 강제로 예외를 던진다.
| catch (TestUserServiceException ignored) { | ||
| } | ||
|
|
||
| checkLevel(users.get(1), false); |
Collaborator
Author
There was a problem hiding this comment.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

트랜잭션을 부셔보자