-
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.
Add: Junit 5 Init and Sample Test Code
- Loading branch information
1 parent
8905b76
commit 7b0cc51
Showing
6 changed files
with
165 additions
and
1 deletion.
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ttp/mvcframework/example/CorrectPasswordGenerator.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,18 @@ | ||
package com.ttp.mvcframework.example; | ||
|
||
/** | ||
* <b>정상패스워드 생성</b> | ||
* <p> | ||
* | ||
* </p> | ||
* | ||
* @author sangdo.park | ||
* @since 3/14/24 | ||
*/ | ||
public class CorrectPasswordGenerator implements PasswordGeneratePolicy { | ||
|
||
@Override | ||
public String generatePassword() { | ||
return "12345678"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/ttp/mvcframework/example/PasswordGeneratePolicy.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,16 @@ | ||
package com.ttp.mvcframework.example; | ||
|
||
/** | ||
* <b>PasswordGeneratePolicy</b> | ||
* <p> | ||
* 패스워드 생성 정책 | ||
* </p> | ||
* | ||
* @author sangdo.park | ||
* @since 3/14/24 | ||
*/ | ||
public interface PasswordGeneratePolicy { | ||
|
||
String generatePassword(); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/ttp/mvcframework/example/PasswordValidator.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,33 @@ | ||
package com.ttp.mvcframework.example; | ||
|
||
/** | ||
* <b>패스워드 검증</b> | ||
* <p> | ||
* 비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다. | ||
* </p> | ||
* | ||
* @author sangdo.park | ||
* @since 3/14/24 | ||
*/ | ||
public class PasswordValidator { | ||
|
||
/** | ||
* 비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다. | ||
*/ | ||
public void validate(String password) { | ||
int length = password.length(); | ||
|
||
if (length < 8 || length > 12) { | ||
throw new IllegalArgumentException("비밀번호는 최소 8자 이상 12자 이하여야 한다."); | ||
} | ||
} | ||
|
||
public void validate2(PasswordGeneratePolicy passwordGeneratePolicy) { | ||
String password = passwordGeneratePolicy.generatePassword(); | ||
|
||
int length = password.length(); | ||
if (length < 8 || length > 12) { | ||
throw new IllegalArgumentException("비밀번호는 최소 8자 이상 12자 이하여야 한다."); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ttp/mvcframework/example/WrongPasswordGenerator.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,18 @@ | ||
package com.ttp.mvcframework.example; | ||
|
||
/** | ||
* <b>비정상패스워드 생성</b> | ||
* <p> | ||
* | ||
* </p> | ||
* | ||
* @author sangdo.park | ||
* @since 3/14/24 | ||
*/ | ||
public class WrongPasswordGenerator implements PasswordGeneratePolicy { | ||
|
||
@Override | ||
public String generatePassword() { | ||
return "zero1234567890"; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/test/java/com/ttp/mvcframework/example/PasswordValidatorTest.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,79 @@ | ||
package com.ttp.mvcframework.example; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
/** | ||
* <b></b> | ||
* <p> | ||
* | ||
* </p> | ||
* | ||
* @author sangdo.park | ||
* @since 3/14/24 | ||
*/ | ||
class PasswordValidatorTest { | ||
|
||
/* | ||
[] 비밀번호는 최소 8자 이상 12자 이하여야 한다. | ||
[] 비밀번호가 8자 미만 또는 12자 초과인 경우 IllegalArgumentException 예외를 발생시킨다. | ||
[] 경계조건에 대해 테스트 코드를 작성해야 한다. | ||
*/ | ||
|
||
@DisplayName("비밀번호가 최소 8자 이상, 12자 이하면 예외가 발생하지 않는다.") | ||
@Test | ||
void validatePasswordTest() { | ||
// given | ||
String password = "12345678"; | ||
PasswordValidator passwordValidator = new PasswordValidator(); | ||
|
||
// when, then | ||
assertThatCode(() -> passwordValidator.validate(password)).doesNotThrowAnyException(); | ||
|
||
} | ||
|
||
|
||
@DisplayName("비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다.") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"aabbcce", "aabbccddeeffg"}) | ||
void validatePasswordTest2(String value) { | ||
// given | ||
PasswordValidator passwordValidator = new PasswordValidator(); | ||
|
||
// when, then | ||
assertThatCode(() -> passwordValidator.validate(value)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("비밀번호는 최소 8자 이상 12자 이하여야 한다."); | ||
} | ||
|
||
/** | ||
* 테스트 하기 쉬운 코드를 작성하다 보면 더 낮은 결합도를 가진 설계를 얻을 수 있다. | ||
*/ | ||
@DisplayName("비밀번호가 최소 8자 이상, 12자 이하면 예외가 발생하지 않는다.") | ||
@Test | ||
void validatePasswordTest2() { | ||
// given | ||
PasswordValidator passwordValidator = new PasswordValidator(); | ||
|
||
// when, then | ||
assertThatCode(() -> passwordValidator.validate2(new CorrectPasswordGenerator())) | ||
.doesNotThrowAnyException(); | ||
} | ||
|
||
|
||
@DisplayName("비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다.") | ||
@Test | ||
void validatePasswordTest3() { | ||
// given | ||
PasswordValidator passwordValidator = new PasswordValidator(); | ||
|
||
// when, then | ||
assertThatCode(() -> passwordValidator.validate2(new WrongPasswordGenerator())) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("비밀번호는 최소 8자 이상 12자 이하여야 한다."); | ||
} | ||
} |