Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_pt
CreateManager_pt
LeftMenu




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateDAO_pt


Difference between version 31 and version 30:

At line 182 added 84 lines.
======================================================
To start, create a {{PersonDaoTest.java}} class in the {{test/dao/**/dao}} directory. This class should extend [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html], a subclass of Spring's [AbstractDependencyInjectionSpringContextTests|http://www.springframework.org/docs/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html] which already exists in this package. This parent class is used to load [Spring's|http://www.springframework.org] ApplicationContext (since Spring binds the layers together), and for (optionally) loading a .properties file (ResourceBundle) that has the same name as your {{*Test.class}}. In this example, if you put a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}}, this file's properties will be available via an "rb" variable.
;:%%(color: blue)''I usually copy (open → save as) an existing test (i.e. UserDaoTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%%
[{Java2HtmlPlugin
package org.appfuse.dao;
import org.appfuse.model.Person;
import org.springframework.dao.DataAccessException;
public class PersonDaoTest extends BaseDaoTestCase {
private Person person = null;
private PersonDao dao = null;
public void setPersonDao(PersonDao dao) {
this.dao = dao;
}
}
}]
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);
}
}
}]
========================================================

Back to CreateDAO_pt, or to the Page History.