英文:
How to create unit test of a transfer of values?
问题
我正在尝试对账户之间转账的方法执行测试用例,但是出现错误:
@Before
public void setup() throws Exception {
origin = new Account();
origin.setBalance(50);
destiny = new Account();
destiny.setBalance(0);
}
@Test
public void transferring_test() throws Exception {
transferring(destiny, 50); // 调用转账方法
Assertions.assertEquals(0, origin.getBalance());
Assertions.assertEquals(50, destiny.getBalance());
}
错误出现在这里:
Assertions.assertTrue(origin.getTransferring(), destiny, 50);
我希望使用assertEquals方法来检查源账户的金额是否已被扣减,以及目标账户的金额是否已被增加。
public void transferring(Account destiny, double value) {
accountRepository.debitBalance(value); // 扣减源账户金额
destiny.setDeposit(value); // 增加目标账户金额
}
英文:
I am trying to perform a test case for a method of transferring between accounts, but it is giving error:
@Before
public void setup() throws Exception {
origin = new Account();
origin.setBalance(50);
destiny = new Account();
destiny.setBalance(0);
@Test
public void transferring_test() throws Exception {
Assertions.assertTrue(origin.getTransferring(),destiny,50);
Assertions.assertEquals(0, origin.getBalance());
Assertions.assertEquals(50,destiny.getBalance());
}
It is accusing error here:
Assertions.assertTrue(origin.getTransferring(),destiny,50);
I want assertEquals methods to check if the source account amount has been debited and the target account amount has been credited.
public void transferring(Account destiny, double value) {
accountRepository.valueBalance(value);
destiny.setDeposit(value);
}
答案1
得分: 0
看起来你没有正确调用transferring
方法。对它进行断言是没有意义的 - 你应该直接调用它,然后断言两个账户的状态:
@Test
public void transferring_test() throws Exception {
origin.transferring(destiny, 50); // 就是这里!
Assertions.assertEquals(0, origin.getBalance());
Assertions.assertEquals(50, destiny.getBalance());
}
英文:
It looks like you aren't calling the transferring
method correctly. There's nothing to assert on it - you should just call it, and then assert the states of the two accounts:
@Test
public void transferring_test() throws Exception {
origin.transferring(destiny, 50); // Here!
Assertions.assertEquals(0, origin.getBalance());
Assertions.assertEquals(50, destiny.getBalance());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论