본문 바로가기

프로그래밍/Java

JUnit 정리 & 기본 사용법

JUnit Assertions

code 설명
assertEquals([message], expected, actual) 두 값이 같은 지 비교
assertSame([message], expceted, actual)
assertNotSame([message], expceted, actual)
두 객체가 동일한 객체인지 비교
assertTrue([message], expceted)
assertFalse([message], expceted)
참/거짓 판별
assertNull([message], expceted)
assertNotNull([message], expceted)
null여부 판단
fail([message]) 테스트 실패로 판단

import static org.junit.Assert.*;
으로 static import 하여 쉽게 사용할 수 있음.

JUnit4의 특징

  1. Java5 애노테이션 지원
  2. test라는 글자로 method 이름을 시작해야 한다는 제약 해소
    Test 메소드는 @Test를 붙인다
  3. 좀 더 유연한 픽스처
    @BeforeClass, @AfterClass, @Before, @After
  4. 예외 테스트
    @Test(expected=NumberFormatException.class)
  5. 시간 제한 테스트
    @Test(timeout=2000)
  6. 테스트 무시
    @Ignore(“”)
  7. 배열 지원
    assertArrayEquals([message], expected, actual);
  8. @RunWith(클래스 이름.class)
    JUnit Test 클래스를 실행하기 위한 러너(Runner)를 명시적으로 지정한다.
    @RunWith는 junit.runner.Runner를 구현한 외부 클래스를 인자로 갖는다.
  9. @SuiteClasses(Class[])
    보통 여러 개의 테스트 클래스를 수행하기 위해 쓰인다. @RunWith를 이용해 Suite.class를 러너로 사용한다.
  10. 파라미터를 이용한 테스트
    @RunWith(Parameterized.class)
    @Parameters
    public static Collection data() {
    }

Junit4 애노테이션
@BeforeClass : 테스트 클래스 내에서 수행 전 한 번만 실행, static method 여야 함
@AfterClass : 테스트 클래스 내에서 수행 후 한 번만 실행, static method 여야 함
@Before : 테스트 케이스 수행 전 반복실행
@After : 테스트 케이스 수행 후 반복실행
@Test : 테스트 메소드 지정

import org.junit.*;

public class Junit4Test {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("@BeforeClass");
    }

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

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

    @After
    public void tearDown() throws Exception {
        System.out.println("@After");
    }

    @Test
    public void testCase1() throws Exception {
        System.out.println("testCase1");
    }

    @Test
    public void testCase2() throws Exception {
        System.out.println("testCase2");
    }
}
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.*;

public class Junit4Exam {
    // 예외 테스트
    @Test(expected = NumberFormatException.class)
    public void testException() throws Exception {
        String str = "hello";
        System.out.println(Integer.parseInt(str));
    }

    // 테스트 시간 제한
    @Test(timeout = 1000)
    public void testTimeout() throws Exception {
        long sum = 0;
        for (int i = 0; i < 10000; i++) {
            for (int j = 0; j < 10000; j++) {
                sum += j;
            }
        }
        System.out.println(sum);
    }

    // 테스트 무시
    @Ignore
    @Test
    public void testIgnore() throws Exception {
        assertTrue(false);
    }

    // 배열 지원 - 값이랑 순서까지 동일해야 함
   @Test
    public void testAssertArrayEquals() throws Exception {
        Object[] a = {"Java", "Python", 1};
        Object[] b = {"Java", "Python", 1};
        assertArrayEquals(a, b);
    }
}