单元测试利器JUnit的实践与总结

开发 后端
不管是hbase还是hadoop的源代码,里面都有一个很大的子工程test,其代码都是开发者所写的单元测试代码。这些代码都用到了junit包,昨天学习了一下这个东西,大概总结一下如何在eclipse环境下使用Junit进行单元测试。

单元测试工具Junit是一个开源项目,昨天学习了一下这个东西,总结下心得。

1.创建相应的test类

package:测试类存放位置。

Name:测试类名字。

setUp,tearDown:测试类创建测试环境以及销毁测试环境,这两个方法只执行一次。

Class Under test:需要被测试的类路径及名称。

点击下一步就会让你选择需要给哪些方法进行测试。

测试类创建完成后在类中会出现你选择的方法的测试方法:

  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表示在执行测试方法前执行,需执行的测试方法,在测试方法执行后执行。

可以给@test添加timeout,exception参数。

在测试方法中可以用assertEquals(arg0,arg1);

可以用TestSuite把多个测试类集中到一起,统一执行测试,例如:

  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开发的JUnit测试包
  3. JUnit常用断言方法
  4. 在Junit中测试私有函数的方法
  5. JUnit与JTiger的比较
责任编辑:林师授 来源: 牛_的博客
相关推荐

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应用单元

2011-02-16 09:45:13

PHPPHPUnit

2009-12-23 15:03:52

WPF单元测试

2012-02-07 09:08:50

Feed4JUnitJava

2021-01-07 14:06:30

Spring BootJUnit5Java

2017-02-21 10:30:17

Android单元测试研究与实践

2017-01-14 23:42:49

单元测试框架软件测试

2011-02-21 09:54:14

PHPPHPUnit

2021-08-26 11:00:54

Spring BootJUnit5Java

2009-06-08 20:04:06

EclipseJUnit4单元测试

2012-10-29 09:45:52

单元测试软件测试测试实践

2023-07-26 08:58:45

Golang单元测试

2023-12-28 17:36:10

JUnit5单元测试框架

2022-09-15 10:02:58

测试软件

2009-06-08 19:59:09

EclipseJUnit单元测试

2009-06-08 19:57:29

EclipseJUnit4单元测试
点赞
收藏

51CTO技术栈公众号