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
  • 运行套件时,检查是否先运行哪个测试

谢谢。由于某种原因,我想“以前只运行一次。是的,请在和<代码>之前分别考虑< <代码> > 分别在每次测试之前和之后运行。在所有测试之前和之后,分别对课前和课后进行编码。(更新了答案以提高可视性)祝您晚上愉快。谢谢。由于某种原因,我想“以前只运行一次。是的,请在和<代码>之前分别考虑< <代码> > 分别在每次测试之前和之后运行。在所有测试之前和之后,分别对课前和课后进行编码。(更新了答案以提高可视性)祝您晚上愉快。