瞭解設計模式html
MyUtil
MyUtilTest1.java
的測試類:新建一個test文件夾在根目錄中->右鍵選擇Mark Directory as->Test Sources Root
以後在test文件夾中創建一個MyUtilTest1.java
的測試類java
正常狀況
編程
邊界狀況
設計模式
異常狀況
app
點擊源代碼中的類名MyUtil
,選擇Junit3測試用例,創建一個MyUtilTest
測試文件編程語言
若TestCase是紅色,則引入junit.jar包
ide
輸入如圖測試代碼並運行,若是測試失敗則出現如圖提示
函數
根提示據修改源代碼,注意邊界異常狀況,修改完善後測試經過
單元測試
public static void main(String [] args){ StringBuffer buffer = new StringBuffer(); buffer.append('S'); buffer.append("tringBuffer"); System.out.println(buffer.charAt(1)); System.out.println(buffer.capacity()); System.out.println(buffer.length()); System.out.println(buffer.indexOf("tring")); System.out.println("buffer = " + buffer.toString());
public class StringBufferDemo{ StringBuffer buffer = new StringBuffer(); public StringBufferDemo(StringBuffer buffer){ this.buffer = buffer; } public Character charAt(int i){ return buffer.charAt(i); } public int capacity(){ return buffer.capacity(); } public int length(){ return buffer.length(); } public int indexOf(String buf) { return buffer.indexOf(buf); } }
利用API查出並猜想charAt(int i),indexOf(String s),capacity(),length()四種方法的功能。
學習
利用JUnit進行測試,並輸入如圖測試代碼,test passed!
題目 :對設計模式示例進行擴充,體會OCP原則和DIP原則的應用,初步理解設計模式,讓系統支持Byte類,並在MyDoc類中添加測試代碼代表添加正確,提交測試代碼和運行結的截圖,加上學號水印
// Server Classes abstract class Data { abstract public void DisplayValue(); } class Integer extends Data { int value; Integer() { value=100; } public void DisplayValue(){ System.out.println (value); } } class Byte extends Data { byte value; Byte(){ value=127; } public void DisplayValue(){ System.out.println(value); } } abstract class Factory { abstract public Data CreateDataObject(); } class IntFactory extends Factory { public Data CreateDataObject(){ return new Integer(); } } class ByteFactory extends Factory { public Data CreateDataObject(){ return new Byte(); } } class Document { Data pd; Document(Factory pf){ pd = pf.CreateDataObject(); } public void DisplayData(){ pd.DisplayValue(); } } public class MyDoc { static Document d; static Document e; public static void main(String[] args) { e=new Document(new ByteFactory()); e.DisplayData(); } }
// 定義屬性並生成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)
本道練習題基本考察TDD編碼的節奏。由於本題中實部虛部很容易混淆,致使出錯
所以根據測試代碼來修改產品代碼的優越性得以體現。
public class Complex{ private double a; private double b; public Complex(double a, double b) { this.a = a; this.b = b; } public static double getRealPart(double a) { return a; } public static double getImagePart(double b) { return b; } public Complex ComplexAdd(Complex c) { return new Complex(a + c.a, b + c.b); } public Complex ComplexSub(Complex c) { return new Complex(a - c.a, b - c.b); } public Complex ComplexMulti(Complex c) { return new Complex(a * c.a - b * c.b, a * c.b + b * c.a); } public Complex ComplexDiv(Complex c) { return new Complex((a * c.b + b * c.a)/(c.b * c.b + c.a * c.a), (b * c.b + a * c.a)/(c.b * c.b + c.a * c.a)); } public String toString() { String s = " "; if (b > 0) s = a + "+" + b + "i"; if (b == 0) s = a + ""; if (b < 0) s = a + " " + b + "i"; return s; } }
import junit.framework.TestCase; import org.junit.Test; public class ComplexTest extends TestCase { Complex c1 = new Complex(0, 2); Complex c2 = new Complex(-1, -1); Complex c3 = new Complex(1,1); @Test public void testgetRealPart() throws Exception { assertEquals(-3.0, Complex.getRealPart(-3.0)); assertEquals(3.0, Complex.getRealPart(3.0)); assertEquals(0.0, Complex.getRealPart(0.0)); } @Test public void testgetImagePart() throws Exception { assertEquals(-3.0, Complex.getImagePart(-3.0)); assertEquals(3.0, Complex.getImagePart(3.0)); assertEquals(0.0, Complex.getImagePart(0.0)); } @Test public void testComplexAdd() throws Exception { assertEquals("-1.0+1.0i", c1.ComplexAdd(c2).toString()); assertEquals("1.0+3.0i", c1.ComplexAdd(c3).toString()); assertEquals("0.0", c2.ComplexAdd(c3).toString()); } @Test public void testComplexSub() throws Exception { assertEquals("1.0+3.0i", c1.ComplexSub(c2).toString()); assertEquals("-1.0+1.0i", c1.ComplexSub(c3).toString()); assertEquals("-2.0 -2.0i", c2.ComplexSub(c3).toString()); } @Test public void testComplexMulti() throws Exception { assertEquals("2.0 -2.0i", c1.ComplexMulti(c2).toString()); assertEquals("-2.0+2.0i", c1.ComplexMulti(c3).toString()); assertEquals("0.0 -2.0i", c2.ComplexMulti(c3).toString()); } @Test public void testComplexComplexDiv() throws Exception { assertEquals("-1.0 -1.0i", c1.ComplexDiv(c2).toString()); assertEquals("1.0+1.0i", c1.ComplexDiv(c3).toString()); assertEquals("-1.0 -1.0i", c2.ComplexDiv(c3).toString()); } }
public abstract class Animal { private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public abstract String shout(); } public class Dog extends Animal{ public String shout(){ return "汪汪"; } public String toString(){ return "The Dog's color is " + this.getColor() +", and it shouts "+ this.shout() + "!"; } } public class Cat extends Animal{ public String shout(){ return "喵喵"; } public String toString(){ return "The Cat's color is " + this.getColor() +", and it shouts "+ this.shout() + "!"; } }
File -> Project Struct... -> Libraies -> 點擊加號 -> Java -> 選擇IDEA目錄下的Lib中的junit-4.12 ->選擇ok
另外的,若是當時安裝是經過Toolbox,IDEA的安裝目錄則頗有可能被隱藏,所以需搜索junit-4.12找到具體路徑後,在管理員權限下解除隱藏才能選擇。
問題二:單元測試時提示找不到main方法。
解決辦法二: 嘗試修改測試類名與JUnit測試類名不一樣,便可解決。
https://www.cnblogs.com/Vivian517/p/6741501.html#YI