如何创建值传递的单元测试?

huangapple go评论75阅读模式
英文:

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());
}

huangapple
  • 本文由 发表于 2020年9月22日 03:27:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63998766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定