At line 182 removed 64 lines. |
====================================================== |
|
|
The code you see above is what we need for a basic Spring integration test that initializes and destroys our PersonDao. Spring will use autowiring byType to call the ''setPersonDao()'' method and set the "personDao" bean as a dependency of this class. |
|
Now we need to actually test that the CRUD (create, retrieve, update, delete) methods work in our DAO. To do this we created methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our <junit> task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous. Add the following methods to your {{PersonDaoTest.java}} file: |
|
[{Java2HtmlPlugin |
|
public void testGetPerson() throws Exception { |
person = new Person(); |
person.setFirstName("Matt"); |
person.setLastName("Raible"); |
|
dao.savePerson(person); |
assertNotNull(person.getId()); |
|
person = dao.getPerson(person.getId()); |
assertEquals(person.getFirstName(), "Matt"); |
} |
|
public void testSavePerson() throws Exception { |
person = dao.getPerson(new Long(1)); |
person.setFirstName("Matt"); |
|
person.setLastName("Last Name Updated"); |
|
dao.savePerson(person); |
|
if (log.isDebugEnabled()) { |
log.debug("updated Person: " + person); |
} |
|
assertEquals(person.getLastName(), "Last Name Updated"); |
} |
|
public void testAddAndRemovePerson() throws Exception { |
person = new Person(); |
person.setFirstName("Bill"); |
person.setLastName("Joy"); |
|
dao.savePerson(person); |
|
assertEquals(person.getFirstName(), "Bill"); |
assertNotNull(person.getId()); |
|
if (log.isDebugEnabled()) { |
log.debug("removing person..."); |
} |
|
dao.removePerson(person.getId()); |
|
try { |
person = dao.getPerson(person.getId()); |
fail("Person found in database"); |
} catch (DataAccessException dae) { |
log.debug("Expected exception: " + dae.getMessage()); |
assertNotNull(dae); |
} |
} |
}] |
|
|
======================================================== |