Chapter 1. JUnit jump-start
Listing 1.1. The test calculator class
Listing 1.2. A simple test calculator program
Chapter 2. Exploring core JUnit
Listing 2.1. The CalculatorTest test case
Listing 2.2. Parameterized tests
Chapter 3. Mastering JUnit
Listing 3.1. Request, Response, RequestHandler, and Controller interfaces
Listing 3.2. The generic controller
Listing 3.3. Special response class signaling an error
Listing 3.4. TestDefaultController—a bootstrap iteration
Listing 3.5. Test classes as inner classes
Listing 3.6. TestDefaultController.testAddHandler
Listing 3.7. testProcessRequest method
Listing 3.8. TestDefaultController after some refactoring
Listing 3.9. Anti-example: don’t combine test methods
Listing 3.10. A refactored SampleResponse
Listing 3.11. Request handler for exception cases
Listing 3.12. testProcessRequestAnswersErrorResponse, first iteration
Listing 3.13. testProcessRequestExceptionHandler, fixed and refactored
Listing 3.14. Testing methods that throw an exception
Listing 3.16. Ignoring a test method in JUnit 4.x
Listing 3.17. Cumbersome JUnit assert method
Listing 3.18. Hamcrest library to simplify our assert declarations
Chapter 4. Software testing principles
Chapter 5. Test coverage and development
Listing 5.1. Reduce dependencies
Listing 5.2. Pass the Driver to the Vehicle
Listing 5.3. Law of Demeter violation
Listing 5.4. Global state in action
Listing 5.5. Avoiding global state
Chapter 6. Coarse-grained testing with stubs
Listing 6.1. Sample method opening an HTTP connection
Listing 6.2. Starting Jetty in embedded mode—JettySample class
Listing 6.3. First test to verify that WebClient works with a valid URL
Listing 6.4. Create a Jetty Handler that returns "It works" when called
Listing 6.5. Putting it all together
Listing 6.6. Providing custom stream handler classes for testing
Chapter 7. Testing with mock objects
Listing 7.2. AccountService.java
Listing 7.3. MockAccountManager.java
Listing 7.4. Testing transfer with MockAccountManager
Listing 7.5. Refactoring DefaultAccountManager for testing
Listing 7.6. A sample method that opens an HTTP connection
Listing 7.7. Extracting retrieval of the connection object from getContent
Listing 7.8. ConnectionFactory.java
Listing 7.9. Refactored WebClient using ConnectionFactory
Listing 7.10. HttpURLConnectionFactory.java
Listing 7.11. MockConnectionFactory.java
Listing 7.12. Refactored WebClient test using MockConnectionFactory
Listing 7.13. Mock InputStream with an expectation on close
Listing 7.14. WebClient closing the stream
Listing 7.15. Reworking the TestAccountService test using EasyMock
Listing 7.16. Reworking the WebClient test using EasyMock
Listing 7.17. Reworking the TestAccountService test using JMock
Chapter 8. In-container testing
Chapter 9. Running JUnit tests from Ant
Listing 9.1. Start of an Ant build file
Listing 9.2. The build file compile targets
Listing 9.3. The build file test target
Listing 9.4. Adding the Ivy changes to the build file
Listing 9.5. The ivy.xml file with the listed dependencies
Chapter 10. Running JUnit tests from Maven2
Listing 10.2. Child modules for parent pom.xml
Listing 10.3. Pom.xml that inherits the parent pom.xml
Listing 10.4. Pom.xml for the maven-sampling project
Listing 10.5. Additional metadata for the pom.xml
Listing 10.6. Description elements for the pom.xml
Chapter 11. Continuous integration tools
Listing 11.1. CruiseControl’s config.xml
Chapter 12. Presentation-layer testing
Listing 12.1. Our first HtmlUnit example
Listing 12.2. Testing for all HtmlUnit-supported browsers
Listing 12.3. Configuring a standalone test
Listing 12.4. Configuring a test with multiple page fixtures
Listing 12.5. Example form page
Listing 12.7. Page navigation through frames
Listing 12.8. Asserting expected alerts
Listing 12.9. Asserting no alerts under normal operation
Listing 12.10. Custom alert handler
Listing 12.11. Boilerplate HtmlUnit ServletTestCase subclass
Listing 12.12. A simple servlet
Listing 12.13. Our first Selenium example
Listing 12.14. Managing the Selenium server from a test
Listing 12.15. A test suite to manage a Selenium server
Listing 12.16. Running the test class for multiple browsers
Listing 12.17. Capturing a screen shot on JUnit 4 test failure
Chapter 13. Ajax testing
Listing 13.1. testform.html—a simple form (client)
Listing 13.2. UnmanagedFormTester.java bad test method
Listing 13.3. Testing the same form with HtmlUnit
Listing 13.4. Ant build.xml for JavaScript unit tests
Listing 13.5. jsFactorialTests.html
Listing 13.6. jsUnitTestSuite.html
Listing 13.7. Building a JsUnit test suite
Listing 13.8. JsUnit build.xml excerpts
Listing 13.9. Ant build results
Listing 13.10. An XML service test
Listing 13.11. A validating XML service test
Listing 13.12. A JSON service test
Listing 13.13. A JSON service test with JSLint
Listing 13.14. The asynchronous RPC call
Chapter 14. Server-side Java testing with Cactus
Listing 14.1. Sample of a servlet method to unit test
Listing 14.2. Using Cactus to unit test SampleServlet
Listing 14.3. Designing and testing the getCommand method
Listing 14.4. Minimum code to make the TestAdminServlet compile
Listing 14.5. Implementation of getCommand that makes the tests pass
Listing 14.6. Unit tests for callView
Listing 14.7. Implementation of callView that makes the test pass
Listing 14.8. Implementation of doGet that makes the tests pass
Listing 14.9. Results View JSP (results.jsp)
Listing 14.10. TestAdminServlet.java: unit tests for results.jsp
Listing 14.11. AdministratorEJB
Listing 14.12. Test case for AdministratorEJB
Listing 14.13. Build.xml skeleton to execute the Cactus tests
Listing 14.14. Build.xml to present cactifywar task
Listing 14.15. Build.xml to present cactifyear task
Listing 14.16. The test target to demonstrate the cactus task
Listing 14.17. Sample pom.xml for running the Cactus tests
Listing 14.18. Build section of the pom.xml to enable Cactus tests execution
Listing 14.19. Continuation of the plug-in declarations from listing 14.18
Listing 14.20. Continuation of the plug-in declarations from listing 14.19
Chapter 15. Testing JSF applications
Listing 15.1. Album.java POJO object
Listing 15.2. The AlbumManager performs different operations on Albums
Listing 15.3. ListAvailableAlbumsBean implementation
Listing 15.4. faces-config.xml to configure our beans
Listing 15.5. The JSP to display all the available albums
Listing 15.6. AlbumDetailsBean implementation
Listing 15.7. album_details.jsp presenting the details for a given product
Listing 15.8. Static analysis for your faces-config.xml
Listing 15.9. Testing the AlbumDetailsBean using JMock
Listing 15.10. First JSFUnit test
Listing 15.11. album_details.jsp
Listing 15.12. Testing the Ajax components from the album_details.jsp
Listing 15.13. Using HtmlUnit in a JSFUnit test
Chapter 16. Testing OSGi components
Listing 16.1. CalculatorService interface
Listing 16.2. Implementation of CalculatorService interface
Listing 16.3. CalculatorBundleActivator
Listing 16.4. Manifest file for the calculator bundle
Listing 16.5. ClientBundleActivator implementation
Listing 16.6. MANIFEST.MF for the client application
Listing 16.7. Refactored ClientBundleActivator to enable testability
Listing 16.8. Mock test for the CalculatorService
Listing 16.9. JUnit4OSGi test for our CalculatorService application
Chapter 17. Testing database access
Listing 17.1. DAO interface used in the examples
Listing 17.2. Domain model used in the examples
Listing 17.3. SQL script that creates the users table
Listing 17.4. XmlDataSet representation of users table
Listing 17.5. FlatXmlDataSet representation of users table (user.xml)
Listing 17.6. Initial test case for UserDaoJdbcImpl (UserDaoJdbcImplTest)
Listing 17.7. Test case for addUser() method
Listing 17.8. Revised version of testAddUser(), using a helper class
Listing 17.9. Second approach for testAddUser()
Listing 17.10. user-token.xml, a dataset that uses a token for IDs
Listing 17.11. Changes on UserDaoJdbcImplTest to handle dynamic IDs
Listing 17.12. user-ok.xml, where the first line has all columns
Listing 17.13. user-reverted.xml, where the first line is incomplete
Listing 17.14. Test case that demonstrates the missing column issue
Listing 17.15. ReplacementDataSet approach to the missing column issue
Listing 17.16. New version of user-reverted.xml, with DTD declaration
Listing 17.18. UserDaoJdbcImplTest using the Template Design Pattern
Listing 17.19. empty.xml, dataset used to clean up the database
Listing 17.20. Custom annotation @DataSets
Listing 17.21. UserDaoJdbcImplTest using custom annotations
Listing 17.22. New superclass, AbstractDbUnitTemplateTestCase
Listing 17.23. New AbstractDbUnitELTemplateTestCase that supports EL
Listing 17.24. UserDaoJdbcImplELTest using custom annotations
Listing 17.25. user-EL.xml, dataset that uses EL syntax for tokens
Chapter 18. Testing JPA-based applications
Listing 18.1. User and Telephone class definitions
Listing 18.2. Business layer interface (UserFacade) and transfer object (UserDto)
Listing 18.3. Initial implementation of UserDao using JPA
Listing 18.4. Façade implementation (UserFacadeImpl)
Listing 18.5. Unit test for UserFacadeImpl
Listing 18.6. Root class of testing infrastructure, AbstractJpaTestCase
Listing 18.7. JPA configuration (persistence.xml)
Listing 18.8. Hibernate-specific configuration (hibernate.properties)
Listing 18.9. Dataset file for User and Telephone tests, user-with-telephone.xml
Listing 18.10. Entity mapping unit tests for User and Telephone classes
Listing 18.11. New version of user-with-telephone.xml dataset, using EL functions
Listing 18.12. Changes to ELContextImpl to support the id function
Listing 18.13. Custom EL functions (ELFunctionMapperImpl)
Listing 18.14. Custom Hibernate event listener (ELPostInsertEventListener)
Listing 18.15. Changes on setEntityManager() to show Hibernate listeners
Listing 18.16. Changes on AbstractJpaDbUnitELTemplateTestCase
Listing 18.17. New version of EntitiesMappingTest using JPA ID’s integration
Listing 18.18. Initial version of UserDaoJpaImplTest
Listing 18.19. New test cases on UserDaoJpaImplTest to handle user with telephone
Listing 18.20. New test cases for negative scenarios
Listing 18.21. Final (and improved) version of UserDaoJpaImpl
Listing 18.22. Test case for Hibernate-generated database schema (SchemaTest.java)
Listing 18.23. Changes on AbstractJpaTestCase to support analyzeSchema()
Chapter 19. JUnit on steroids
Listing 19.1. UserFacadeImpl test refactored to use Unitils mock support
Listing 19.2. UserFacadeImpl test refactored to use FEST-Mocks
Listing 19.3. UserFacadeImpl test refactored to use Mycila EasyMock plug-in
Listing 19.4. UserFacadeImpl test refactored to use Mycila JMock plug-in
Listing 19.5. Refactored UserDaoJdbcImplTest using Unitils
Listing 19.6. Relevant changes to UserDaoJpaImplTest
Listing 19.7. unitils.properties settings for the DbUnit examples
Listing 19.8. Initial implementation of TestingHelper using the reflection API directly
Appendix A. Differences between JUnit 3 and JUnit 4
Listing A.1. JUnit4TestAdapter to wrap your JUnit 4.x tests
Listing A.2. Test method annotations in JUnit 4.x
Listing A.3. @Before and @After annotations in action
Listing A.4. @BeforeClass and @AfterClass annotations in action
Listing A.5. Static imports in action
Listing A.6. Exception handling the old 3.x way
Listing A.7. Exception handling with the expected parameter of the @Test annotation
Listing A.8. Timeout parameter in the @Test annotation
Listing A.9. Constructing test suites with JUnit 4.x
Listing A.10. Parameterized test runner in action
Appendix B. Extending the JUnit API with custom runners and matchers
Listing B.1. Interceptor interface defining the methods for any of our interceptors
Listing B.2. InterceptorStatement that wraps the original statement
Listing B.3. Custom JUnit runner—InterceptorRunner
Listing B.4. First interceptor—SampleLoggingInterceptor
Listing B.5. Executing a test case using our custom runner
Listing B.6. Second interceptor—SampleTimingInterceptor
Listing B.7. Test case for the timing interceptor
Listing B.8. Our first Hamcrest matcher implementation
Listing B.9. Test class to demonstrate the IsNotNullOrEmpty matcher
Appendix D. JUnit IDE integration
3.142.199.184