List of Listings

Chapter 1. Fundamentals

Listing 1.1. Your first test

Chapter 2. Elementary tests

Listing 2.1. Testing Money.equals() with EqualsTester

Listing 2.2. Testing Money.equals() with EqualsHashCodeTestCase

Listing 2.3. Song.getDurationInMinutes()

Listing 2.4. An alternate implementation of Song.getDurationInMinutes()

Listing 2.5. An easier-to-test BankTransferAction

Listing 2.6. BankTransferActionTest

Listing 2.7. ListIteratorTest

Listing 2.8. A more complete IteratorTest

Listing 2.9. Using ValueObjectEqualsTest with five key properties

Chapter 3. Organizing and building JUnit tests

Listing 3.1. Simple Money test

Listing 3.2. MoneyTest before moving code into setUp()

Listing 3.3. MoneyTest after moving code into setUp()

Listing 3.4. A minimal Ant buildfile for JUnit tests

Listing 3.5. A more useful Ant buildfile for JUnit tests

Chapter 4. Managing test suites

Listing 4.1. Collecting all the tests in one part of the file system

Listing 4.2. Eliminating abstract classes from a test suite using TestFilter

Listing 4.3. Eliminating abstract classes from a test suite using DirectorySuiteBuilder

Listing 4.4. Collecting tests that implement the CustomerTest interface

Listing 4.5. AllocateMoneyTest

Listing 4.6. Test data for splitting money n ways

Chapter 5. Working with test data

Listing 5.1. SystemPropertyDemo

Listing 5.2. PrintSystemVar

Listing 5.3. InterestCalculatorTest

Listing 5.4. LoadPropsFromClasspathTest

Listing 5.5. InterestCalculatorTest2

Listing 5.6. InterestCalculatorConfigurableTest

Listing 5.7. InterestCalculatorTestWithTestSetup

Chapter 7. Reporting JUnit results

Listing 7.1. BaseTestCase configured with Avalon LogKit

Listing 7.2. Log4UnitExample.java

Listing 7.3. junit-run Ant target using brief results formatter

Listing 7.4. build.xml using the <junitreport> task

Listing 7.5. Ant script snippet using <report> element with custom stylesheet

Listing 7.6. Ant script snippet using <junitreport> task to merge XML result files into one

Listing 7.7. Ant script snippet using <style> task just to transform XML results

Listing 7.8. HtmlJUnitResultFormatter

Listing 7.9. junit.framework.TestListener

Listing 7.10. CookbookTestListener

Listing 7.11. CookbookTestRunner, an extension of TestRunner

Listing 7.12. CountingAssert, an extension of Assert

Listing 7.13. CookbookTestRunner.main() displaying the assertion count

Chapter 9. Testing and XML

Listing 9.1. A simple XML document

Listing 9.2. Testing an XML object marshaller

Listing 9.3. Comparing documents with assertXMLEqual()

Listing 9.4. Testing for identical XML documents

Listing 9.5. A sample web deployment descriptor

Listing 9.6. An implementation of DifferenceListener

Listing 9.7. Using the DifferenceListener

Listing 9.8. WelcomePageTest

Listing 9.9. How not to use HTMLConfiguration for NekoHTML

Listing 9.10. A shopcart as an XML document

Listing 9.11. DisplayShopcartXslTest

Chapter 10. Testing and JDBC

Listing 10.1. A test for unmarshalling ResultSet data

Listing 10.2. DiscountStoreJdbcMapper

Listing 10.3. A Learning Test for the database

Listing 10.4. Our Learning Test after having learned something

Listing 10.5. Verifying a unique index

Listing 10.6. CoffeeShopDatabaseSchemaTest, the final version

Listing 10.7. Two database schema tests

Listing 10.8. Handling JDBC resources in the test fixture

Listing 10.9. CoffeeShopDatabaseFixture

Listing 10.10. CoffeeShopDatabaseSchemaTest using the new fixture

Listing 10.11. Verify that the CatalogStore closes its PreparedStatement

Listing 10.12. Using the “rollback” trick

Listing 10.13. Testing privileges on stored procedures

Listing 10.14. Adding code to expect an SQLException

Listing 10.15. StoredProcedurePrivilegesTest

Listing 10.16. A database test using the Mock Objects JDBC API

Listing 10.17. A test case using a DbUnit dataset

Listing 10.18. Using a schema-qualified dataset

Listing 10.19. AddProductTest using a stored procedure

Chapter 11. Testing Enterprise JavaBeans

Listing 11.1. ShopcartOperationsBean

Listing 11.2. ShopcartLogic

Listing 11.3. The final version of ShopcartOperationsBean

Listing 11.4. A session bean using an entity bean

Listing 11.5. Deploying some objects with MockEJB

Listing 11.6. A Cactus test for adding a product to a shopcart

Listing 11.7. CoffeeCatalogItemEntityBeanMetaDataTest

Listing 11.8. The refactored test

Listing 11.9. An early version of EntityBeanMetaDataTest

Listing 11.10. OrderBmpBeanTest

Listing 11.11. ProcessOrderSubmissionBeanTest

Listing 11.12. Testing order submission with MockEJB

Listing 11.13. ProcessOrderSubmissionLegacyBean

Listing 11.14. ProcessOrderSubmissionMessageListenerTest

