-
Notifications
You must be signed in to change notification settings - Fork 0
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
5.2 트랜잭션 서비스 추상화 #2
base: main
Are you sure you want to change the base?
Conversation
assertThat(userWithoutLevelRead.getLevel(), is(Level.BASIC)); | ||
} | ||
|
||
static class TestUserService extends UserService { |
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.
- 모든 사용자 레벨을 업그레이드 하는 도중 예외가 발생해서 작업이 중단되는 경우 변경된 작업도 모두 취소시킨다.
- 그럼, 현재 코드에서 중간에 예외가 발생하면 이미 변경된 사용자의 레벨은 작업 이전 상태로 돌아갈까?
아니면 바뀐 채로 남아 있을까?
TestUserService
는 강제로 예외를 발생시키기 위한 테스트용 클래스를 정의한 것이다.
- UserService를 상속해서 테스트에 필요한 기능을 추가하도록 일부 메소드를 오버라이딩한다.
- 테스트에서만 사용할 클래스라면 번거롭게 파일을 따로 만들지 말고 테스트 클래스 내부에 static 클래스로 만드는 것이 간편하다.
if (user.getId().equals(this.id)) throw new TestUserServiceException(); | ||
super.upgradeLevel(user); | ||
} | ||
} |
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.
UserService의 upgradeLevel을 오버라이딩 했다.
- 미리 지정된 id를 가진 사용자가 발견되면 강제로 예외를 던진다.
- 테스트 목적의 예외인
TestUserServiceException
도 테스트 클래스 내 static 멤버 클래스로 만든다.
@Test | ||
public void upgradeAllOrNothing() { | ||
UserService testUserService = new TestUserService(users.get(3).getId()); | ||
testUserService.setUserDao(this.userDao); |
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.
user id가 3인 사용자가 발견되면 강제로 예외를 던진다.
catch (TestUserServiceException ignored) { | ||
} | ||
|
||
checkLevel(users.get(1), false); |
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.
트랜잭션을 부셔보자