[問題] 請問關於Junit Test Code的問題

看板java作者時間12年前 (2013/06/09 15:26), 編輯推噓0(007)
留言7則, 3人參與, 最新討論串1/1
版上的大大,不好意思請問 由於最近才剛研究TestCode還不是很熟,所以問得不好麻煩請鞭不要客氣 先貼上我的Source Code(changScore Method) / * * changSocre Method主要的功能是會先使用aGradeSystem class的showGrade Method * 功能是顯示舊成績如下Lab1 87 * Lab2 86 * Lab3 98 * Mid-Term 88 * Final Exam 94 *接下來可以 更改凌宗廷Lab1分數? (yes/no) no(輸入Yes就可以改,反之則不行) * 更改凌宗廷Lab2分數? (yes/no) yes * 輸入凌宗廷Lab2新分數 90 * 凌宗廷新分數Lab2 90 改好了 * 更改凌宗廷Lab3分數? (yes/no) no * 更改凌宗廷Mid-term分數? (yes/no) no * 更改凌宗廷Final exam分數? (yes/no) no * 更改分數962001044凌宗廷 完成了 * * * * * * */ public int[] changScore(){ System.out.println("姓名 "+findName(this.ID)); aGradeSystem.showGrade(this.ID); String check; cin = new Scanner(System.in); for(int i=0; i<5; i++){ System.out.println("更改"+findName(this.ID)+"Lab"+(i+1)+"分數? (yes/no)"); check=cin.next(); if(check.equals("yes")){ System.out.println("輸入"+findName(this.ID)+"Lab"+(i+1)+"新分數 :"); newScore[i]=cin.nextInt(); } } return newScore; } 接下來是我的TestCode: /*1~2行是將輸出到console setOut *3行是依序照我的功能描述輸入值 *4行期望的輸出 *5行將exceptReturn實際輸入並建立InContent *6行將inContent setIn *7行實際執行changScore Method *8~11行進行實際test */ public void test1() throws NoSuchIDExceptions { final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); String exceptReturn="yes 100 yes 11 yes 100 yes 100 yes 100" int[] test={100,100,100,100,100}; final ByteArrayInputStream InContent = new ByteArrayInputStream(exceptReturn.getBytes()); System.setIn(InContent); int result[]=aUI.changScore(); assertEquals(test[0],result[0]); assertEquals(test[1],result[1]); assertEquals(test[2],result[2]); assertEquals(test[3],result[3]); assertEquals(test[4],result[4]); } 問題點在於會卡在changScore Method中的aGradeSystem.showGrade();這行 但我又把TestshowGrade的測試碼補上去也還是沒辦法解決 但是最後我直接把changScore Method中的aGradeSystem.showGrade(); 這行註解掉就可以測試成功 我有點在亂槍debug因為實在不知道問題在哪~"~ 但是我去看別人的TestCode,可是其中也有像showGrade() Method在其中但也相安無事 我可以請有寫過TestCode的大大給我一些Hits嘛?或是告訴我哪裡寫錯 不好意思 我知道我的問題問得不是很好...但時間有點急迫,我明天必須有點成果出來,所以... 拜託各位大大了 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.236.1 ※ 編輯: pugboy 來自: 140.115.236.1 (06/09 15:26) ※ 編輯: pugboy 來自: 140.115.236.1 (06/09 15:29)

06/09 15:41, , 1F
為什麼最可疑的 aGradeSystem.showGrade() 沒有 source code
06/09 15:41, 1F

06/09 15:43, , 2F
另外哪有 unit test 還要手動輸入的 Orz
06/09 15:43, 2F

06/09 15:47, , 3F
把 User Input Interface 跟 logic 隔離出來才是好的方法
06/09 15:47, 3F

06/09 16:02, , 4F
鎖文原因同 qrtt1 第一個推文
06/09 16:02, 4F
/* method showGrade ---------------------------------------------- * aGradeSystem會呼叫此method,印出傳入的ID的學生成績. * @param ID 想要查詢學生成績的ID. * * Time estimate : O(n) n:系統內學生的數量. * * Example: * 在系統中有一筆資料為:962001051 李威廷 81 98 84 90 93 91 * aGradeSystem.showGrade("962001044"); * 結果會印出: * 李威廷成績:lab1:81  * lab2:98  * lab3:84  * mid-term :90  * final:93 * total grade :91 */ /* pseudo code * public showGrade(ID) { show 這 ID 的 grade } * */ public void showGrade(String ID) { Grades find=null; for(Grades a:aList) { if(a.getID().equals(ID)) { find=a; } } System.out.println(find.getName()+"成績:"); System.out.println(" Lab1: "+find.getLab1()); System.out.println(" Lab2: "+find.getLab2()); System.out.println(" Lab3: "+find.getLab3()); System.out.println(" mid-term: "+find.getMidTerm()); System.out.println(" final: "+find.getFinalExam()); System.out.println(" totall Grade : "+find.getTotalGrade()); } ※ 編輯: pugboy 來自: 140.115.236.1 (06/09 16:07) ※ 編輯: pugboy 來自: 140.115.236.1 (06/09 16:07)

06/09 16:08, , 5F
這樣可以解文嘛QQ 拍謝
06/09 16:08, 5F

06/09 16:14, , 6F
回q大 我這樣不算隔離出來嗎~"~ 謝謝
06/09 16:14, 6F

06/09 16:17, , 7F
勉強算,但很醜。
06/09 16:17, 7F
文章代碼(AID): #1Hj2uDh_ (java)