Java是一門面向?qū)ο?a target="_blank">編程語言,不僅吸收了C++語言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。
Java具有簡(jiǎn)單性、面向?qū)ο蟆⒎植际?、健壯性、安全性、?**立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
單元測(cè)試(unit testing),是指對(duì)軟件中的最小可測(cè)試單元進(jìn)行檢查和驗(yàn)證。對(duì)于單元測(cè)試中單元的含義,一般來說,要根據(jù)實(shí)際情況去判定其具體含義,如C語言中單元指一個(gè)函數(shù),Java里單元指一個(gè)類,圖形化的軟件中可以指一個(gè)窗口或一個(gè)菜單等。總的來說,單元就是人為規(guī)定的最小的被測(cè)功能模塊。單元測(cè)試是在軟件開發(fā)過程中要進(jìn)行的最低級(jí)別的測(cè)試活動(dòng),軟件的獨(dú)立單元將在與程序的其他部分相隔離的情況下進(jìn)行測(cè)試。
在一種傳統(tǒng)的結(jié)構(gòu)化編程語言中,比如C,要進(jìn)行測(cè)試的單元一般是函數(shù)或子過程。在像C++這樣的面向?qū)ο蟮恼Z言中, 要進(jìn)行測(cè)試的基本單元是類。對(duì)Ada語言來說,開發(fā)人員可以選擇是在獨(dú)立的過程和函數(shù),還是在Ada包的級(jí)別上進(jìn)行單元測(cè)試。單元測(cè)試的原則同樣被擴(kuò)展到第四代語言(4GL)的開發(fā)中,在這里基本單元被典型地劃分為一個(gè)菜單或顯示界面。
經(jīng)常與單元測(cè)試聯(lián)系起來的另外一些開發(fā)活動(dòng)包括代碼走讀(Code review),靜態(tài)分析(Static analysis)和動(dòng)態(tài)分析(Dynamic analysis)。靜態(tài)分析就是對(duì)軟件的源代碼進(jìn)行研讀,查找錯(cuò)誤或收集一些度量數(shù)據(jù),并不需要對(duì)代碼進(jìn)行編譯和執(zhí)行。動(dòng)態(tài)分析就是通過觀察軟件運(yùn)行時(shí)的動(dòng)作,來提供執(zhí)行跟蹤,時(shí)間分析,以及測(cè)試覆蓋度方面的信息。
java單元測(cè)試的寫法
首先創(chuàng)建一個(gè)java工程,在工程中創(chuàng)建一個(gè)被單元測(cè)試的Student數(shù)據(jù)類,如下:
?。踛ava] view plain copypackage 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下單元測(cè)試這個(gè)類:
首先導(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)建測(cè)試類:
將測(cè)試類和被測(cè)試類放在不同的包中(也可以放在同一個(gè)包中,此處只是為了區(qū)別),代碼如下:
測(cè)試類1:
[java] view plain copypackage com.phicomme.test;
import com.phicomme.hu.Student;
import junit.framework.TestCase;
public class StudentTest01 extends TestCase
{
Student testStudent;
//此方法在執(zhí)行每一個(gè)測(cè)試方法之前(測(cè)試用例)之前調(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í)行每一個(gè)測(cè)試方法之后調(diào)用
@Override
protected void tearDown() throws Exception
{
// TODO Auto-generated method stub
super.tearDown();
System.out.println(“tearDown()”);
}
//測(cè)試用例,測(cè)試Person對(duì)象的getSex()方法
public void testGetSex()
{
assertEquals(“boy”, testStudent.getSex());
System.out.println(“testGetSex()”);
}
//測(cè)試Person對(duì)象的getAge()方法
public void testGetAge()
{
assertEquals(24, testStudent.getAge());
System.out.println(“testGetAge()”);
}
}
測(cè)試類2:
?。踛ava] view plain copypackage 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ù)期值和實(shí)際值不一樣,測(cè)試時(shí)出現(xiàn)失?。‵ailure)
assertEquals(“南昌大學(xué)”, testStudent.getSchool());
}
public void testGetName()
{
assertEquals(“hdy”, testStudent.getName());
}
}
當(dāng)然,如果同時(shí)需要一起測(cè)試以上這兩個(gè)測(cè)試類,可以通過TestSuite類實(shí)現(xiàn),它相當(dāng)于是一個(gè)套件,可以把所有測(cè)試類添進(jìn)來一起運(yùn)行測(cè)試;
代碼如下:
?。踛ava] view plain copypackage 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;
}
}
最后,分別測(cè)試以上三個(gè)類(選中需要測(cè)試的類----》鼠標(biāo)右鍵----》Run As----》Junit Test):
StudentTest類的測(cè)試結(jié)果圖:
StudentTest01類的測(cè)試結(jié)果圖:
AllTest類的測(cè)試結(jié)果圖:
有關(guān)java的測(cè)試就講到這里,希望對(duì)大家有幫助,有時(shí)間也會(huì)接著講講有關(guān)android的單元測(cè)試,和在手機(jī)上實(shí)現(xiàn)編寫一個(gè)UI界面替代eclipse如上圖中的測(cè)試界面;
評(píng)論