You are previewing JUnit in Action.
O'Reilly logo
JUnit in Action

Book Description

A guide to unit testing Java applications (including J2EE applications) using the JUnit framework and its extensions, this book provides techniques for solving real-world problems such as unit testing legacy applications, writing real tests for real objects, automating tests, testing in isolation, and unit testing J2EE and database applications. Using a sample-driven approach, various unit testing strategies are covered, such as how to unit test EJBs, database applications, JSPs, and Taglibs. Also addressed are testing strategies using freely available open source frameworks and tools, and how to unit test in isolation with Mock Objects. Testing J2EE applications by running tests from inside the container for performing integration unit tests is discussed, as is how to automate unit testing in automated builds (such as Ant and Maven) for performing continuous integration.

Table of Contents

  1. Copyright
  2. Preface
  3. Acknowledgments
  4. About this Book
    1. Special features
      1. Best practices
      2. Design patterns in action
      3. Software directory
    2. Roadmap
      1. Part 1: JUnit distilled
      2. Part 2: Testing strategies
      3. Part 3: Testing components
    3. Code
    4. References
    5. Author online
  5. About the Authors
  6. About the Title
  7. About the Cover Illustration
  8. 1. JUnit distilled
    1. 1. JUnit jumpstart
      1. 1.1. Proving it works
      2. 1.2. Starting from scratch
      3. 1.3. Understanding unit testing frameworks
      4. 1.4. Setting up JUnit
      5. 1.5. Testing with JUnit
      6. 1.6. Summary
    2. 2. Exploring JUnit
      1. 2.1. Exploring core JUnit
      2. 2.2. Launching tests with test runners
        1. 2.2.1. Selecting a test runner
        2. 2.2.2. Defining your own test runner
      3. 2.3. Composing tests with TestSuite
        1. 2.3.1. Running the automatic suite
        2. 2.3.2. Rolling your own test suite
      4. 2.4. Collecting parameters with TestResult
      5. 2.5. Observing results with TestListener
      6. 2.6. Working with TestCase
        1. 2.6.1. Managing resources with a fixture
        2. 2.6.2. Creating unit test methods
          1. The Assert supertype
          2. The TestCase members
          3. Keeping tests independent
      7. 2.7. Stepping through TestCalculator
        1. 2.7.1. Creating a TestSuite
        2. 2.7.2. Creating a TestResult
        3. 2.7.3. Executing the test methods
        4. 2.7.4. Reviewing the full JUnit life cycle
      8. 2.8. Summary
    3. 3. Sampling JUnit
      1. 3.1. Introducing the controller component
        1. 3.1.1. Designing the interfaces
        2. 3.1.2. Implementing the base classes
      2. 3.2. Let’s test it!
        1. 3.2.1. Testing the DefaultController
        2. 3.2.2. Adding a handler
          1. Where do tests come from?
          2. Where do test classes live?
        3. 3.2.3. Processing a request
          1. Factorizing setup logic
        4. 3.2.4. Improving testProcessRequest
      3. 3.3. Testing exception-handling
        1. 3.3.1. Simulating exceptional conditions
        2. 3.3.2. Testing for exceptions
      4. 3.4. Setting up a project for testing
      5. 3.5. Summary
    4. 4. Examining software tests
      1. 4.1. The need for unit tests
        1. 4.1.1. Allowing greater test coverage
        2. 4.1.2. Enabling teamwork
        3. 4.1.3. Preventing regression and limiting debugging
        4. 4.1.4. Enabling refactoring
        5. 4.1.5. Improving implementation design
        6. 4.1.6. Serving as developer documentation
        7. 4.1.7. Having fun
      2. 4.2. Different kinds of tests
        1. 4.2.1. The four flavors of software tests
          1. Integration software testing
          2. Functional software testing
          3. Stress/load testing
          4. Acceptance software testing
        2. 4.2.2. The three flavors of unit tests
      3. 4.3. Determining how good tests are
        1. 4.3.1. Measuring test coverage
        2. 4.3.2. Generating test coverage reports
        3. 4.3.3. Testing interactions
      4. 4.4. Test-Driven Development
        1. 4.4.1. Tweaking the cycle
        2. 4.4.2. The TDD two-step
      5. 4.5. Testing in the development cycle
      6. 4.6. Summary
    5. 5. Automating JUnit
      1. 5.1. A day in the life
      2. 5.2. Running tests from Ant
        1. 5.2.1. Ant, indispensable Ant
        2. 5.2.2. Ant targets, projects, properties, and tasks
        3. 5.2.3. The javac task
        4. 5.2.4. The JUnit task
        5. 5.2.5. Putting Ant to the task
          1. The empty classpath
        6. 5.2.6. Pretty printing with JUnitReport
        7. 5.2.7. Automatically finding the tests to run
      3. 5.3. Running tests from Maven
        1. 5.3.1. Maven the goal-seeker
        2. 5.3.2. Configuring Maven for a project
          1. Executing Maven web-site generation
          2. Describing build-related information
        3. 5.3.3. Executing JUnit tests with Maven
        4. 5.3.4. Handling dependent jars with Maven
      4. 5.4. Running tests from Eclipse
        1. 5.4.1. Creating an Eclipse project
        2. 5.4.2. Running JUnit tests in Eclipse
      5. 5.5. Summary
  9. 2. Testing strategies
    1. 6. Coarse-grained testing with stubs
      1. 6.1. Introducing stubs
      2. 6.2. Practicing on an HTTP connection sample
        1. 6.2.1. Choosing a stubbing solution
        2. 6.2.2. Using Jetty as an embedded server
      3. 6.3. Stubbing the web server’s resources
        1. 6.3.1. Setting up the first stub test
          1. Creating a Jetty handler
          2. Starting and stopping Jetty once per test suite
          3. Writing the Test class
        2. 6.3.2. Testing for failure conditions
        3. 6.3.3. Reviewing the first stub test
      4. 6.4. Stubbing the connection
        1. 6.4.1. Producing a custom URL protocol handler
        2. 6.4.2. Creating a JDK HttpURLConnection stub
        3. 6.4.3. Running the test
      5. 6.5. Summary
    2. 7. Testing in isolation with mock objects
      1. 7.1. Introducing mock objects
      2. 7.2. Mock tasting: a simple example
      3. 7.3. Using mock objects as a refactoring technique
        1. 7.3.1. Easy refactoring
        2. 7.3.2. Allowing more flexible code
      4. 7.4. Practicing on an HTTP connection sample
        1. 7.4.1. Defining the mock object
        2. 7.4.2. Testing a sample method
        3. 7.4.3. Try #1: easy method refactoring technique
        4. 7.4.4. Try #2: refactoring by using a class factory
      5. 7.5. Using mocks as Trojan horses
      6. 7.6. Deciding when to use mock objects
      7. 7.7. Summary
    3. 8. In-container testing with Cactus
      1. 8.1. The problem with unit-testing components
      2. 8.2. Testing components using mock objects
        1. 8.2.1. Testing the servlet sample using EasyMock
        2. 8.2.2. Pros and cons of using mock objects to test components
      3. 8.3. What are integration unit tests?
      4. 8.4. Introducing Cactus
      5. 8.5. Testing components using Cactus
        1. 8.5.1. Running Cactus tests
        2. 8.5.2. Executing the tests using Cactus/Jetty integration
        3. 8.5.3. Drawbacks of in-container testing
          1. Specific tools required
          2. Longer execution time
          3. Complex configuration
      6. 8.6. How Cactus works
        1. 8.6.1. Executing client-side and server-side steps
        2. 8.6.2. Stepping through a test
          1. Step 1: execute beginXXX
          2. Step 2: open the redirector connection
          3. Step 3: create the server-side TestCase instance
          4. Step 4: call setUp, testXXX, and tearDown on the server side
          5. Step 5: execute endXXX
          6. Step 6: Gathering the test result
      7. 8.7. Summary
  10. 3. Testing components
    1. 9. Unit-testing servlets and filters
      1. 9.1. Presenting the Administration application
      2. 9.2. Writing servlet tests with Cactus
        1. 9.2.1. Designing the first test
        2. 9.2.2. Using Maven to run Cactus tests
        3. 9.2.3. Finishing the Cactus servlet tests
          1. Testing the callView method
          2. Testing the doGet method
      3. 9.3. Testing servlets with mock objects
        1. 9.3.1. Writing a test using DynaMocks and DynaBeans
        2. 9.3.2. Finishing the DynaMock tests
      4. 9.4. Writing filter tests with Cactus
        1. 9.4.1. Testing the filter with a SELECT query
        2. 9.4.2. Testing the filter for other query types
        3. 9.4.3. Running the Cactus filter tests with Maven
      5. 9.5. When to use Cactus, and when to use mock objects
      6. 9.6. Summary
    2. 10. Unit-testing JSPs and taglibs
      1. 10.1. Revisiting the Administration application
      2. 10.2. What is JSP unit testing?
      3. 10.3. Unit-testing a JSP in isolation with Cactus
        1. 10.3.1. Executing a JSP with SQL results data
        2. 10.3.2. Writing the Cactus test
        3. 10.3.3. Executing Cactus JSP tests with Maven
      4. 10.4. Unit-testing taglibs with Cactus
        1. 10.4.1. Defining a custom tag
        2. 10.4.2. Testing the custom tag
        3. 10.4.3. Unit-testing tags with a body
          1. Testing a tag life cycle method
          2. Testing all the life cycle methods at once
        4. 10.4.4. Unit-testing collaboration tags
      5. 10.5. Unit-testing taglibs with mock objects
        1. 10.5.1. Introducing MockMaker and installing its Eclipse plugin
        2. 10.5.2. Using MockMaker to generate mocks from classes
      6. 10.6. When to use mock objects and when to use Cactus
      7. 10.7. Summary
    3. 11. Unit-testing database applications
      1. 11.1. Introduction to unit-testing databases
      2. 11.2. Testing business logic in isolation from the database
        1. 11.2.1. Implementing a database access layer interface
        2. 11.2.2. Setting up a mock database interface layer
        3. 11.2.3. Mocking the database interface layer
      3. 11.3. Testing persistence code in isolation from the database
        1. 11.3.1. Testing the execute method
          1. Passing the mock Connection object
          2. Creating a first test
          3. Refining the test
          4. Discovering indirect calls in source code
          5. Mocking at a different level
          6. Fixing the test
        2. 11.3.2. Using expectations to verify state
          1. Adding expectations
          2. Testing for errors
      4. 11.4. Writing database integration unit tests
        1. 11.4.1. Filling the requirements for database integration tests
        2. 11.4.2. Presetting database data
          1. Connecting to the database
          2. Setting up the database with data
      5. 11.5. Running the Cactus test using Ant
        1. 11.5.1. Reviewing the project structure
        2. 11.5.2. Introducing the Cactus/Ant integration module
        3. 11.5.3. Creating the Ant build file step by step
          1. Creating the database schema
          2. Creating the web application war
          3. Compiling the Cactus tests
          4. Running the Cactus tests
        4. 11.5.4. Executing the Cactus tests
      6. 11.6. Tuning for build performance
        1. 11.6.1. Factoring out read-only data
        2. 11.6.2. Grouping tests in functional test suites
        3. 11.6.3. Using an in-memory database
      7. 11.7. Overall database unit-testing strategy
        1. 11.7.1. Choosing an approach
        2. 11.7.2. Applying continuous integration
      8. 11.8. Summary
    4. 12. Unit-testing EJBs
      1. 12.1. Defining a sample EJB application
      2. 12.2. Using a façade strategy
      3. 12.3. Unit-testing JNDI code using mock objects
      4. 12.4. Unit-testing session beans
        1. 12.4.1. Using the factory method strategy
          1. Refactoring the PetstoreEJB class
          2. Mocking factory methods
          3. Writing the unit tests
        2. 12.4.2. Using the factory class strategy
          1. Making the interfaces
          2. Adding static private variables
          3. Adding methods to set the domain objects
          4. Creating the test case
        3. 12.4.3. Using the mock JNDI implementation strategy
          1. Mocking the JNDI implementation
          2. Writing the test case
          3. Extending the code for other tests
          4. Resolving the EJB home caching issue
      5. 12.5. Using mock objects to test message-driven beans
      6. 12.6. Using mock objects to test entity beans
      7. 12.7. Choosing the right mock-objects strategy
      8. 12.8. Using integration unit tests
      9. 12.9. Using JUnit and remote calls
        1. 12.9.1. Requirements for using JUnit directly
          1. Defining a directory structure
        2. 12.9.2. Packaging the Petstore application in an ear file
        3. 12.9.3. Performing automatic deployment and execution of tests
          1. Creating a test target
          2. Verifying deployment
        4. 12.9.4. Writing a remote JUnit test for PetstoreEJB
        5. 12.9.5. Fixing JNDI names
        6. 12.9.6. Running the tests
      10. 12.10. Using Cactus
        1. 12.10.1. Writing an EJB unit test with Cactus
        2. 12.10.2. Project directory structure
        3. 12.10.3. Packaging the Cactus tests
        4. 12.10.4. Executing the Cactus tests
      11. 12.11. Summary
  11. A. The source code
    1. A.1. Getting the source code
    2. A.2. Source code overview
    3. A.3. External libraries
    4. A.4. Jar versions
    5. A.5. Directory structure conventions
  12. B. Eclipse quick start
    1. B.1. Installing Eclipse
    2. B.2. Setting up Eclipse projects from the sources
    3. B.3. Running JUnit tests from Eclipse
    4. B.4. Running Ant scripts from Eclipse
    5. B.5. Running Cactus tests from Eclipse
  13. References
    1. Bibliography
    2. Software directory
    3. Software licenses