testImplementation 'org.assertj:assertj-core:3.23.1'
@DisplayName("")
@DisplayName("비밀번호 최소 8자 이상 12자 이하이면 예외가 발생하지 않는다.")
@Test
void validatorPasswordTest() { ... }
@ParameterizedTest
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2'
@ParameterizedTest
@ValueSource(strings = {"aabbcce", "aabbccddeeffg"})
void test(String str) { ... }
🔧 환경 설정
implementation 'org.passay:passay:1.6.1'
💻 코드 설명
RandomPasswordGenerator Class
User class
public void initPassword() {
RandomPasswordGenerator randomPasswordGenerator = new RandomPasswordGenerator();
String randomPassword = randomPasswordGenerator.generatePassword();
if (randomPassword.length() >= 8 & randomPassword.length() <= 12) {
this.password = randomPassword;
}
}
UserTest class
@DisplayName("패스워드를 초기화한다.")
@Test
void passwordTest() {
// given
User user = new User();
// when
user.initPassword();
// then
assertThat(user.getPassword()).isNotNull();
}
📌 테스트 케이스 실행 결과, 어떨 때는 성공이고, 어떨 때는 실패!
RandomPasswordGenerator randomPasswordGenerator = new RandomPasswordGenerator();
interface 생성 (PasswordGeneratePolicy Interface)
public interface PasswordGeneratePolicy {
String generatePassword();
}
RandomPasswordGenerator가 PasswordGeneratePolicy를 구현
public class RandomPasswordGenerator implements PasswordGeneratePolicy {...}
컨트롤할 수 없었던 코드를 외부로부터 주입받기 생성
컨트롤할 수 없었던 코드
RandomPasswordGenerator randomPasswordGenerator = new RandomPasswordGenerator();
User Class
public void initPassword(PasswordGeneratePolicy passwordGeneratePolicy ) {
String randomPassword = passwordGeneratePolicy.generatePassword();
...}
테스트 코드
void passwordTest() {
// given
User user = new User();
// when
user.initPassword(new CorrectFixedPasswordGenerator());
// then
assertThat(user.getPassword()).isNotNull();
}
void passwordTest2() {
// given
User user = new User();
// when
user.initPassword(new WrongFixedPasswordGenerator());
// then
assertThat(user.getPassword()).isNull();
}
user.initPassword(new CorrectFixedPasswordGenerator());
user.initPassword(new WrongFixedPasswordGenerator());