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

成都创新互联是一家专业提供吉隆企业网站建设,专注与成都网站设计、成都做网站、HTML5建站、小程序制作等业务。10年已为吉隆众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
1.创建相应的test类
package:测试类存放位置。
Name:测试类名字。
setUp,tearDown:测试类创建测试环境以及销毁测试环境,这两个方法只执行一次。
Class Under test:需要被测试的类路径及名称。
点击下一步就会让你选择需要给哪些方法进行测试。
测试类创建完成后在类中会出现你选择的方法的测试方法:
- package test.com.boco.bomc.alarmrelevance.show.dao;
 - import junit.framework.TestCase;
 - import org.junit.After;
 - import org.junit.Before;
 - import org.junit.BeforeClass;
 - import org.junit.Test;
 - public class ShowStrategyDaoTest extends TestCase{
 - @BeforeClass
 - public static void setUpBeforeClass() throws Exception {
 - System.out.println("OK1");
 - }
 - @Before
 - public void setUp() throws Exception {
 - }
 - @After
 - public void tearDown() throws Exception {
 - }
 - @Test
 - public final void testGetDataByApplyNameOrHostIp() {
 - fail("Not yet implemented"); // TODO
 - }
 - @Test
 - public final void testGetDataByObject() {
 - fail("Not yet implemented"); // TODO
 - }
 - @Test(timeout=1)
 - public final void testGetApplyUser() {
 - fail("Not yet implemented"); // TODO
 - }
 - @Test
 - public final void testGetVoiceUser() {
 - fail("Not yet implemented"); // TODO
 - }
 - @Test
 - public final void testSearchInAera() {
 - fail("Not yet implemented"); // TODO
 - }
 - @Test
 - public final void testGetDataByPolicyId() {
 - fail("Not yet implemented"); // TODO
 - }
 - }
 
其中的@before,@test,@after表示在执行测试方法前执行,需执行的测试方法,在测试方法执行后执行。
可以给@test添加timeout,exception参数。
在测试方法中可以用assertEquals(arg0,arg1);
可以用TestSuite把多个测试类集中到一起,统一执行测试,例如:
- package test.com.boco.bomc.alarmrelevance.show.dao;
 - import junit.framework.Test;
 - import junit.framework.TestSuite;
 - public class TestAll {
 - public static Test suite(){
 - TestSuite suite = new TestSuite("Running all the tests");
 - suite.addTestSuite(ShowStrategyDaoTest.class);
 - suite.addTestSuite(com.boco.bomc.alarmrelevance.show.dao.ShowStrategyDaoTest.class);
 - return suite;
 - }
 - }
 
另外还可以把多个TestSuite组合到一个Test类里面,例如:
- package test.com.boco.bomc.alarmrelevance.show.dao;
 - import junit.framework.Test;
 - import junit.framework.TestCase;
 - import junit.framework.TestSuite;
 - public class TestAll1 extends TestCase {
 - public static Test suite(){
 - TestSuite suite1 = new TestSuite("TestAll1");
 - suite1.addTest(TestAll.suite());
 - suite1.addTest(TestAll2.suite());
 - return suite1;
 - }
 - }
 
这就更方便与集中测试,一个方法测试完了,可以对个方法,多个类一起测试。
注意:在写代码的时候TestSuite,TestCase,Test的包不要到错了。
测试效果如下:
原文链接:http://www.cnblogs.com/God-froest/archive/2011/11/18/JunitTest.html
编辑推荐:
                文章标题:单元测试利器JUnit的实践与总结
                
                标题来源:http://www.csdahua.cn/qtweb/news45/547345.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网