Java Reference
In-Depth Information
class JdbcAccountDAOSpec extends Specification {
@Autowired
JdbcAccountDAO dao
def "dao is injected properly"() {
expect: dao
}
def "find 3 accounts in sample db"() {
expect: dao.findAllAccounts().size() == 3
}
def "find account 0 by id"() {
when:
Account account = dao.findAccountById(0)
then:
account.id == 0
account.balance == 100.0
}
// tests for other methods as well
}
The @ContextConfiguration annotation tells the test runner how to find the Spring
bean configuration file. Adding @Transactional means that each test runs in a re-
quired transaction that (and this is the cool part) rolls back automatically at the end of each
test, implying that the database is reinitialized at the beginning of each test. The DAO is
autowired into the test class. The individual tests check that all the methods in the DAO
work as expected.
The next listing shows the tests for the service class, which includes using the old method
from Spock described in chapter 6 on testing.
Listing 7.7. Spock tests for the service class
import spock.lang.Specification
@ContextConfiguration("classpath:applicationContext.xml")
@Transactional
class AccountServiceSpec extends Specification {
@Autowired
AccountService service
def "balance of test account is 100"() {
expect: service.getAccountBalance(0) == 100.0
}
// ... other tests as necessary ...
Search WWH ::




Custom Search