作者pugboy ()
看板java
标题[问题] 请问关於Junit Test Code的问题
时间Sun Jun 9 15:26:03 2013
版上的大大,不好意思请问
由於最近才刚研究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)
1F:→ qrtt1:为什麽最可疑的 aGradeSystem.showGrade() 没有 source code 06/09 15:41
2F:→ qrtt1:另外哪有 unit test 还要手动输入的 Orz 06/09 15:43
3F:→ qrtt1:把 User Input Interface 跟 logic 隔离出来才是好的方法 06/09 15:47
4F:→ PsMonkey:锁文原因同 qrtt1 第一个推文 06/09 16:02
/* 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)
5F:→ pugboy:这样可以解文嘛QQ 拍谢 06/09 16:08
6F:→ pugboy:回q大 我这样不算隔离出来吗~"~ 谢谢 06/09 16:14
7F:→ qrtt1:勉强算,但很丑。 06/09 16:17