Skip to content

Commit 7b0cc51

Browse files
Add: Junit 5 Init and Sample Test Code
1 parent 8905b76 commit 7b0cc51

File tree

6 files changed

+165
-1
lines changed

6 files changed

+165
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141

4242
testImplementation 'org.springframework.boot:spring-boot-starter-test'
4343
}
44-
44+
4545
tasks.named('test') {
4646
useJUnitPlatform()
4747
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ttp.mvcframework.example;
2+
3+
/**
4+
* <b>정상패스워드 생성</b>
5+
* <p>
6+
*
7+
* </p>
8+
*
9+
* @author sangdo.park
10+
* @since 3/14/24
11+
*/
12+
public class CorrectPasswordGenerator implements PasswordGeneratePolicy {
13+
14+
@Override
15+
public String generatePassword() {
16+
return "12345678";
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ttp.mvcframework.example;
2+
3+
/**
4+
* <b>PasswordGeneratePolicy</b>
5+
* <p>
6+
* 패스워드 생성 정책
7+
* </p>
8+
*
9+
* @author sangdo.park
10+
* @since 3/14/24
11+
*/
12+
public interface PasswordGeneratePolicy {
13+
14+
String generatePassword();
15+
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ttp.mvcframework.example;
2+
3+
/**
4+
* <b>패스워드 검증</b>
5+
* <p>
6+
* 비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다.
7+
* </p>
8+
*
9+
* @author sangdo.park
10+
* @since 3/14/24
11+
*/
12+
public class PasswordValidator {
13+
14+
/**
15+
* 비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다.
16+
*/
17+
public void validate(String password) {
18+
int length = password.length();
19+
20+
if (length < 8 || length > 12) {
21+
throw new IllegalArgumentException("비밀번호는 최소 8자 이상 12자 이하여야 한다.");
22+
}
23+
}
24+
25+
public void validate2(PasswordGeneratePolicy passwordGeneratePolicy) {
26+
String password = passwordGeneratePolicy.generatePassword();
27+
28+
int length = password.length();
29+
if (length < 8 || length > 12) {
30+
throw new IllegalArgumentException("비밀번호는 최소 8자 이상 12자 이하여야 한다.");
31+
}
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ttp.mvcframework.example;
2+
3+
/**
4+
* <b>비정상패스워드 생성</b>
5+
* <p>
6+
*
7+
* </p>
8+
*
9+
* @author sangdo.park
10+
* @since 3/14/24
11+
*/
12+
public class WrongPasswordGenerator implements PasswordGeneratePolicy {
13+
14+
@Override
15+
public String generatePassword() {
16+
return "zero1234567890";
17+
}
18+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.ttp.mvcframework.example;
2+
3+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
4+
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.ValueSource;
9+
10+
/**
11+
* <b></b>
12+
* <p>
13+
*
14+
* </p>
15+
*
16+
* @author sangdo.park
17+
* @since 3/14/24
18+
*/
19+
class PasswordValidatorTest {
20+
21+
/*
22+
[] 비밀번호는 최소 8자 이상 12자 이하여야 한다.
23+
[] 비밀번호가 8자 미만 또는 12자 초과인 경우 IllegalArgumentException 예외를 발생시킨다.
24+
[] 경계조건에 대해 테스트 코드를 작성해야 한다.
25+
*/
26+
27+
@DisplayName("비밀번호가 최소 8자 이상, 12자 이하면 예외가 발생하지 않는다.")
28+
@Test
29+
void validatePasswordTest() {
30+
// given
31+
String password = "12345678";
32+
PasswordValidator passwordValidator = new PasswordValidator();
33+
34+
// when, then
35+
assertThatCode(() -> passwordValidator.validate(password)).doesNotThrowAnyException();
36+
37+
}
38+
39+
40+
@DisplayName("비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다.")
41+
@ParameterizedTest
42+
@ValueSource(strings = {"aabbcce", "aabbccddeeffg"})
43+
void validatePasswordTest2(String value) {
44+
// given
45+
PasswordValidator passwordValidator = new PasswordValidator();
46+
47+
// when, then
48+
assertThatCode(() -> passwordValidator.validate(value))
49+
.isInstanceOf(IllegalArgumentException.class)
50+
.hasMessage("비밀번호는 최소 8자 이상 12자 이하여야 한다.");
51+
}
52+
53+
/**
54+
* 테스트 하기 쉬운 코드를 작성하다 보면 더 낮은 결합도를 가진 설계를 얻을 수 있다.
55+
*/
56+
@DisplayName("비밀번호가 최소 8자 이상, 12자 이하면 예외가 발생하지 않는다.")
57+
@Test
58+
void validatePasswordTest2() {
59+
// given
60+
PasswordValidator passwordValidator = new PasswordValidator();
61+
62+
// when, then
63+
assertThatCode(() -> passwordValidator.validate2(new CorrectPasswordGenerator()))
64+
.doesNotThrowAnyException();
65+
}
66+
67+
68+
@DisplayName("비밀번호가 8자 미만 또는 12자 초과하는 경우 IllegalArgumentException 예외가 발생한다.")
69+
@Test
70+
void validatePasswordTest3() {
71+
// given
72+
PasswordValidator passwordValidator = new PasswordValidator();
73+
74+
// when, then
75+
assertThatCode(() -> passwordValidator.validate2(new WrongPasswordGenerator()))
76+
.isInstanceOf(IllegalArgumentException.class)
77+
.hasMessage("비밀번호는 최소 8자 이상 12자 이하여야 한다.");
78+
}
79+
}

0 commit comments

Comments
 (0)