Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- Programming
- django
- MySQL
- AI
- Italy
- QT
- Book
- ubuntu
- hbase
- RFID
- program
- history
- Book review
- hadoop
- comic agile
- agile
- Linux
- Malaysia
- Software Engineering
- leadership
- Kuala Lumpur
- programming_book
- management
- Java
- essay
- Artificial Intelligence
- erlang
- Python
- France
- web
Archives
- Today
- Total
Parameterized test 본문
// http://kentbeck.github.com/junit/javadoc/latest/index.html?org/junit/runners/Parameterized.html
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static List>Object[]< data() {
return Arrays.asList(new Object[][] {
Fibonacci,
{ { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }
});
}
private int fInput;
private int fExpected;
public FibonacciTest(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
/*
1. setUp() method는 아래 link에서는 @BeforeClass로 전역 설정을 해주는 것으로 설명했으나
나의 경우 test method마다 set up을 다시 해줘야 하는 경우가 있어,
@Before public void setUp()으로 변경해서 해봤는데 잘 동작하였음
2. @Parameters의 return type이 Collection인 경우 문제는 없으나
Eclipse에서 warning을 출력하여 Collection>Object[]<로 변경
3. test하던 도중에 @Parameters method가 없으면 setUp() method에서 다음 exception을 발생시킨다
java.lang.Exception: No public static parameters method on class
*/
// http://www.egovframe.go.kr/wiki/doku.php?id=egovframework:dev:tst:test_case#parameterized_test
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ParameterizedTest {
private String password;
private boolean isValid;
private static PasswordValidator validator;
//@BeforeClass
//public static void setUp() {
// validator = new PasswordValidator();
//}
@Before
public void setUp() {
validator = new PasswordValidator();
}
public ParameterizedTest(String password, boolean isValid) {
this.password = password;
this.isValid = isValid;
}
@Parameters
//public static Collection passwords() {
public static Collection>Object[]< passwords() {
return Arrays.asList(new Object[][] {
{ "1234qwer", true }, {"12345678", false}, {"1q2w3e4r", true}
});
}
@Test
public void isValidPasswordWithParams() {
assertEquals(validator.isValid(this.password), this.isValid);
}
}
class PasswordValidator {
public boolean isValid(String password) {
boolean result = false;
int letterCnt = 0;
int digitCnt = 0;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (Character.isLetter(c)) letterCnt++;
else if (Character.isDigit(c)) digitCnt++;
}
// 8자리 이상이고(입력 때 체크되지만) 문자와 숫자가 적어도 한 개씩은 있어야 함
if (password.length() >= 8 && letterCnt > 0 && digitCnt > 0)
result = true;
return result;
}
}
실행순서
1. @parameter method - passwords()
2. test class constructor - ParameterizedTest()
3. setUp()
4. @Test method - isValidPasswordWithParams()
5. test class method - PasswordValidator의 isValid()
2~5는 @Parameters의 Object 개수만큼 반복
Comments