国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java編程之單元測試(Junit)實例分析(附實例源碼)

2019-11-26 14:50:52
字體:
供稿:網(wǎng)友

本文實例講述了java編程之單元測試。分享給大家供大家參考,具體如下:

完整實例代碼代碼點(diǎn)擊此處本站下載

在有些時候,我們需要對我們自己編寫的代碼進(jìn)行單元測試(好處是,減少后期維護(hù)的精力和費(fèi)用),這是一些最基本的模塊測試。當(dāng)然,在進(jìn)行單元測試的同時也必然得清楚我們測試的代碼的內(nèi)部邏輯實現(xiàn),這樣在測試的時候才能清楚地將我們希望代碼邏輯實現(xiàn)得到的結(jié)果和測試實際得到的結(jié)果進(jìn)行驗證對比。

廢話少說,上代碼:

首先創(chuàng)建一個java工程,在工程中創(chuàng)建一個被單元測試的Student數(shù)據(jù)類,如下:

package com.phicomme.hu; public class Student {  private String name;  private String sex;  private int high;  private int age;  private String school;  public Student(String name, String sex ,int high, int age, String school)  {   this.name = name;   this.sex = sex;   this.high = high;   this.age = age;   this.school = school;  }  public String getName()  {   return name;  }  public void setName(String name)  {   this.name = name;  }  public String getSex()  {   return sex;  }  public void setSex(String sex)  {   this.sex = sex;  }  public int getHigh()  {   return high;  }  public void setHigh(int high)  {   this.high = high;  }  public int getAge()  {   return age;  }  public boolean setAge(int age)  {   if (age >25)   {    return false;   }   else   {    this.age = age;    return true;   }      }  public String getSchool()  {   return school;  }  public void setSchool(String school)  {   this.school = school;  } }

在eclipse下單元測試這個類:

首先導(dǎo)入Junit包:選中java工程,點(diǎn)擊鼠標(biāo)右鍵--->選擇properties---->在窗口中選Java Build Path---->在右側(cè)點(diǎn)擊Add Library---->在彈出的窗口列表中選中Junit---->下一步----->Junit 4(我用的是Junit 4)---->finish

這樣Junit 4包就導(dǎo)完了,接下來就是創(chuàng)建測試類:

將測試類和被測試類放在不同的包中(也可以放在同一個包中,此處只是為了區(qū)別),代碼如下:

測試類1:

package com.phicomme.test; import com.phicomme.hu.Student; import junit.framework.TestCase; public class StudentTest01 extends TestCase {  Student testStudent;  //此方法在執(zhí)行每一個測試方法之前(測試用例)之前調(diào)用  @Override  protected void setUp() throws Exception  {   // TODO Auto-generated method stub   super.setUp();   testStudent = new Student("djm", "boy", 178, 24, "華東政法");   System.out.println("setUp()");  }  //此方法在執(zhí)行每一個測試方法之后調(diào)用  @Override  protected void tearDown() throws Exception  {   // TODO Auto-generated method stub   super.tearDown();   System.out.println("tearDown()");  }  //測試用例,測試Person對象的getSex()方法  public void testGetSex()  {   assertEquals("boy", testStudent.getSex());   System.out.println("testGetSex()");  }  //測試Person對象的getAge()方法  public void testGetAge()  {   assertEquals(24, testStudent.getAge());   System.out.println("testGetAge()");  } }

測試類2:

package com.phicomme.test; import junit.framework.TestCase; import com.phicomme.hu.Student; public class StudentTest extends TestCase {  private Student testStudent;  @Override  protected void setUp() throws Exception  {   // TODO Auto-generated method stub   super.setUp();   testStudent = new Student("steven_hu", "boy", 170 , 23, "上海理工");  }  @Override  protected void tearDown() throws Exception  {   // TODO Auto-generated method stub   super.tearDown();  }  public void testSetage()  {   assertTrue(testStudent.setAge(21));  }  public void testGetSchool()  {   //預(yù)期值和實際值不一樣,測試時出現(xiàn)失敗(Failure)   assertEquals("南昌大學(xué)", testStudent.getSchool());  }  public void testGetName()  {   assertEquals("hdy", testStudent.getName());  } }

當(dāng)然,如果同時需要一起測試以上這兩個測試類,可以通過TestSuite類實現(xiàn),它相當(dāng)于是一個套件,可以把所有測試類添進(jìn)來一起運(yùn)行測試;

代碼如下:

package com.phicomme.test; import com.phicomme.hu.StudentTest02; import junit.framework.Test; import junit.framework.TestSuite; public class AllTest {  //static PersonTest p = new PersonTest();  //static PersonTest p1 = new PersonTest();  public static Test suite()  {   TestSuite suite = new TestSuite("Test for com.phicomme.test");   //suite.addTest(p);   //suite.addTest(p1);   suite.addTestSuite(StudentTest.class);   suite.addTestSuite(StudentTest01.class);   return suite;  } }

最后,分別測試以上三個類(選中需要測試的類---->鼠標(biāo)右鍵---->Run As---->Junit Test):

StudentTest類的測試結(jié)果圖:

StudentTest01類的測試結(jié)果圖:

AllTest類的測試結(jié)果圖:

有關(guān)java的測試就講到這里,希望對大家有幫助,有時間也會接著講講有關(guān)android的單元測試,和在手機(jī)上實現(xiàn)編寫一個UI界面替代eclipse如上圖中的測試界面;

希望本文所述對大家Java程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新昌县| 开远市| 聂荣县| 仙桃市| 雷山县| 崇左市| 东兰县| 新丰县| 磐安县| 夹江县| 庆安县| 甘孜| 习水县| 凤翔县| 祁阳县| 长葛市| 武穴市| 虞城县| 保康县| 宝丰县| 庆城县| 南雄市| 白河县| 荆门市| 崇文区| 洪泽县| 鹰潭市| 大埔县| 道孚县| 历史| 木兰县| 新蔡县| 贵南县| 泾阳县| 平南县| 太仆寺旗| 合阳县| 木兰县| 桂阳县| 凌源市| 资溪县|