單元測試并不能證明你的代碼是正确的,隻能證明你的代碼是沒有錯誤的。
keep bar green and keep your code cool
第一種方式<4.0的junit版本
1、 在已經編寫好的項目中建立一個package用于單元測試。
2、 要在buildpath中加入junit對應的包。
3、 建立一個類,比如unittest
import static org.junit.assert.*;
import junit.framework.testcase;
import org.junit.test;
5、 編寫自己的測試函數,可以編寫多個,感覺上每個函數都相當于一個main方法,要注意的是需要用來執行的函數都要以test開頭。
6、 在對應的測試類上點選run as 之後點選junit test 就可以執行對應的test開頭的方法了。
第二種方式>=4.0的junit版本
1、 這種方式是基于注解來進行的,先要加上對應的包import org.junit.test,其他的就不用加了。
2、 類名不需要繼承testcase,測試方法也不需要以test開頭。
3、 隻需要在方法的前面加上@test的注解,之後 run as—>junit test這樣就會自動對加了注解的方法進行測試。
使用注解的方式還是比較推薦的,最好在利用注解的時候方法名也能與之前的保持一緻,這樣就能與4.0版本之前的junit相容了。
這種方式的大緻原理還是利用反射,先獲得class類執行個體,之後利用getmethods方法得到這個類的所有的方法,之後周遊這個方法,判斷每個方法是否加上了@test注解,如果加上了注解,就執行。大多數架構内部都是依靠反射來進行的。實際情況中還是比較推薦使用注解的,還有一些常用的注解,比如:@before @after這兩個分别表示方法(@test之後的)執行之前要執行的部分,以及方法執行之後要執行的部分,注意這裡每個被@test标注過的方法在執行之前與執行之後都要執行@before以及@after标注過的方法,是以被這兩個注解标記過的方法可能會執行多次。
對于@beforeclass以及@afterclass顧名思義就表示在整個測試類執行之前與執行之後要執行的方法,被這兩個注解标記過的方法在整個類的測試過程中隻是執行一次。
還有一個常用到的方法是assert.assertequals方法,表示預期的結果是否與實際出現的結果是否一緻,可以有三個參數,第一個參數表示不一緻時候的報錯資訊,第二個參數表示期望的結果,第三個參數表示實際的結果。
還有一部分是關于組合模式的使用,比如寫了好多的測試類,atest btest ....總不能一個一個點,能一起讓這些測試類都運作起來就是最好不過了,這時候要使用到兩個注解:@runwith(suite.class)以及@suiteclasses({ xxtest.class,xxtest.class })
當然junit的整個過程中還涉及到了許多經典的設計模式,這個再進一步進行分析。
下面是一個實際的例子,展示一下常見的幾個注解的使用:
//一個簡單的student類以及一個teacher類 輸出其基本資訊
package com.test.unittest;
public class student {
int id;
int age;
string name;
public student(int id, int age, string name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
public int getid() {
return id;
public void setid(int id) {
public int getage() {
return age;
public void setage(int age) {
public string getname() {
return name;
public void setname(string name) {
public void info()
{
system.out.println("the stu info:"+this.age+" "+this.id+" "+this.name);
public class teacher {
string tname;
string tage;
public teacher(string tname, string tage) {
this.tname = tname;
this.tage = tage;
public string gettname() {
return tname;
public void settname(string tname) {
public string gettage() {
return tage;
public void settage(string tage) {
public void info(){
system.out.println("the teacher info:"+this.tage+" " +this.tname);
後面這部分就是對兩個類進行的單元測試以及一個組合方式的使用
package com.unittest;
import org.junit.after;
import org.junit.before;
import org.junit.test;
import com.test.unittest.student;
public class studenttest {
student stu=new student(1,23,"令狐沖");
@before
public void setup(){
system.out.println("student initial");
@test
public void infotest()
stu.info();
@after
public void teardown(){
system.out.println("student destroy");
import com.test.unittest.teacher;
public class teachertest {
teacher teacher=new teacher("風清揚","90");
system.out.println("teacher initial");
teacher.info();
system.out.println("teacher destroy");
import org.junit.runner.runwith;
import org.junit.runners.suite;
import org.junit.runners.suite.suiteclasses;
@runwith(suite.class)
@suiteclasses({studenttest.class,teachertest.class})
public class alltest {
/*輸出的結果如下:
student initial
the stu info:23 1 令狐沖
student destroy
teacher initial
the teacher info:90 風清揚
teacher destroy
*/
補充說明:
寫作業的時候把測試類一個一個手敲進去,真是太out了,還是用eclipse中自帶的生成junit test的類比較好一點,直接在測試的那個package下面,建立一個新的junit test class 標明版本以及標明class under test 這個表示你希望生成哪一個類的測試類,這樣生成的測試類中命名也比較規範,比如相同的方法名不同參數的方法,連參數類型都寫上去了,比以前直接用a b c d...1 2 3 4....來差別同名的方法正規多了....
最新内容請見作者的github頁:http://qaseven.github.io/