之前寫程序時沒什麼規範,都是想到哪寫到哪,能一遍過簡直就是中彩票了。可是編程指的不只僅是敲代碼的過程,最重要的是動手以前的思路要清晰,把幹什麼、如何幹想清楚。寫小程序的時候或許能夠直接寫,可是真的要作大項目的時候必需要會寫三種代碼java
測試代碼git
需求:咱們要在一個MyUtil類中解決一個百分制成績轉成「優、良、中、及格、不及格」五級製成績的功能。
僞代碼:編程
hundred-mark to five-point: if grade less-than 60, convert to "不及格" if grade between 60 and 70, convert to "及格" if grade between 70 and 80, convert to "中等" if grade between 80 and 90, convert to "良好" if grade between 90 and 100, convert to "優秀" else, convert to "錯誤"
產品代碼:小程序
public class MyUtil{ public static String percentage2fivegrade(int grade){ //若是成績小於60,轉成「不及格」 if (grade >= 0 && grade < 60) { return "不及格"; } //若是成績在60與70之間,轉成「及格」 else if (grade >= 60 && grade < 70) { return "及格"; } //若是成績在70與80之間,轉成「中等」 else if (grade >= 70 && grade < 80) { return "中等"; } //若是成績在80與90之間,轉成「良好」 else if (grade >= 80 && grade < 90) { return "良好"; } //若是成績在90與100之間,轉成「優秀」 else if (grade >= 90 && grade <= 100) { return "優秀"; } //其餘,轉成「錯誤」 else { return "錯誤"; } } }
測試代碼:app
import org.junit.Test; import 考察點1.MyUtil; public class MyUtilTest extends TestCase { @Test public void testNormal(){ assertEquals("不及格", MyUtil.percentage2fivegrade(55)); assertEquals("及格",MyUtil.percentage2fivegrade(65)); assertEquals("中等",MyUtil.percentage2fivegrade(75)); assertEquals("良好",MyUtil.percentage2fivegrade(85)); assertEquals("優秀",MyUtil.percentage2fivegrade(95)); } @Test public void testException(){ assertEquals("錯誤",MyUtil.percentage2fivegrade(200)); assertEquals("錯誤",MyUtil.percentage2fivegrade(-10)); } @Test public void testBoundary(){ assertEquals("不及格",MyUtil.percentage2fivegrade(0)); assertEquals("及格",MyUtil.percentage2fivegrade(60)); assertEquals("中等",MyUtil.percentage2fivegrade(70)); assertEquals("良好",MyUtil.percentage2fivegrade(80)); assertEquals("優秀",MyUtil.percentage2fivegrade(90)); assertEquals("優秀",MyUtil.percentage2fivegrade(100)); } }
使用Junit進行單元測試,分別測試正常、異常、邊界三種狀況,測試經過less
也是第一次據說寫代碼能夠先寫測試代碼再寫產品代碼的,可是仔細想一想其實差很少。咱們每次寫代碼的時候都有個預期,但願實現什麼樣的功能,TDD只是把這個過程體如今測試代碼裏面。
以TDD的方式研究學習StringBuffer
測試代碼ide
package 考察點2; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class StringBufferDemoTest { StringBuffer str12 = new StringBuffer("hereare12chr"); StringBuffer str24 = new StringBuffer("Here are 24 characters!!"); StringBuffer str36 = new StringBuffer("Surprise!!! Here are 36 characters!!"); @Test public void testcharAt(){ Assert.assertEquals('h', str12.charAt(0)); Assert.assertEquals('a', str24.charAt(5)); } @Rule public ExpectedException thrown= ExpectedException.none(); @Test public void testcharAtException(){ thrown.expect(StringIndexOutOfBoundsException.class); str36.charAt(40); } @Test public void testlength(){ Assert.assertEquals(12, str12.length()); Assert.assertEquals(24, str24.length()); Assert.assertEquals(36, str36.length()); } @Test public void testcapacity(){ Assert.assertEquals(28, str12.capacity()); Assert.assertEquals(40, str24.capacity()); Assert.assertEquals(52, str36.capacity()); Assert.assertEquals(82,str24.append("here are 22 characters").capacity()); } @Test public void testindex(){ Assert.assertEquals(4, str12.indexOf("a")); Assert.assertEquals(9, str24.indexOf("24")); Assert.assertEquals(-1, str36.indexOf("?")); } }
查詢文檔基本均可以獲得這些方法的詳細信息,只有capacity方法敘述不是很清楚,我一開始理解錯了,百度之後纔算懂關於Java中StringBuffer的capacity問題,使用不帶參數的構造capacity(),默認的大小爲length()+16,若是大於16就會對length()*2+2。分析源碼應該是最高級最完全的解決辦法了,我必定要學會看源碼ヾ(o・ω・)ノ函數
讓系統支持Double類,並在MyDoc類中添加測試代碼代表添加正確。根據參考連接,咱們能夠把Double類和Int類看作是兩個產品,把Document看作是工廠,由於並無多種類的產品,因此我認爲這裏不須要使用抽象工廠模式,工廠模式就能夠了。單元測試
public class Document { Data pd; Document(String choice) { if (choice.equalsIgnoreCase("Int")){ pd = new Integer(); } if (choice.equalsIgnoreCase("Double")){ pd = new Double(); } } public void displayData(){ pd.displayvalue(); } }
要擴展的話只須要增長一個Data類的子類,並在Document裏增長相應的代碼就能夠了。
任務:以TDD的方式開發一個複數類Complex
先寫測試代碼,要求全都保留兩位小數,toString方法返回a+bi形式
import junit.framework.TestCase; import org.junit.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; public class ComplexTest extends TestCase { Complex a = new Complex(); Complex b = new Complex(2,-3); Complex c = new Complex(1.23,3.21); @Test public void testequals(){ assertTrue(a.equals(new Complex(0,0))); assertFalse(b.equals(a)); } @Test public void testtoString(){ assertEquals("0.00",a.toString()); assertEquals("2.00-3.00i",b.toString()); assertEquals("1.23+3.21i",c.toString()); } @Test public void testComplexAdd(){ assertEquals("2.00-3.00i",a.ComplexAdd(b).toString()); assertTrue(c.ComplexAdd(b).equals(b.ComplexAdd(c))); } @Test public void testComplexSub(){ assertEquals("-0.77+6.21i",c.ComplexSub(b).toString()); assertEquals("-2.00+3.00i",a.ComplexSub(b).toString()); } @Test public void testComplexMul(){ assertEquals("0.00",a.ComplexMulti(b).toString()); assertEquals("12.09+2.73i",b.ComplexMulti(c).toString()); } @Test public void testComplexDiv(){ try { assertEquals("0.00", a.ComplexDiv(b).toString()); assertEquals("-0.61-0.86i", b.ComplexDiv(c).toString()); }catch (ComplexDivideZeroException e){ System.exit(1); } } @Test public void testComplexDivException() { try{ b.ComplexDiv(a); fail("Excepted an ComplexDivideZeroException to be thrown."); }catch (ComplexDivideZeroException e){ assertThat(e.getMessage(),is("除數不能爲0")); } } }
而後再根據測試代碼實現產品代碼並進行調試,最終代碼見碼雲連接
問題1:須要測試Complex類發生除0錯誤時可否正確拋出異常,可是不知道如何使用Junit進行測試
問題1解決方法:JUnit中測試異常拋出的方法有三種解決方法,我只使用了@rule和try...fail...catch兩種方法
問題2:Complex類Double類型比較失敗
問題2解決方法:百度後知道java中double類型直接用==比較的話永遠返回false。另外在重寫equals方法的時候,精度的問題也很頭疼。後來我想到試試看有沒有辦法設置精度,這樣比較起來就會好辦不少,查閱之後知道可使用format方法,在toString方法中把Complex類的double類型字段格式化成保留小數點後兩位,比較的時候也比較toString的返回值。
可是測試的時候值和預期的不太同樣,查閱文檔才知道format的精度控制是自動四捨五入的。。。這人性化的我都不習慣了。。
步驟 | 耗時 | 百分比 |
---|---|---|
需求分析 | 30分鐘 | 15% |
設計 | 30分鐘 | 15% |
代碼實現 | 50分鐘 | 25% |
測試 | 60分鐘 | 30% |
分析總結 | 30分鐘 | 15% |