自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

單元測試利器JUnit的實踐與總結(jié)

開發(fā) 后端
不管是hbase還是hadoop的源代碼,里面都有一個很大的子工程test,其代碼都是開發(fā)者所寫的單元測試代碼。這些代碼都用到了junit包,昨天學(xué)習(xí)了一下這個東西,大概總結(jié)一下如何在eclipse環(huán)境下使用Junit進行單元測試。

單元測試工具Junit是一個開源項目,昨天學(xué)習(xí)了一下這個東西,總結(jié)下心得。

1.創(chuàng)建相應(yīng)的test類

package:測試類存放位置。

Name:測試類名字。

setUp,tearDown:測試類創(chuàng)建測試環(huán)境以及銷毀測試環(huán)境,這兩個方法只執(zhí)行一次。

Class Under test:需要被測試的類路徑及名稱。

點擊下一步就會讓你選擇需要給哪些方法進行測試。

測試類創(chuàng)建完成后在類中會出現(xiàn)你選擇的方法的測試方法:

  1. package test.com.boco.bomc.alarmrelevance.show.dao; 
  2.   
  3. import junit.framework.TestCase; 
  4. import org.junit.After; 
  5. import org.junit.Before; 
  6. import org.junit.BeforeClass; 
  7. import org.junit.Test; 
  8.  
  9. public class ShowStrategyDaoTest extends TestCase{ 
  10.  
  11.     @BeforeClass 
  12.     public static void setUpBeforeClass() throws Exception { 
  13.         System.out.println("OK1"); 
  14.     } 
  15.  
  16.     @Before 
  17.     public void setUp() throws Exception { 
  18.     } 
  19.  
  20.     @After 
  21.     public void tearDown() throws Exception { 
  22.     } 
  23.  
  24.     @Test 
  25.     public final void testGetDataByApplyNameOrHostIp() { 
  26.         fail("Not yet implemented"); // TODO 
  27.     } 
  28.  
  29.     @Test 
  30.     public final void testGetDataByObject() { 
  31.         fail("Not yet implemented"); // TODO 
  32.     } 
  33.  
  34.     @Test(timeout=1
  35.     public final void testGetApplyUser() { 
  36.         fail("Not yet implemented"); // TODO 
  37.     } 
  38.  
  39.     @Test 
  40.     public final void testGetVoiceUser() { 
  41.         fail("Not yet implemented"); // TODO 
  42.     } 
  43.  
  44.     @Test 
  45.     public final void testSearchInAera() { 
  46.         fail("Not yet implemented"); // TODO 
  47.     } 
  48.  
  49.     @Test 
  50.     public final void testGetDataByPolicyId() { 
  51.         fail("Not yet implemented"); // TODO 
  52.     } 

其中的@before,@test,@after表示在執(zhí)行測試方法前執(zhí)行,需執(zhí)行的測試方法,在測試方法執(zhí)行后執(zhí)行。

可以給@test添加timeout,exception參數(shù)。

在測試方法中可以用assertEquals(arg0,arg1);

可以用TestSuite把多個測試類集中到一起,統(tǒng)一執(zhí)行測試,例如:

  1. package test.com.boco.bomc.alarmrelevance.show.dao; 
  2.  
  3. import junit.framework.Test; 
  4. import junit.framework.TestSuite; 
  5.  
  6. public class TestAll { 
  7.     public static Test suite(){ 
  8.         TestSuite suite = new TestSuite("Running all the tests"); 
  9.         suite.addTestSuite(ShowStrategyDaoTest.class); 
  10.         suite.addTestSuite(com.boco.bomc.alarmrelevance.show.dao.ShowStrategyDaoTest.class); 
  11.         return suite; 
  12.     } 

另外還可以把多個TestSuite組合到一個Test類里面,例如:

  1. package test.com.boco.bomc.alarmrelevance.show.dao; 
  2.  
  3. import junit.framework.Test; 
  4. import junit.framework.TestCase; 
  5. import junit.framework.TestSuite; 
  6.  
  7. public class TestAll1 extends TestCase { 
  8.     public static Test suite(){ 
  9.         TestSuite suite1 = new TestSuite("TestAll1"); 
  10.         suite1.addTest(TestAll.suite()); 
  11.         suite1.addTest(TestAll2.suite()); 
  12.         return suite1; 
  13.     } 

這就更方便與集中測試,一個方法測試完了,可以對個方法,多個類一起測試。

注意:在寫代碼的時候TestSuite,TestCase,Test的包不要到錯了。

測試效果如下:

原文鏈接:http://www.cnblogs.com/God-froest/archive/2011/11/18/JunitTest.html

編輯推薦:

  1. Struts2單元測試:使用Junit測試Action
  2. Jython開發(fā)的JUnit測試包
  3. JUnit常用斷言方法
  4. 在Junit中測試私有函數(shù)的方法
  5. JUnit與JTiger的比較
責(zé)任編輯:林師授 來源: 牛_的博客
相關(guān)推薦

2017-01-14 23:26:17

單元測試JUnit測試

2017-01-16 12:12:29

單元測試JUnit

2011-02-15 10:05:48

PHPPHPUnit

2011-08-11 13:02:43

Struts2Junit

2022-04-08 09:01:56

腳本Go應(yīng)用單元

2009-12-23 15:03:52

WPF單元測試

2011-02-16 09:45:13

PHPPHPUnit

2017-02-21 10:30:17

Android單元測試研究與實踐

2017-01-14 23:42:49

單元測試框架軟件測試

2012-02-07 09:08:50

Feed4JUnitJava

2021-01-07 14:06:30

Spring BootJUnit5Java

2011-02-21 09:54:14

PHPPHPUnit

2024-04-26 11:14:34

C#單元測試框架

2021-08-26 11:00:54

Spring BootJUnit5Java

2023-12-28 17:36:10

JUnit5單元測試框架

2012-10-29 09:45:52

單元測試軟件測試測試實踐

2022-09-15 10:02:58

測試軟件

2009-06-08 19:59:09

EclipseJUnit單元測試

2009-06-08 19:57:29

EclipseJUnit4單元測試

2023-07-26 08:58:45

Golang單元測試
點贊
收藏

51CTO技術(shù)棧公眾號