JUnit4单元测试

来源:互联网 发布:linux expect ssh 编辑:程序博客网 时间:2024/05/14 00:57

JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的几个annotation:
@Before:初始化方法
@After:释放资源
@Test:测试方法,在这里可以测试期望异常和超时时间
@Test(expected=ArithmeticException.class)检查被测方法是否抛出ArithmeticException异常
@Ignore:忽略的测试方法
@BeforeClass:针对所有测试,只执行一次,且必须为static void
@AfterClass:针对所有测试,只执行一次,且必须为static void
一个JUnit4的单元测试用例执行顺序为:
@BeforeClass -> @Before -> @Test -> @After -> @AfterClass;
每一个测试方法的调用顺序为:
@Before -> @Test -> @After;

import static org.junit.Assert.*; //Java5版本以上   
    
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 JUnit4Test {   
    @Before  
    public void before() {   
        System.out.println("@Before");   
    }   
    
    @Test  
         /**  
          *Mark your test cases with @Test annotations.   
          *You don’t need to prefix your test cases with “test”.  
          *tested class does not need to extend from “TestCase” class.  
          */  
    public void test() {   
        System.out.println("@Test");   
        assertEquals(5 + 5, 10);   
    }   
    
    @Ignore  
    @Test  
    public void testIgnore() {   
        System.out.println("@Ignore");   
    }   
    
    @Test(timeout = 50)   
    public void testTimeout() {   
        System.out.println("@Test(timeout = 50)");   
        assertEquals(5 + 5, 10);   
    }   
    
    @Test(expected = ArithmeticException.class)   
    public void testExpected() {   
        System.out.println("@Test(expected = Exception.class)");   
        throw new ArithmeticException();   
    }   
    
    @After  
    public void after() {   
        System.out.println("@After");   
    }   
    
    @BeforeClass  
    public static void beforeClass() {   
        System.out.println("@BeforeClass");   
    };   
    
    @AfterClass  
    public static void afterClass() {   
        System.out.println("@AfterClass");   
    };   
};  

输出结果:
@BeforeClass
@Before
@Test(timeout = 50)
@After
@Before
@Test(expected = Exception.class)
@After
@Before
@Test
@After
@AfterClass

0 0
原创粉丝点击