Listing 11.15. Verifying the “To” address in an e-mail

Listing 11.16. MapMessageSender

Listing 11.17. BuildMapMessageTest

Listing 11.18. The “happy path” test for submitting an order

Listing 11.19. JndiDirectoryContentsTest

Chapter 12. Testing web components

Listing 12.1. ShopcartModel

Listing 12.2. Verifying the contents of the HTTP session

Listing 12.3. RenderShopcartJspTest

Listing 12.4. ForwardingServlet

Listing 12.5. GoldMasterFile

Listing 12.6. RenderShopcartDisplayTemplateTest

Listing 12.7. Using an IterationTag

Listing 12.8. Pseudocode for a generic IterationTag handler

Listing 12.9. EachShopcartItemHandlerTest

Listing 12.10. CoffeeTagLibraryDeploymentTest

Listing 12.11. Verifying the behavior of init()

Listing 12.12. Test processing initialization parameters

Listing 12.13. InitializeUnitPricesTest

Listing 12.14. Test making the “add to shopcart” command

Listing 12.15. Verifying the servlet invokes the new command

Listing 12.16. testServletInvokesExecuteCommand()

Listing 12.17. Loading web pages from the file system with FileSystemWebResponse

Listing 12.18. Shopcart Display page template (JSP)

Listing 12.19. testControllerProvidesShopcartBean()

Listing 12.20. EnsureShopperHasShopcartFilterLogicTest

Listing 12.21. EnsureShopperHasShopcartFilterIntegrationTest

Listing 12.22. Some simple filter tests

Listing 12.23. EnsureShopperHasShopcartFilter (final version)

Chapter 13. Testing J2EE applications

Listing 13.1. NavigationTest, a sample page flow test

Listing 13.2. CoffeeShopController using an Action Mapper

Listing 13.3. MapRequestToActionTest

Listing 13.4. CoffeeShopController using the location mapper

Listing 13.5. XMLUnit tests for struts-config.xml

Listing 13.6. A sample fixture for struts-config.xml tests

Listing 13.7. LinksTest

Listing 13.8. Test the authorization rules for administrative web resources

Listing 13.9. A web deployment descriptor that passes the tests in AdminWelcomePageTest

Listing 13.10. PricingOperationsSecurityTest, a server-side Cactus test

Listing 13.11. Verifying the SecurityException message

Listing 13.12. EntityBeanTransactionAttributeTest

Chapter 14. Testing design patterns

Listing 14.1. Tests for a text-based TestListener implementation

Listing 14.2. Further text-based TestListener tests

Listing 14.3. Testing MessageSender with EasyMock

Listing 14.4. Verifying the events generated when running a JUnit test

Listing 14.5. Adding a dummy passing test

Listing 14.6. Adding a dummy failing test

Listing 14.7. Adding a test that throws an exception

Listing 14.8. SpyShapeMaker

Listing 14.9. TestCaseTest

Chapter 15. GSBase

Listing 15.1. Using EventCatcher on a Swing component

Listing 15.2. Testing serialization

Listing 15.3. Verifying events with “appears equal”

Chapter 16. JUnit-addons

Listing 16.1. ComparabilityTestCase (abstract methods)

Listing 16.2. A test from ComparabilityTestCase

Listing 16.3. Comparability test for class String

Listing 16.4. Collecting the tests in a JAR file

Listing 16.5. Using ResourceManager to manage a DataSource

Listing 16.6. ResourceManagerTestSetup

Listing 16.7. The standard JUnit implementation of TestSetup.run()

Listing 16.8. The JUnit-addons implementation of TestSetup.run()

Listing 16.9. DumpTestNameListener

Chapter 17. Odds and ends

Listing 17.1. A fixture that tries to delete its files

Listing 17.2. Cleaning up the file in setUp() and teardown()

Listing 17.3. CoffeeCatalogFileReader

Listing 17.4. CoffeeCatalogFileTest

Listing 17.5. CoffeeCatalogFileTest with a relative filename

Listing 17.6. CoffeeCatalogFileTest using a test data directory

Listing 17.7. CoffeeCatalogFileTest with an inline file

Listing 17.8. CoffeeCatalogReader

Listing 17.9. A test case class in need of validation

Listing 17.10. An implementation of Observable

Listing 17.11. ObservableTest

Listing 17.12. TestProxy

Appendix A. Complete solutions

Listing A.1. AllocateMoneyXmlBasedTest

Listing A.2. AllocateMoneyTestBuilder

Listing A.3. AllocateMoneyTestOverridesRunTest

Listing A.4. StrutsDeploymentTest

Listing A.5. DisplayShopcartXslTest

Listing A.6. XSL stylesheet for displaying a shopcart

Listing A.7. TransformXmlService

Listing A.8. Testing SomeClass with the universal Spy

Listing A.9. The universal Spy aspect

Listing A.10. UniversalSpyFixture

Listing A.11. UniveralSpy

Listing A.12. A BMP entity bean for orders

Listing A.13. A thinner BMP entity bean for orders

Listing A.14. JDBC implementation of OrderStore

Listing A.15. OrderStoreCommand

Listing A.16. EJB-based OrderStoreCommand executer

Listing A.17. Business logic POJO for orders

Listing A.18. Testing the POJO for orders

Listing A.19. OrderStoreCommandExecuterEjbImplTest

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset