写在我的班级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