测试到字符串 Junit

2022-09-04 06:58:06

给定一个 toString 方法:

public String toString()
{
    String accountString;
    NumberFormat money = NumberFormat.getCurrencyInstance();

    accountString = cust.toString();
    accountString += " Current balance is " + money.format (balance);
    return accountString;
}

我怎么能用朱尼特测试它?


答案 1

以下是我的做法:

public class AccountTest
{
    @Test
    public void testToString()
    {
        Account account = new Account(); // you didn't supply the object, so I guessed
        String expected = ""; // put the expected value here
        Assert.assertEquals(expected, account.toString());
    }
}

答案 2

一个非常好的库,用于在模型类上测试toString:https://github.com/jparams/to-string-verifier

样本:

@Test
public void myTest()
{
    ToStringVerifier.forClass(Student.class).verify();
}