`
niwtsew
  • 浏览: 69083 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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;
    }
     
  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  
        }  
     
  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);  
       }  
     
  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));  
        }  
     
  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)  
                ;  
        }  
     
  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);  
     } 
     
  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);  
       }  
     

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 {

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics