寫在我的班級Junit的@試驗方法,在運行時
public static Advertisement[] createAd() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Advertisement[] ad = new Advertisement[5];
int count =0;
char ch;
do
{
System.out.println("Enter advertisement id:");
int id=Integer.parseInt(br.readLine());
System.out.println("Enter advertisement type:");
String type=br.readLine();
ad[count]= new Advertisement(id, type);
count++;
System.out.println("Do you want publish another advertisement(Y/N):");
ch = br.readLine().charAt(0);
if((ch!='y')&&(ch!='Y'))
{
break;
}
}while(count<=5);
if(count==5)
{
System.out.println("Maximum ads reached");
return ad;
}
return ad;
}
Juint @Test method
@Test
public void testCreateAd() throws IOException
{
Advertisement[] a = Advertisement.createAd();
assertTrue("Maximum five ads only should be created",(a.length<=5));
}
我要檢查方法給予輸入,createAd方法應該返回類型廣告的5對象的最大。但是當@ Test方法運行時,它會詢問輸入,我們是否可以通過某些方法將輸入傳遞給那些readLine方法
要看的東西:https://stackoverflow.com/a/6416591/1531124。事情是:你*可以*使用一些技巧測試從System.in中讀取的代碼。但是:你不應該這樣做。您完全*隱藏*您的代碼通常從生產代碼中的標準輸入讀取的事實。以便它可以與*任何*種輸入一起使用。 – GhostCat