JUnit4 in 60 Seconds

copy from http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/

 

played with JUnit 4 library this weekend and here is the short introduction to it:

  1. @Test
    Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”.  In addition, your class does not need to extend from “TestCase” class. (My note: 如果你的class不幸继承了TestCase的话,那么还将沿用Junit3的那一套寻找测试函数的模式(即函数名test***)@Test 这个annotattion将无效。

 

    @Test  
    public void addition() {  
        assertEquals(12, simpleMath.add(7, 5));  
    }  
      
    @Test  
    public void subtraction() {  
       assertEquals(9, simpleMath.substract(12, 3));  
    }  

 2. @Before and @After

  1. Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.
    @Before
    public void runBeforeEveryTest() {
    simpleMath = new SimpleMath();
    }

    @After
    public void runAfterEveryTest() {
    simpleMath = null;
    }
     
    @Before public void runBeforeEveryTest() { simpleMath = new SimpleMath(); } @After public void runAfterEveryTest() { simpleMath = null; }
  2. @BeforeClass and @AfterClass
    Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases.
        @BeforeClass  
        public static void runBeforeClass() {  
            // run for one time before all test cases  
        }  
          
        @AfterClass  
        public static void runAfterClass() {  
            // run for one time after all test cases  
        }  
     
    @BeforeClass public static void runBeforeClass() { // run for one time before all test cases } @AfterClass public static void runAfterClass() { // run for one time after all test cases }
  3. Exception Handling
    Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown.
        @Test(expected = ArithmeticException.class)  
        public void divisionWithException() {  
            // divide by zero  
            simpleMath.divide(1, 0);  
       }  
     
    @Test(expected = ArithmeticException.class) public void divisionWithException() { // divide by zero simpleMath.divide(1, 0); }
  4. @Ignore
    Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want.
        @Ignore("Not Ready to Run")  
        @Test  
        public void multiplication() {  
            assertEquals(15, simpleMath.multiply(3, 5));  
        }  
     
    @Ignore("Not Ready to Run") @Test public void multiplication() { assertEquals(15, simpleMath.multiply(3, 5)); }
  5. Timeout
    Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds.
        @Test(timeout = 1000)  
        public void infinity() {  
            while (true)  
                ;  
        }  
     
    @Test(timeout = 1000) public void infinity() { while (true) ; }
  6. New Assertions
    Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.

    public static void assertEquals(Object[] expected, Object[] actual);
    public static void assertEquals(String message, Object[] expected, Object[] actual);

     @Test  
     public void listEquality() {  
         List<Integer> expected = new ArrayList<Integer>();  
         expected.add(5);  
       
         List<Integer> actual = new ArrayList<Integer>();  
         actual.add(5);  
       
         assertEquals(expected, actual);  
     } 
     
    @Test public void listEquality() { List<Integer> expected = new ArrayList<Integer>(); expected.add(5); List<Integer> actual = new ArrayList<Integer>(); actual.add(5); assertEquals(expected, actual); }
  7. JUnit4Adapter
    Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.
        public static junit.framework.Test suite() {  
            return new JUnit4TestAdapter(SimpleMathTest.class);  
       }  
     
    public static junit.framework.Test suite() { return new JUnit4TestAdapter(SimpleMathTest.class); }

Happy coding. :D

 

 

My Note goes here:

文中没有提到JUnit4中TestSuite的用法,新的用法TestSuite.addTest(..)已经不再适用,因为新的testclass都没有extends TestCase.取而代之的用法如下:

import junit.framework.Test;
import junit.framework.TestSuite;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({YouTestCaseOne.class,YourTestCase2.class})
public class AllTests {

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值