Java Reference
In-Depth Information
Dynamic Typing to The Rescue
If I declare a reference using def , I can assign it to anything. When I invoke methods on it
I'm relying on the methods being there in whatever class I've used.
The next listing shows the complete unit test for the file DAO.
Listing 6.21. FileAccountDAO unit test, using an Expando to stub the File
class FileAccountDAOUnitTests {
FileAccountDAO dao
@Before
void setUp() {
Expando ex = new Expando()
ex.data = ''
ex.splitEachLine = { pattern, clos ->
data.splitEachLine(pattern, clos) }
ex.withWriter = { new StringWriter() }
ex. println = { data.append(it) }
dao = new FileAccountDAO(accountsFile:ex)
}
@Test
void testCreateAndFindNewAccount() {
int id = dao.createNewAccount(100.0)
Account local = new Account(id:id,balance:100.0)
Account fromDao = dao.findAccountById(id)
assertEquals local.id, fromDao.id
assertEquals local.balance, fromDao.balance, 0.01
}
@Test
void testFindAllAccounts() {
(1..10). each { num -> dao.createNewAccount(num*100) }
def accounts = dao.findAllAccounts()
assertEquals 10, accounts.size()
accounts*.balance. each { it in (100..1000) }
}
@Test
void testDeleteAccount() {
(1..10). each { num -> dao.createNewAccount(num*100) }
def accounts = dao.findAllAccounts()
assertEquals 10, accounts.size()
accounts. each { account -> dao.deleteAccount(account.id) }
assert 0 == dao.findAllAccounts().size()
Search WWH ::




Custom Search