在插件安裝對話框的搜索中輸入junit
,單擊JunitGenerator V2.0
,單擊綠色的Install
按鈕安裝。由於我沒有次插件,所以實在網上download到本地,再install plugin from disk
git
green bar
,測試經過。以 TDD的方式研究學習StringBuffer,用junit進行單元測試設計模式
運行結果如圖ide
class Integer { int value; public Integer(){ value=100; } public void DisplayValue(){ System.out.println(value); } } class Document { Integer pi; public Document(){ pi = new Integer(); } public void DisplayData(){ pi.DisplayValue(); } } public class MyDoc{ static Document d; public static void main(String [] args) { d = new Document(); d.DisplayData(); } }
讓系統支持Boolean類,Document類要修改構造方法,這還違反了OCP原則。封裝、繼承、多態解決不了問題,在則須要設計模式。函數
abstract class Data { abstract public void DisplayValue(); } class Integer extends Data { int value; Integer() { value=100; } public void DisplayValue(){ System.out.println (value); } } class Boolean extends Data { boolean value; Boolean() { value = true; } public void DisplayValue(){ System.out.println (value); } } // Pattern Classes abstract class Factory { abstract public Data CreateDataObject(); } class IntFactory extends Factory { public Data CreateDataObject(){ return new Integer(); } } class BooleanFactory extends Factory { public Data CreateDataObject(){ return new Boolean(); } } //Client classes class Document { Data pd; Document(Factory pf){ pd = pf.CreateDataObject(); } public void DisplayData(){ pd.DisplayValue(); } } //Test class public class MyDoc { static Document d; public static void main(String[] args) { d = new Document(new BooleanFactory()); d.DisplayData(); } }
如此,再class Boolean extends Data
、class BooleanFactory extends Factory
便可使系統支持Boolean單元測試
// 定義屬性並生成getter,setter double RealPart; double ImagePart; // 定義構造函數 public Complex() public Complex(double R,double I) //Override Object public boolean equals(Object obj) public String toString() // 定義公有方法:加減乘除 Complex ComplexAdd(Complex a) Complex ComplexSub(Complex a) Complex ComplexMulti(Complex a) Complex ComplexDiv(Complex a)
public class Complex{ private double r; private double i; public Complex(double r, double i) { this.r = r; this.i = i; } public static double getRealPart(double r) { return r; } public static double getImagePart(double i) { return i; } public Complex ComplexAdd(Complex c) { return new Complex(r + c.r, i + c.i); } public Complex ComplexSub(Complex c) { return new Complex(r - c.r, i - c.i); } public Complex ComplexMulti(Complex c) { return new Complex(r * c.r - i * c.i, r * c.i + i * c.r); } public Complex ComplexDiv(Complex c) { return new Complex((r * c.i + i * c.r)/(c.i * c.i + c.r * c.r), (i * c.i + r * c.r)/(c.i * c.i + c.r * c.r)); } public String toString() { String s = " "; if (i > 0) s = r + "+" + i + "i"; if (i == 0) s = r + ""; if (i < 0) s = r + " " + i + "i"; return s; } }
嘗試寫測試代碼ComplexTest學習
運行結果測試
junit
,單擊JunitGenerator V2.0
,單擊綠色的Install
按鈕安裝。但我沒有搜到JunitGenerator V2.0
。問題一解決方案:經過百度搜索,在網上查找並下載了http://plugins.jetbrains.com/pluginManager/?action=download&id=org.intellij.plugins.junitgen&build=IU-162.2228.15&uuid=6a6cb9f5-4107-46d7-9b1c-d8205218a73b
,到本地,而後找到插件所在位置,再install plugin from disk
,。ui
問題二:在進行第二步操做時,出現標紅,測試未經過。this
17
換成5
就能夠了。步驟 | 耗時 | 百分比 |
---|---|---|
設計 | 120min | 50% |
代碼實現 | 60 | 25% |
測試 | 30 | 12.5% |
分析總結 | 30 | 12.5% |
本次實驗主要學會了如何使用junit來測試代碼。在不斷調試中,發現junit有他得天獨厚之處,方便,簡潔,快速,收穫很大。.net