【发布时间】:2019-11-23 15:56:11
【问题描述】:
我需要您的帮助来理解单元测试类中的单元(方法)行为,如下所示。
public void updateAccount(Account account) {
if (!accountExists(account.getAccountNo())) {
throw new AccountNotFoundException();
}
accounts.put(account.getAccountNo(), account);
}
上面显示的方法告诉我如果找不到账号就会抛出异常
但是,下面显示的第二个测试 (updateNotExistedAccount) 方法表明,上面的方法 (updateAccount) 应该抛出异常以通过测试。但是,newAccount 已经在 createNewAccount 中初始化/创建,因此它已经存在。所以我假设updateNotExistedAccount 会通过测试(因为updateAccount 在这种情况下不会抛出异常),但是updateNotExistedAccount 通过了。
public class InMemoryAccountDaoTests {
private static final String NEW_ACCOUNT_NO = "998";
private Account newAccount;
private InMemoryAccountDao accountDao;
@Before
public void init() {
newAccount = new Account(NEW_ACCOUNT_NO, 200);
accountDao = new InMemoryAccountDao();
}
@Test
public void createNewAccount() {
accountDao.createAccount(newAccount);
assertEquals(accountDao.findAccount(NEW_ACCOUNT_NO), newAccount); }
@Test(expected = AccountNotFoundException.class)
public void updateNotExistedAccount() { accountDao.updateAccount(newAccount);
}
如果我认为updateNotExistedAccount 会通过测试,这有错吗?
【问题讨论】:
标签: java unit-testing junit tdd hamcrest