-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 모의 객체 생성, - BDDMockito를 사용하여 Stub을 대체 - 모의 객체가 기대한 대로 불렸는지 검증 - Spy 대체
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
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,5 @@ | ||
package com.hou27.chap07.mock; | ||
|
||
public interface EmailNotifier { | ||
void sendRegisterEmail(String email); | ||
} |
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,27 @@ | ||
package com.hou27.chap07.mock; | ||
|
||
public class UserRegister { | ||
private WeakPasswordChecker passwordChecker; | ||
private UserRepository userRepository; | ||
private EmailNotifier emailNotifier; | ||
|
||
public UserRegister(WeakPasswordChecker passwordChecker, | ||
EmailNotifier emailNotifier, | ||
UserRepository userRepository) { | ||
this.passwordChecker = passwordChecker; | ||
this.emailNotifier = emailNotifier; | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public void register(String id, String pw, String email) { | ||
if(passwordChecker.checkPasswordWeak(pw)) { | ||
throw new WeakPasswordException(); | ||
} | ||
|
||
User user = new User(id, pw, email); | ||
userRepository.save(user); | ||
|
||
emailNotifier.sendRegisterEmail(email); | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
김정호/src/test/java/com/hou27/chap07/mock/UserRegisterMockTest.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,64 @@ | ||
package com.hou27.chap07.mock; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class UserRegisterMockTest { | ||
|
||
private UserRegister userRegister; | ||
private WeakPasswordChecker mockPasswordChecker | ||
= Mockito.mock(WeakPasswordChecker.class); | ||
private EmailNotifier mockEmailNotifier | ||
= Mockito.mock(EmailNotifier.class); | ||
private UserRepository fakeRepository | ||
= new MemoryUserRepository(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
userRegister = new UserRegister( | ||
mockPasswordChecker, | ||
mockEmailNotifier, | ||
fakeRepository | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("약한 암호면 가입 실패") | ||
void whenWeakPassword_thenFailRegister() { | ||
BDDMockito.given(mockPasswordChecker.checkPasswordWeak("pw")) | ||
.willReturn(true); | ||
|
||
assertThrows(WeakPasswordException.class, () -> { | ||
userRegister.register("id", "pw", "email"); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 가입 시 암호 검사 수행") | ||
void whenRegister_thenCheckPassword() { | ||
userRegister.register("id", "pw", "email"); | ||
|
||
BDDMockito.then(mockPasswordChecker) | ||
.should() | ||
.checkPasswordWeak("pw"); | ||
} | ||
|
||
@Test | ||
@DisplayName("가입 시 메일 전송") | ||
void whenRegister_thenSendMail() { | ||
userRegister.register("id", "pw", "[email protected]"); | ||
|
||
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); | ||
BDDMockito.then(mockEmailNotifier) | ||
.should() | ||
.sendRegisterEmail(captor.capture()); | ||
|
||
String realEmail = captor.getValue(); | ||
assertEquals("[email protected]", realEmail); | ||
} | ||
} |