New->Directory
新建一個test目錄,再右鍵點擊設置環境變量,選擇Mark Directory->Test Sources Roo
t便可正常狀況
html
邊界狀況:通常容易遺漏邊界狀況,並且容易出錯
java
異常狀況 ->依據所出現的異常狀況,就應該針對此問題修改源代碼,直至測試成功。
git
下載完成後,在IDEA中新建空類,鼠標單擊類名會出現一個燈泡狀圖標,單擊圖標或按Alt + Enter,在彈出的菜單中選擇Create Test
編程
選擇建立JUnit3的測試用例
設計模式
C:\Users\13015\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-1\171.4073.35\lib\junit.jar
。charAt返回該位置上的字符
安全
indexOf返回第一次出現的指定子字符串在該字符串中的索引
網絡
String類:String的值是不可變的,所以每次對String操做都會生成新的String對象,浪費大量內存空間。爲理解這個,我從書上摘取了一個小栗子~a最後指向56EF,但最後12AB和56EF地址中的數據仍然存在,所以String的操做都是改變賦值地址而不是改變值操做。
String a = "你好"
a = "boy"
a = "12.97"
多線程
StringBuffer是可變類,任何對它指向的字符串的操做都不會產生新的對象。每一個StringBuffer對象都有必定的緩衝區容量,當字符串大小沒有超過容量時,不會分配新的容量,當字符串大小超過容量時,會自動增長容量,很是人性化。
StringBuffer buf=new StringBuffer(); //分配默認長16字節的字符緩衝區
StringBuffer buf=new StringBuffer(512); //分配長512字節的字符緩衝區
StringBuffer buf=new StringBuffer("this is a test")//在緩衝區中存放了字符串,並在後面預留了16字節的空緩衝區。
app
// Sever Classer abstract class Data{ public abstract void DisplayValue(); } class Integer extends Data{ int value; Integer(){ value = 100; } public void DisplayValue(){ System.out.println(value); } } class Double extends Data{ double value; Double(){ value = 5.0; } public void DisplayValue(){ System.out.println(value); } } // Pattern Classes abstract class Factory{ public abstract Data CreateDataObject(); } class IntFactory extends Factory{ public Data CreateDataObject(){ return new Integer(); } } class DoubleFactory extends Factory{ public Data CreateDataObject(){ return new Double(); } } //Client classes class Document { Data pd; Document(Factory pf){ pd = pf.CreateDataObject(); } public void DisplayData(){ pd.DisplayValue(); } } public class MyDoc { static Document d; static Document f; public static void main(String[] args) { d = new Document(new IntFactory()); d.DisplayData(); f = new Document(new DoubleFactory()); f.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)
public class MyComplex{ //定義屬性並生成getter,setter private double realPart; private double imagePart; public double getRealPart(){ return realPart; } public double getImagePart(){ return imagePart; } //定義構造函數 public MyComplex(){} public MyComplex(double r,double i){ realPart = r; imagePart = i; } //Override Object @Override public boolean equals(Object obj){ if(this == obj){ return true; } if(!(obj instanceof MyComplex)) { return false; } MyComplex complex = (MyComplex) obj; if(complex.realPart != ((MyComplex) obj).realPart) { return false; } if(complex.imagePart != ((MyComplex) obj).imagePart) { return false; } return true; } @Override public String toString(){ String s = new String(); if(imagePart >0 && realPart!=0){ s = ""+getRealPart()+"+"+getImagePart()+"i"; } if(imagePart >0 && realPart==0){ s= getImagePart()+"i"; } if(imagePart <0 &&realPart!=0) { s = ""+getRealPart()+getImagePart()+"i"; } if(imagePart <0 && realPart==0){ s= getImagePart()+"i"; } if(imagePart ==0 &&realPart!=0) { s = ""+getRealPart(); } if(imagePart ==0 && realPart==0){ s= "0"; } return s; } //定義公有方法:加減乘除 public MyComplex complexAdd(MyComplex a){ return new MyComplex(realPart+a.realPart, imagePart +a.imagePart); } public MyComplex complexSub(MyComplex a){ return new MyComplex(realPart-a.realPart, imagePart -a.imagePart); } public MyComplex complexMulti(MyComplex a){ return new MyComplex(realPart * a.realPart - imagePart * a.imagePart, realPart * a.imagePart + imagePart * a.realPart); } public MyComplex complexDiv(MyComplex a){ return new MyComplex((realPart * a.realPart + imagePart * a.imagePart) / (a.imagePart * a.imagePart + a.realPart * a.realPart), (imagePart * a.realPart - realPart * a.imagePart) / (a.realPart * a.realPart + a.realPart * a.realPart)); } }
import junit.framework.TestCase; import org.junit.Test; public class MyComplexTest extends TestCase { MyComplex a = new MyComplex(2.0,4.0); MyComplex b = new MyComplex(0.0,-3.0); MyComplex c = new MyComplex(-5.0,0.0); @Test public void testgetRealpart(){ assertEquals(2.0,a.getRealPart()); assertEquals(0.0,b.getRealPart()); assertEquals(-5.0,c.getRealPart()); } @Test public void testgetImagePart(){ assertEquals(4.0,a.getImagePart()); assertEquals(-3.0,b.getImagePart()); assertEquals(0.0,c.getImagePart()); } @Test public void testMyComplexAdd(){ String q = a.complexAdd(b).toString(); String w = b.complexAdd(c).toString(); String e = c.complexAdd(a).toString(); assertEquals("2.0+1.0i",q); assertEquals("-5.0-3.0i",w); assertEquals("-3.0+4.0i",e); } @Test public void testMyComplexSub(){ String r = a.complexSub(b).toString(); String t = b.complexSub(c).toString(); String y = c.complexSub(a).toString(); assertEquals("2.0+7.0i",r); assertEquals("5.0-3.0i",t); assertEquals("-7.0-4.0i",y); } @Test public void testMyComplexMulti(){ String u = a.complexMulti(b).toString(); String i = b.complexMulti(c).toString(); String o = c.complexMulti(a).toString(); assertEquals("12.0-6.0i",u); assertEquals("15.0i",i); assertEquals("-10.0-20.0i",o); } @Test public void testMyComplexDiv(){ String p = c.complexDiv(a).toString(); assertEquals("-0.5+2.5i",p); } @Test public void testtoString(){ assertEquals("2.0+4.0i",a.toString()); assertEquals("-3.0i",b.toString()); assertEquals("-5.0",c.toString()); } }
Q: junit 使用org.junit不存在,點到代碼中紅色的部分顯示:Cannot resolve symbol 'junit'
編程語言
A:File -> Project Struct... -> Libraies -> 點擊綠色的加號 -> Java -> 找到 IDEA 安裝路徑下的 Lib 中的junit-4.12 ->點擊OK
Q:在對append進行測試的時候,明明指望的值和實際值相同,但仍是測試失敗
A:將StringBuffer轉爲字符串再比較,便可得出答案
public void testappend() throws Exception{ String q,w,e; a = a.append("abc"); b = b.append("abc"); c = c.append(ch,2,3); q = a.toString(); w = b.toString(); e = c.toString(); assertEquals("StringBufferabc",q); assertEquals("StringBufferStringBufferabc",w); assertEquals("StringBufferStringBufferStringBufferc12",e); }