3.x 버전을 사용할때의 JUnit 스타일#
package openframework.common.junit3;
import junit.framework.TestCase;
import openframework.common.Util;
public class UtilTest extends TestCase {
public void testSum() {
assertTrue(3 == Util.sum(1, 2));
}
}
|
일단 위 소스를 보자. junit 3.x 를 사용할때는 다음과 같은 제약사항이 따랐다.
- TestCase 클래스를 확장해야 한다.
- 테스트 메소드는 "test" 로 시작해야만 한다.
- 다양한 assert 메소드를 사용해서 조건을 체크해야 한다.
junit 4.x 에서의 스타일#
package openframework.common;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class UtilTest {
@Test
public void checkSumValue() throws Exception {
assertTrue(3 == Util.sum(1, 2));
}
}
|
위 코드는 앞서 본 3.x 소스와 같은 테스트를 수행하는 소스로 junit 4.x을 사용한다.
차이점은 다음과 같다.
- TestCase 를 확장하지 않는다.
- 테스트 메소드는 "test" 로 시작하지 않아도 된다.
- 테스트 메소드임을 나타내기 위해 @Test 라는 주석을 붙여준다.
- assert 메소드를 사용하기 위해 org.junit.Assert.assertTrue 를 static 으로 import해줘야 한다.
대체되는 방법들#
3.x에서 테스트 이전에 초기화를 해주거나 특정 자원(DB Connection과 같은)을 셋팅해야 할 필요가 있을 경우 setup()메소드에 해당 로직을 넣어줬다. 4.x 에서는 명시적으로 setup()메소드는 없다. 단 setup() 메소드 역할을 하는 메소드에 @Before 주석만 붙여주면 된다.
junit 3.x 에서의 방법
protected void setUp() throws Exception {
System.out.println("## TestCase Setup");
}
|
junit 4.x 에서의 방법
@Before
public void setUpBefore() throws Exception {
System.out.println("## TestCase Setup");
}
|
앞서 본 setup()메소드처럼 3.x에서는 teardown()메소드가 있었으나 4.x에서는 @After 주석만 붙여주면 된다.
junit 3.x 에서의 방법
@Override
protected void tearDown() throws Exception {
System.out.println("## TestCase TearDown");
}
|
junit 4.x 에서의 방법
@After
public void setUpAfter() throws Exception {
System.out.println("## TestCase TearDown");
}
|
4.x에서의 주석으로 처리하는 방식으로 인한 장점은 다음과 같다. setup(), teardown() 의 기능을 하는 메소드를 정의할때 개수의 제한이 없다.
- @BeforeClass, @AfterClass
참고로 @Before, @After 로 처리된 메소드는 테스트 메소드가 실행되는 매번 작동한다. 즉 테스트 메소드가 3개라면 각각 3번씩 실행된다. 하지만 대개 이 메소드들은 테스트시 한번씩만 실행되면 되는데 그런 요구사항이 있을때 이 어노테이션을 사용하면 된다. 이 어노테이션으로 처리된 메소드는 테스트 메소드의 개수의 상관없이 한번만 실행된다. 이런 기능을 하는 메소드는 JUnit 3.x에서는 제공되지 않았다. 여기서 주의할 점은 @BeforeClass, @AfterClass 로 정의되는 메소드는 static으로 정의되어야 한다.
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("## BeforeClass");
}
@AfterClass
public static void setUpAfterClass() throws Exception {
System.out.println("## AftereClass");
}
|
- @Test(expected=Exception클래스)
이 주석은 테스트중에 Exception이 발생되어야 하는 조건이 있을 경우 사용된다. 다시 말해 특정 조건을 만족하는게 테스트에 통과하는게 아니라 특정 Exception이 발생하는 것이 테스트에 통과하는 조건이 될때 사용된다.
@Test(expected = SQLException.class)
public void checkSumValue() throws SQLException {
assertTrue("each value is different", 3 == Util.sum(1, 2));
throw new SQLException();
}
|
위 코드의 경우 SQLException이 발생해야 다음 테스트 메소드로 정상적으로 넘어간다.
테스트 메소드를 임시로 테스트에서 제외시키고자 할때 사용한다.
@Ignore
@Test
public void checkSumValue2() {
System.out.println("=> checkSumValue2");
assertTrue("each value is different", 3 == Util.sum(1, 2));
}
|
일반적으로 네트워크나 데이터베이스 프로그래밍의 경우 lock이 걸리거나 응답을 받는데 오랜시간이 걸리는 경우가 있다. 이럴때 타임아웃을 통해 강제로 끊어야 할 경우 사용할 수 있다.
@Test(timeout=10)
public void checkSumValue() {
System.out.println("=> checkSumValue");
assertTrue("each value is different", 3 == Util.sum(1, 2));
}
|
여기서 사용된 샘플소스#
package openframework.common;
import static org.junit.Assert.assertTrue;
import java.sql.SQLException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class UtilTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("## BeforeClass");
}
@AfterClass
public static void setUpAfterClass() throws Exception {
System.out.println("## AftereClass");
}
@Before
public void setUpBefore() throws Exception {
System.out.println("## Before");
}
@After
public void setUpAfter() throws Exception {
System.out.println("## After");
}
@Test(expected = SQLException.class)
public void checkSQLException() throws SQLException {
System.out.println("=> checkSQLException");
throw new SQLException();
}
@Test(timeout=10)
public void checkSumValue() {
System.out.println("=> checkSumValue");
assertTrue("each value is different", 3 == Util.sum(1, 2));
}
@Ignore
@Test
public void checkSumValue2() {
System.out.println("=> checkSumValue2");
assertTrue("each value is different", 3 == Util.sum(1, 2));
}
}
|