需要在java中测试方法的帮助吗
我有以下代码:需要在java中测试方法的帮助吗,java,netbeans,junit,junit4,Java,Netbeans,Junit,Junit4,我有以下代码: for (empCount =0 ; empCount < NUMBEROFEMPLOYEES; empCount = empCount + 1) { System.out.println("Ref NO: " + spRefNo[empCount] + " \tSalespersons Name: " + spName[empCount]); System.out.println("...........................
for (empCount =0 ; empCount < NUMBEROFEMPLOYEES; empCount = empCount + 1)
{
System.out.println("Ref NO: " + spRefNo[empCount] + " \tSalespersons Name: " +
spName[empCount]);
System.out.println("...............................................");
System.out.println("Type in the salespersons monthly sales");
System.out.println("...............................................");
spMonthlySales[empCount]=InOut.readDouble();
//If to calculate sales persons monthly sales over 10000 pounds
if (spMonthlySales[empCount]>=10000)
{
spCommission [empCount] = (spMonthlySales [empCount] * (HIGHCOMRATE/100));
spGrossPay [empCount] = STAFFBASIC + spCommission [empCount];
spDeductions [empCount] = (spGrossPay [empCount] * (DEDUCTIONSPERCENTAGE/100));
spNettPay [empCount] = (spGrossPay [empCount] - spDeductions [empCount]);
}
}
for(empCount=0;empCount=10000)
{
spCommission[empCount]=(spMonthlySales[empCount]*(高通信速率/100));
spGrossPay[empCount]=员工基础+spCommission[empCount];
p扣除额[empCount]=(spGrossPay[empCount]*(扣除百分比/100));
spNettPay[empCount]=(spGrossPay[empCount]-spDeductions[empCount]);
}
}
我正在寻找在JUnit中为这段代码编写测试的帮助——我知道如何测试计算出的值,但我不知道如何模拟该方法要求的用户输入。代码基本上是无关的,我还不担心如何测试它,我认为这只是测试中需要模拟的用户输入。有人能帮我解决这个问题,或者给我指出正确的方向来找到答案吗?我建议重写函数,使用类似于
扫描仪的对象()。您可以在生产代码中的系统上安装扫描仪
,在测试中的字符串
对象上安装扫描仪
(例如Scanner sc=new Scanner(“这是测试字符串”)
。您可以使用库提供输入和测试输出
完全公开:我是系统规则的作者。将代码分为两种方法。一个用于获取输入,另一个仅用于处理目的。因此,您需要测试处理方法。这似乎是最好的做法,但这是针对作业的,我不确定是否可以重写代码。这是针对作业的,我认为我无法重写代码。有什么方法可以按原样执行吗?StackOverflow正在突出显示InOut
语法,但实际上是什么?它只是从键盘读取用户输入,似乎readDouble()会验证或尝试将输入解析为双精度,如果我键入一个字母,它将返回到for循环的开头,输出错误为“错误:不是一个实数”。实际上似乎非常方便,它似乎不需要编写任何类型的验证循环,它只是自己做。有一个单独的类叫做InOut,它似乎处理键盘输入,使用java.io和System.in.read()阅读输入。实际上,忽略我的上一条评论,它只接受键盘输入,检查是否为双精度输入,如果不是,则返回0.0,并继续循环,不再要求输入。非常感谢,我将尝试一下,看看效果如何。
public class YourTest {
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Rule
public TextFromStandardInputStream systemInMock = emptyStandardInputStream();
@Test
public void test() {
systemInMock.provideLines("first", "second");
... //execute your code
assertEquals("First!\nSecond!\n", log.getLogWithNormalizedLineSeparator());
}
}