单元测试工具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 } 53 54 }

其中的@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 7 public class TestAll { 8 public static Test suite(){ 9 TestSuite suite = new TestSuite("Running all the tests"); 10 suite.addTestSuite(ShowStrategyDaoTest.class); 11 suite.addTestSuite(com.boco.bomc.alarmrelevance.show.dao.ShowStrategyDaoTest.class); 12 return suite; 13 } 14 }
另外还可以把多个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 } 14 }
这就更方便与集中测试,一个方法测试完了,可以对个方法,多个类一起测试
注意:在写代码的时候TestSuite,TestCase,Test的包不要到错了
测试效果如下:

----->froest
My cnblogs is :
http://www.cnblogs.com/God-froest/
Welcome you to exchange technology with me.
《夜的钢琴曲八》
---石进
Piano music can develop the temperament of a man.
|