At line 26 changed 1 line. |
This class should extend [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], which already exists in the ''service'' package. The parent class (BaseManagerTestCase) serves the same functionality as the BaseDaoTestCase - to load a properties file that has the same name as your *Test.class, as well as to initialize Spring's ApplicationContext. |
This class should extend [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], which already exists in the ''service'' package. The parent class (BaseManagerTestCase) serves similar functionality as the BaseDaoTestCase. |
At line 30 changed 1 line. |
The code below is what we need for a basic JUnit test of our Managers. The code below simply creates and destroys the PersonManager. The "ctx" object is initialized in the [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html] class. |
The code below is what you need for a basic JUnit test of your Manager. Unlike the DaoTest, this test uses [jMock|http://jmock.org] to isolate the Manager from its dependencies and make it a ''true'' "unit" test. This can be very helpful because it allows you to test your business logic w/o worrying about other dependencies. The code below simply sets up the Manger and its dependencies (as Mocks) for testing. |
At line 36 added 4 lines. |
import java.util.List; |
import java.util.ArrayList; |
|
import org.appfuse.dao.PersonDao; |
At line 37 changed 1 line. |
import org.springframework.dao.DataAccessException; |
import org.appfuse.service.impl.PersonManagerImpl; |
At line 39 changed 1 line. |
public class PersonManagerTest extends BaseManagerTestCase { |
import org.jmock.Mock; |
import org.springframework.orm.ObjectRetrievalFailureException; |
At line 46 added 4 lines. |
public class PersonManagerTest extends BaseManagerTestCase { |
private final String personId = "1"; |
private PersonManager personManager = new PersonManagerImpl(); |
private Mock personDao = null; |
At line 42 removed 1 line. |
private PersonManager mgr = null; |
At line 46 changed 1 line. |
mgr = (PersonManager) ctx.getBean("personManager"); |
personDao = new Mock(PersonDao.class); |
personManager.setPersonDao((PersonDao) personDao.proxy()); |
At line 51 changed 1 line. |
mgr = null; |
personManager = null; |
At line 56 changed 1 line. |
Now that we have the JUnit framework down for this class, let's add the meat: the test methods to make sure everything works in our Manager. Here's a snippet from the [DAO Tutorial|CreateDAO] tutorial to help you understand what we're about to do. |
Now that you have the skeleton done for this class, you need to add the meat: the test methods to make sure everything works. Here's a snippet from the [DAO Tutorial|CreateDAO] tutorial to help you understand what we're about to do. |
At line 65 changed 3 lines. |
person = mgr.getPerson("1"); |
|
assertNotNull(person.getFirstName()); |
// set expected behavior on dao |
personDao.expects(once()).method("getPerson") |
.will(returnValue(new Person())); |
person = personManager.getPerson(personId); |
assertTrue(person != null); |
personDao.verify(); |
At line 71 changed 2 lines. |
person = mgr.getPerson("1"); |
person.setFirstName("test"); |
// set expected behavior on dao |
personDao.expects(once()).method("savePerson") |
.with(same(person)).isVoid(); |
At line 74 changed 3 lines. |
mgr.savePerson(person); |
assertEquals(person.getFirstName(), "test"); |
} |
personManager.savePerson(person); |
personDao.verify(); |
} |
At line 80 removed 1 line. |
person = (Person) populate(person); |
At line 82 changed 3 lines. |
mgr.savePerson(person); |
assertEquals(person.getFirstName(), "Bill"); |
assertNotNull(person.getId()); |
// set required fields |
person.setFirstName("firstName"); |
person.setLastName("lastName"); |
At line 86 changed 1 line. |
log.debug("removing person, personId: " + person.getId()); |
// set expected behavior on dao |
personDao.expects(once()).method("savePerson") |
.with(same(person)).isVoid(); |
personManager.savePerson(person); |
personDao.verify(); |
At line 88 changed 1 line. |
mgr.removePerson(person.getId().toString()); |
// reset expectations |
personDao.reset(); |
At line 107 added 11 lines. |
personDao.expects(once()).method("removePerson").with(eq(new Long(personId))); |
personManager.removePerson(personId); |
personDao.verify(); |
|
// reset expectations |
personDao.reset(); |
// remove |
Exception ex = new ObjectRetrievalFailureException(Person.class, person.getId()); |
personDao.expects(once()).method("removePerson").isVoid(); |
personDao.expects(once()).method("getPerson").will(throwException(ex)); |
personManager.removePerson(personId); |
At line 91 changed 5 lines. |
person = mgr.getPerson(person.getId().toString()); |
fail("Person found in database"); |
} catch (DataAccessException dae) { |
log.debug("Expected exception: " + dae.getMessage()); |
assertNotNull(dae); |
personManager.getPerson(personId); |
fail("Person with identifier '" + personId + "' found in database"); |
} catch (ObjectRetrievalFailureException e) { |
assertNotNull(e.getMessage()); |
At line 124 added 1 line. |
personDao.verify(); |
At line 106 changed 1 line. |
First off, create a PersonManager.java interface in the src/service/**/service directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.'' |
First off, create a PersonManager.java interface in the src/service/**/service directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.'' The ''setPersonDao()'' method is not used in most cases - its just exists so the PersonManagerTest can set the DAO on the interface. |
At line 143 added 1 line. |
import org.appfuse.dao.PersonDao; |
At line 146 added 1 line. |
public void setPersonDao(PersonDao dao); |
At line 154 changed 2 lines. |
One thing to note is the {{setPersonDao}} method. This is used by Spring to bind the PersonDao to this Manager. This is configured in the applicationContext-service.xml file. We'll get to configuring that in Step 3[3]. |
You should be able to compile everything now using "ant compile-service"... |
One thing to note is the {{setPersonDao}} method. This is used by Spring to bind the PersonDao to this Manager. This is configured in the applicationContext-service.xml file. We'll get to configuring that in Step 3[3]. You should be able to compile everything now using "ant compile-service". |
At line 157 changed 1 line. |
Finally, we need to create the PersonManagerTest.properties file in test/service/**/service so that {{person = (Person) populate(person);}} will work in our test. |
Now you need to edit Spring's config file for our services layer so it will know about this new Manager. |
At line 159 removed 7 lines. |
;:''If you created a PersonDaoTest.properties file in the last tutorial, duplicating (and renaming) it is the easy route. Alternatively, you could set the properties manually.'' |
|
{{{firstName=Bill |
lastName=Joy}}} |
|
Now we need to edit Spring's config file for our services layer so it will know about this new Manager. |
|