我需要您的帮助来理解单元测试类中的单元(方法)行为,如下所示。
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
如果我假设考试会失败,那是错的吗?