Junit3 junit.framework 單元測試,簡單例項說明.
阿新 • • 發佈:2019-01-10
在有些時候,我們需要對我們自己編寫的程式碼進行單元測試(好處是,減少後期維護的精力和費用),這是一些最基本的模組測試。當然,在進行單元測試的同時也必然得清楚我們測試的程式碼的內部邏輯實現,這樣在測試的時候才能清楚地將我們希望程式碼邏輯實現得到的結果和測試實際得到的結果進行驗證對比。
首先建立一個java工程,在工程中建立一個被單元測試的Student資料類,如下:
- package com.phicomme.hu;
- publicclass Student
- {
- private String name;
-
private String sex;
- privateint high;
- privateint 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;
- }
- publicvoid setName(String name)
- {
- this.name = name;
- }
- public String getSex()
- {
- return sex;
- }
- publicvoid setSex(String sex)
- {
- this.sex = sex;
-
}
- publicint getHigh()
- {
- return high;
- }
- publicvoid setHigh(int high)
- {
- this.high = high;
- }
- publicint getAge()
- {
- return age;
- }
- publicboolean setAge(int age)
- {
- if (age >25)
- {
- returnfalse;
- }
- else
- {
- this.age = age;
- returntrue;
- }
- }
- public String getSchool()
- {
- return school;
- }
- publicvoid setSchool(String school)
- {
- this.school = school;
- }
- }
在eclipse下單元測試這個類:
首先匯入Junit包:選中java工程,點選滑鼠右鍵--->選擇properties---->在視窗中選Java Build Path---->在右側點選Add Library---->在彈出的視窗列表中選中Junit---->下一步----->Junit 4(我用的是Junit 4)---->finish( Junit3的jar 用Junit4的jar也可以, 所以不用奇怪Junit3用Junit4的Jar)
這樣Junit 4包就導完了,接下來就是建立測試類:
將測試類和被測試類放在不同的包中(也可以放在同一個包中,此處只是為了區別),程式碼如下:
測試類1:
- package com.phicomme.test;
- import com.phicomme.hu.Student;
- import junit.framework.TestCase;
- publicclass StudentTest01 extends TestCase
- {
- Student testStudent;
- //此方法在執行每一個測試方法之前(測試用例)之前呼叫
- @Override
- protectedvoid setUp() throws Exception
- {
- // TODO Auto-generated method stub
- super.setUp();
- testStudent = new Student("djm", "boy", 178, 24, "華東政法");
- System.out.println("setUp()");
- }
- //此方法在執行每一個測試方法之後呼叫
- @Override
- protectedvoid tearDown() throws Exception
- {
- // TODO Auto-generated method stub
- super.tearDown();
- System.out.println("tearDown()");
- }
- //測試用例,測試Person物件的getSex()方法
- publicvoid testGetSex()
- {
- assertEquals("boy", testStudent.getSex());
- System.out.println("testGetSex()");
- }
- //測試Person物件的getAge()方法
- publicvoid testGetAge()
- {
- assertEquals(24, testStudent.getAge());
- System.out.println("testGetAge()");
- }
- }
測試類2:
- package com.phicomme.test;
- import junit.framework.TestCase;
- import com.phicomme.hu.Student;
- publicclass StudentTest extends TestCase
- {
- private Student testStudent;
- @Override
- protectedvoid setUp() throws Exception
- {
- // TODO Auto-generated method stub
- super.setUp();
- testStudent = new Student("steven_hu", "boy", 170 , 23, "上海理工");
- }
- @Override
- protectedvoid tearDown() throws Exception
- {
- // TODO Auto-generated method stub
- super.tearDown();
- }
- publicvoid testSetage()
- {
- assertTrue(testStudent.setAge(21));
- }
- publicvoid testGetSchool()
- {
- //預期值和實際值不一樣,測試時出現失敗(Failure)
- assertEquals("南昌大學", testStudent.getSchool());
- }
- publicvoid testGetName()
- {
- assertEquals("hdy", testStudent.getName());
- }
- }
當然,如果同時需要一起測試以上這兩個測試類,可以通過TestSuite類實現,它相當於是一個套件,可以把所有測試類添進來一起執行測試;
程式碼如下:
- package com.phicomme.test;
- import com.phicomme.hu.StudentTest02;
- import junit.framework.Test;
- import junit.framework.TestSuite;
- publicclass AllTest
- {
- //static PersonTest p = new PersonTest();
- //static PersonTest p1 = new PersonTest();
- publicstatic Test suite()
- {