java JUnit在else语句以字符串形式输出错误消息时测试方法
我正在开发一个处理银行交易的程序。我的取款方法从余额中减去金额,如果没有足够的资金取款,将向屏幕输出一条错误消息。错误消息是我在测试时遇到的问题
public void withdraw(double amt) {
double bal = getBalance();
if(bal >= amt) {
super.setBalance(bal - amt);
if(bal < minBalance) {
super.setBalance(bal - amt - maintFee);
}
} else {
System.out.println("Error: Not enough funds for withdraw");
}
}
这些是我目前对此方法的JUnit测试。我只需要关于testdrawThree()的帮助。谢谢大家!
@Test
void testWithdrawOne() {
Savings s = new Savings(500.00, 30.00, "111", "Andrew Green", 1000.00);
s.withdraw(200);
assertEquals(800.00, s.getBalance());
}
@Test
void testWithdrawTwo() {
Savings s = new Savings(500.00, 30.00, "111", "Andrew Green", 400.00);
s.withdraw(200.00);
assertEquals(170.00, s.getBalance());
}
@Test
void testWithdrawThree() {
Savings s = new Savings(500.00, 30.00, "111", "Andrew Green", 400.00);
s.withdraw(600.00);
//Need to check that program will output "Error: Not enough funds for withdrawal"
}
# 1 楼答案
有几种方法可以做到这一点:
另外,还有一个旁注:用有意义的名称来命名测试方法将大有帮助,例如
testWithdrawWithoutFunds
,而不是泛型名称testOne
、testTwo
,等等# 2 楼答案
在我看来,最好的方法就是按照Yoni的建议进行重构。除了更改方法的签名(可能因为它是在接口中定义的而不可能),您的原始方法可以调用另一个方法,您可以传递要使用的
PrintStream
:您的测试类(位于同一个包中,并且therfor能够访问
performWithdraw
)如下所示:顺便说一句:您应该测试余额是否保持不变(在我的示例中,我添加了一个相应的
assertEquals
),并且您还应该检查边缘情况,即检查您在提取400.01时是否收到错误消息。还应检查边缘案例是否收取维护费用BTW2:使用
double
表示货币金额是一件坏事[TM]。学习JUnit没关系,但对于真正的应用程序[TM],你应该使用BigDecimal