我必须为以下类创建 JUnit 测试,但无法对方法 deposit
进行测试。
public class Account {
private int balance;
private float Interest_Rate =10;
public Account() {
this.balance = 0;
}
public void deposit(int amount) {
balance = balance+amount;
}
}
@Test
public void testdeposit() {
Account i = new Account();
assertEquals("Result",75,i.deposit(25));
}
最佳答案
您可以在 Account
类中添加 getBalance()
方法:
public int getBalance(){
return balance;
}
并用它来进行断言:
@Test
public void deposit(){
Account i = new Account();
i.deposit(25)
assertEquals("Result",25, i.getBalance());
}
通常可以评估和讨论添加仅在单元测试期间使用的方法。
但这里 getBalance()
似乎是不可避免的。
关于java - 使用 void 方法进行 Junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43769465/