Java 为什么这次考试通过了?
我需要您的帮助来理解下面所示的单元测试类中的单元(方法)行为Java 为什么这次考试通过了?,java,unit-testing,junit,tdd,hamcrest,Java,Unit Testing,Junit,Tdd,Hamcrest,我需要您的帮助来理解下面所示的单元测试类中的单元(方法)行为 public void updateAccount(Account account) { if (!accountExists(account.getAccountNo())) { throw new AccountNotFoundException(); } accounts.put(account.getAccountNo(), account); } 上面显示的方法告诉我,如果找不到帐户,将抛出异常 但是,下面显示的第
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
会使测试失败,这是错误的吗?看起来数据是从一个测试保存到另一个测试的。
尝试在每次测试后清理数据
@After
public void clean(){
// this method will be run after each single @Test method
// you can use this to clean all resoruces after a test. in your case for example
accountDao.deleteById(newAccount.getId());
}
为了完成测试并测试更新,我将执行以下操作:
@Test
public void updateExistingAccount() {
accountDao.createAccount(newAccount);
dbAccount = accountDao.findAccount(newAccount.getId);
dbAccount.setName("");
dbAccount.setSurname("");
// etc...
accountDao.updateAccount(dbAccount);
dbAccountUpdated = accountDao.findAccount(newAccount.getId);
assertEquals(accountDao.findAccount(dbAccountUpdated.getId()), dbAccount);
}
更新还要考虑<>代码> > >和<>代码>在每次测试之前和之后分别运行。 在所有测试之前和之后,分别使用
@BeforeClass
和@AfterClass
通过使用这4种方法,您可以始终使用所需的数据集启动测试,并在测试后按原样清理所有内容请参阅:
数据似乎是从一个测试保存到另一个测试的。 尝试在每次测试后清理数据
@After
public void clean(){
// this method will be run after each single @Test method
// you can use this to clean all resoruces after a test. in your case for example
accountDao.deleteById(newAccount.getId());
}
为了完成测试并测试更新,我将执行以下操作:
@Test
public void updateExistingAccount() {
accountDao.createAccount(newAccount);
dbAccount = accountDao.findAccount(newAccount.getId);
dbAccount.setName("");
dbAccount.setSurname("");
// etc...
accountDao.updateAccount(dbAccount);
dbAccountUpdated = accountDao.findAccount(newAccount.getId);
assertEquals(accountDao.findAccount(dbAccountUpdated.getId()), dbAccount);
}
更新还要考虑<>代码> > >和<>代码>在每次测试之前和之后分别运行。 在所有测试之前和之后,分别使用
@BeforeClass
和@AfterClass
通过使用这4种方法,您可以始终使用所需的数据集启动测试,并在测试后按原样清理所有内容请参阅:
要正确检查,需要查看您的
新帐户
代码以及您正在模拟的内容
- 检查您的
方法,因为该方法将在每次@Before
@Test
- 运行套件时,检查是否先运行哪个测试
新帐户
代码以及您正在模拟的内容
- 检查您的
方法,因为该方法将在每次@Before
@Test
- 运行套件时,检查是否先运行哪个测试