JUnit4

JUnit 4로 뛰어들기
Andrew Glover

기존 버전과 차이점을 비교해서 잘 보여주고 있어서 기존 JUnit 사용법도 배울수 있다.

- 기존 버전의 문제
테스트 메서드는 test라는 단어로 시작해야 한다.
테스트를 포함하는 클래스는 TestCase를 상속받아야 한다.

JUnit4
- JUnit 4에서는 테스트 메서드에 @Test 를 추가한다.
- import static 을 이용해서 org.junit.Assert.assert* 메서드를 사용한다.

- 예외 테스트
예전 버전
public void testZipCodeGroupException() throws Exception{		 
	Matcher mtcher = this.pattern.matcher("22101-5051");
	boolean isValid = mtcher.matches();			
	try{
		mtcher.group(2);
		fail("No exception was thrown");
	}catch(IndexOutOfBoundsException e){
	}
}
JUnit4
@Test(expected=IndexOutOfBoundsException.class)
public void verifyZipCodeGroupException() throws Exception{		
	Matcher mtcher = this.pattern.matcher("22101-5051");
	boolean isValid = mtcher.matches();			
	mtcher.group(2);		
}
- TestSuite
@RunWith(Suite.class)
@SuiteClasses({ParametricRegularExpressionTest.class,
      RegularExpressionTest.class,
      TimedRegularExpressionTest.class})
public class JUnit4Suite {

}
- 전/후처리
public class SampleTest {

	private int i;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		System.out.println("setUpBeforeClass");
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		System.out.println("tearDownAfterClass");
	}

	@Before
	public void setUp() throws Exception {
		System.out.println("setUp");
	}

	@After
	public void tearDown() throws Exception {
		System.out.println("tearDown");
	}
	
	@Test
	public void method1(){
		System.out.println("method1");
		assertEquals("test1", i, 0);
	}
	
	@Test
	public void method2(){
		System.out.println("method2");
		assertEquals("test2", i, 1);
	}
}
setUpBeforeClass
setUp
method1
tearDown
setUp
method2
tearDown
tearDownAfterClass

- 매개변수 테스트
1. 매개변수를 사용하지 않는 일반 테스트를 작성한다.
	@BeforeClass
	public static void setUpBeforeClass(){
		pattern = Pattern.compile(zipRegEx);
	}
	
	@Test
	public void verifyGoodZipCode(){
/*		Matcher matcher = this.pattern.matcher("22010");
		boolean isValid = matcher.matches();
		assertTrue("Pattern did not validate zip code", isValid);
*/
		Matcher matcher = pattern.matcher(phrase);
		boolean isValid = matcher.matches();
		assertEquals("Pattern did not validate zip code", isValid, match);
	}
2. Collection 유형을 반환하는 static 피더 메서드를 작성하고 @Parameters 주석으로 표시한다.
	@Parameters
	public static Collection<Object[]> regExvalues(){
		return Arrays.asList((new Object[][]{
			{"22101", true}
			, {"221x1", false}
			, {"22101-5150", true}
			, {"221015150", false}
		}));
	}
3. 첫 번째 단계에서 정의한 일반 메서드에 필요한 매개변수 유형에 대한 클래스 멤버를 만든다.
	private String phrase;
	private boolean match;
4. 이러한 매개변수 유형을 사용하고 이를 세 번째 단계에서 정의한 클래스 멤버와 연결하는 생성자를 만든다.
	public RegularExpressionTest(String phrase, boolean match){
		this.phrase = phrase;
		this.match = match;
	}
5. @RunWith 주석을 통해 Parameterized 클래스와 함께 실행할 테스트 케이스를 지정한다.
@RunWith(Parameterized.class)
테스트를 실행하면 verifyGoodZipCode() 테스트 메서드가 regExValues() 데이터 피더 메서드에 정의된 각 값에 대해 한번씩 네번 실행된다.

- timeout, @Ignore, 명령행에서 실행, ant에서 사용, 배열을 검사하는 assertEquals() 추가