Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Praise for the First Edition

Preface

Preface to the First Edition

Acknowledgments

About this Book

About the Authors

About the Cover Illustration

1. JUnit essentials

Chapter 1. JUnit jump-start

1.1. Proving it works

1.2. Starting from scratch

1.3. Understanding unit testing frameworks

1.4. JUnit design goals

1.5. Setting up JUnit

1.6. Testing with JUnit

1.7. Summary

Chapter 2. Exploring core JUnit

2.1. Exploring core JUnit

2.2. Running parameterized tests

2.3. JUnit test runners

2.3.1. Test runner overview

2.3.2. The JUnitCore façade

2.3.3. Custom test runners

2.4. Composing tests with a suite

2.4.1. Composing a suite of test classes

2.4.2. Composing a suite of suites

2.4.3. Suites, IDEs, Ant, and Maven

2.5. Summary

Chapter 3. Mastering JUnit

3.1. Introducing the controller component

3.1.1. Designing the interfaces

3.1.2. Implementing the base class

3.2. Let’s test it!

3.2.1. Testing the DefaultController

3.2.2. Adding a handler

3.2.3. Processing a request

3.2.4. Improving testProcessRequest

3.3. Testing exception handling

3.3.1. Simulating exceptional conditions

3.3.2. Testing for exceptions

3.4. Timeout testing

3.5. Introducing Hamcrest matchers

3.6. Setting up a project for testing

3.7. Summary

Chapter 4. Software testing principles

4.1. The need for unit tests

4.1.1. Allowing greater test coverage

4.1.2. Increasing team productivity

4.1.3. Detecting regressions and limiting debugging

4.1.4. Refactoring with confidence

4.1.5. Improving implementation

4.1.6. Documenting expected behavior

4.1.7. Enabling code coverage and other metrics

4.2. Test types

4.2.1. The four types of software tests

4.2.2. The three types of unit tests

4.3. Black box versus white box testing

User-centric approach

Testing difficulties

Test coverage

4.4. Summary

2. Different testing strategies

Chapter 5. Test coverage and development

5.1. Measuring test coverage

5.1.1. Introduction to test coverage

5.1.2. Introduction to Cobertura

5.1.3. Generating test coverage reports

5.1.4. Combining black box and white box testing

5.2. Writing testable code

5.2.1. Public APIs are contracts

5.2.2. Reduce dependencies

5.2.3. Create simple constructors

5.2.4. Follow the Principle of Least Knowledge

5.2.5. Avoid hidden dependencies and global state

5.2.6. Singletons pros and cons

5.2.7. Favor generic methods

5.2.8. Favor composition over inheritance

5.2.9. Favor polymorphism over conditionals

5.3. Test-driven development

5.3.1. Adapting the development cycle

5.3.2. The TDD two-step

5.4. Testing in the development cycle

5.5. Summary

Chapter 6. Coarse-grained testing with stubs

6.1. Introducing stubs

6.2. Stubbing an HTTP connection

6.2.1. Choosing a stubbing solution

6.2.2. Using Jetty as an embedded server

6.3. Stubbing the web server’s resources

6.3.1. Setting up the first stub test

6.3.2. Testing for failure conditions

6.3.3. Reviewing the first stub test

6.4. Stubbing the connection

6.4.1. Producing a custom URL protocol handler

6.4.2. Creating a JDK HttpURLConnection stub

6.4.3. Running the test

6.5. Summary

Chapter 7. Testing with mock objects

7.1. Introducing mock objects

7.2. Unit testing with mock objects

7.3. Refactoring with mock objects

7.3.1. Refactoring example

7.4. Mocking an HTTP connection

7.4.1. Defining the mock objects

7.4.2. Testing a sample method

7.4.3. First attempt: easy method refactoring technique

7.4.4. Second attempt: refactoring by using a class factory

7.5. Using mocks as Trojan horses

7.6. Introducing mock frameworks

7.6.1. Using EasyMock

7.6.2. Using JMock

7.7. Summary

Chapter 8. In-container testing

8.1. Limitations of standard unit testing

8.2. The mock objects solution

8.3. In-container testing

8.3.1. Implementation strategies

8.3.2. In-container testing frameworks

8.4. Comparing stubs, mock objects, and in-container testing

8.4.1. Stubs pros and cons

8.4.2. Mock objects pros and cons

8.4.3. In-container testing pros and cons

8.4.4. In-container versus out-of-container testing

8.5. Summary

3. JUnit and the build process

Chapter 9. Running JUnit tests from Ant

9.1. A day in the life

9.2. Running tests from Ant

9.3. Introducing and installing Ant

9.4. Ant targets, projects, properties, and tasks

9.4.1. The javac task

9.4.2. The JUnit task

9.5. Putting Ant to the task

9.6. Dependency management with Ivy

9.7. Creating HTML reports

9.8. Batching tests

9.9. Summary

Chapter 10. Running JUnit tests from Maven2

10.1. Maven’s features

10.1.1. Convention over configuration

10.1.2. Strong dependency management

10.1.3. Maven build lifecycles

10.1.4. Plug-in-based architecture

10.1.5. The Maven Project Object Model

10.2. Setting up a Maven project

10.3. Introduction to Maven plug-ins

10.3.1. Maven Compiler plug-in

10.3.2. Maven Surefire plug-in

10.3.3. HTML JUnit reports with Maven

10.4. The bad side of Maven

10.5. Summary

Chapter 11. Continuous integration tools

11.1. A taste of continuous integration

11.1.1. Continuous integration testing

11.2. CruiseControl to the rescue

11.2.1. Getting started with CruiseControl

11.2.2. Setting up a sample project

11.2.3. The CruiseControl config file explained

11.3. Another neat tool—Hudson

11.3.1. Introducing Hudson

11.3.2. Installation

11.3.3. Configuring Hudson

11.3.4. Configuring a project in Hudson

11.4. Benefits of continuous integration

11.5. Summary

4. JUnit extensions

Chapter 12. Presentation-layer testing

12.1. Choosing a testing framework

12.2. Introducing HtmlUnit

12.2.1. A live example

12.3. Writing HtmlUnit tests

12.3.1. HTML assertions

12.3.2. Testing for a specific web browser

12.3.3. Testing more than one web browser

12.3.4. Creating standalone tests

12.3.5. Navigating the object model

12.3.6. Accessing elements by specific element type

12.3.7. Accessing elements by name versus index

12.3.8. Accessing elements with references

12.3.9. Using XPath

12.3.10. Test failures and exceptions

12.3.11. Application and internet navigation

12.3.12. Testing forms with HtmlUnit

12.3.13. Testing frames

12.3.14. Testing JavaScript

12.3.15. Testing CSS

12.3.16. SSL errors

12.4. Integrating HtmlUnit with Cactus

12.4.1. Writing tests in Cactus

12.5. Introducing Selenium

12.6. Generating Selenium tests

12.6.1. A live example

12.7. Running Selenium tests

12.7.1. Managing the Selenium server

12.7.2. Running Selenium tests with JUnit 4

12.8. Writing Selenium tests

12.8.1. Testing for a specific web browser

12.8.2. Testing multiple browsers

12.8.3. Application and internet navigation

12.8.4. Accessing elements with references

12.8.5. Failing tests with exceptions

12.8.6. Testing forms with Selenium

12.8.7. Testing JavaScript alerts

12.8.8. Capturing a screen shot for a JUnit 3 test failure

12.8.9. Capturing a screen shot for a JUnit 4 test failure

12.9. HtmlUnit versus Selenium

12.10. Summary

Chapter 13. Ajax testing

13.1. Why are Ajax applications difficult to test?

13.1.1. Web-classic interaction

13.1.2. Ajax interaction

13.1.3. A brave new world

13.1.4. Testing challenges

13.2. Testing patterns for Ajax

13.2.1. Functional testing

13.2.2. Client-side script unit testing

13.2.3. Service testing

13.3. Functional testing

13.3.1. Functional testing with Selenium

13.3.2. Functional testing with HtmlUnit

13.4. JavaScript testing

13.4.1. JavaScript testing with RhinoUnit

13.4.2. JavaScript testing with JsUnit

13.4.3. Writing JsUnit tests

13.4.4. Writing JsUnit test suites

13.4.5. Running JsUnit tests manually

13.4.6. Running JsUnit tests with Ant

13.5. RhinoUnit versus JsUnit

13.6. Checking best practices with JSLint

13.7. Testing services with HttpClient

13.7.1. Calling an XML service

13.7.2. Validating an XML response

13.7.3. Validating a JSON response

13.8. Testing Google Web Toolkit applications

13.8.1. Choosing a testing framework for a GWT application

13.8.2. Creating a GWTTestCase manually

13.8.3. Creating a GWTTestCase with junitCreator

13.8.4. Running test cases

13.8.5. Setup and teardown

13.8.6. Creating a test suite

13.8.7. Running a test suite

13.9. Summary

Chapter 14. Server-side Java testing with Cactus

14.1. What is Cactus?

14.2. Testing with Cactus

14.2.1. Java components that you can test with Cactus

14.2.2. General principles

14.2.3. How Cactus works

14.3. Testing servlets and filters

14.3.1. Presenting the Administration application

14.3.2. Writing servlet tests with Cactus

14.4. Testing JSPs

14.4.1. Revisiting the Administration application

14.4.2. What is JSP unit testing?

14.4.3. Unit testing a JSP in isolation with Cactus

14.4.4. Executing a JSP with SQL results data

14.5. Testing EJBs

14.6. What is Cargo?

14.7. Executing Cactus tests with Ant

14.7.1. Cactus tasks to prepare the archive

14.8. Executing Cactus tests with Maven2x

14.8.1. Maven2 cactifywar MOJO

14.8.2. Maven2 cactifyear MOJO

14.9. Executing Cactus tests from the browser

14.10. Summary

Chapter 15. Testing JSF applications

15.1. Introducing JSF

15.2. Introducing the sample application

15.3. Typical problems when testing JSF applications

15.4. Strategies for testing JSF applications

15.4.1. Black box approach

15.4.2. Mock objects to the rescue

15.5. Testing the sample application with JSFUnit

15.5.1. Executing a JSFUnit test from a browser

15.5.2. Testing Ajax using JSFUnit

15.6. Using HtmlUnit with JSFUnit

15.7. Performance testing for your JSF application

15.8. Summary

Chapter 16. Testing OSGi components

16.1. Introducing OSGi

16.2. Our first OSGi service

16.2.1. The sample application

16.3. Testing OSGi services

16.3.1. Mock objects

16.4. Introducing JUnit4OSGi

16.5. Summary

Chapter 17. Testing database access

17.1. The database unit testing impedance mismatch

17.1.1. Unit tests must exercise code in isolation

17.1.2. Unit tests must be easy to write and run

17.1.3. Unit tests must be fast to run

17.2. Introducing DbUnit

17.2.1. The sample application

17.2.2. Setting up DbUnit and running the sample application

17.3. Using datasets to populate the database

17.3.1. DatabaseOperation dissected

17.4. Asserting database state with datasets

17.4.1. Filtering data sets

17.4.2. Ignoring columns

17.5. Transforming data using ReplacementDataSet

17.5.1. Using ReplacementDataSet to handle the different IDs issue

17.5.2. Handling NULL values

17.6. Creating datasets from existing database data

17.7. Advanced techniques

17.7.1. DbUnit and the Template Design Pattern

17.7.2. Improving reuse through custom annotations

17.7.3. Using Expression Language in datasets

17.8. Database access testing best practices

17.8.1. Use one database per developer

17.8.2. Make sure the target database is tested

17.8.3. Create complementary tests for loading and storing data

17.8.4. When writing load test cases, cover all the basic scenarios

17.8.5. Plan your dataset usage

17.8.6. Test cleanup

17.9. Summary

Chapter 18. Testing JPA-based applications

18.1. Testing multilayered applications

18.1.1. The sample application

18.1.2. Multiple layers, multiple testing strategies

18.2. Aspects of JPA testing

What Should Be Tested?

The Embedded Database Advantage

Commitment Level

18.3. Preparing the infrastructure

18.4. Testing JPA entities mapping

18.4.1. Integrating test cases with JPA ID generators

18.5. Testing JPA-based DAOs

18.6. Testing foreign key names

18.7. Summary

Chapter 19. JUnit on steroids

19.1. Introduction

19.1.1. Tools overview

19.1.2. Running the examples

19.2. Transparent mock usage

19.2.1. Unitils EasyMock support

19.2.2. FEST-Mocks

19.2.3. Mycila

19.3. DbUnit integration

19.4. Assertions made easy

19.4.1. JUnit-addons assertions package

19.4.2. Unitils’ ReflectionAssert

19.4.3. FEST Fluent Assertions Module

19.4.4. Mycila extend assertions

19.5. Using reflection to bypass encapsulation

19.5.1. In-house alternative

19.5.2. JUnit-addons

19.5.3. FEST-Reflect

19.6. Summary

Appendix A. Differences between JUnit 3 and JUnit 4

A.1. Global changes

A.1.1. JDK required

A.1.2. Backward/forward compatibility

A.2. Changes in the API

A.2.1. Package structure

A.2.2. Constructors

A.2.3. Extending TestCase

A.2.4. Test method names

A.3. Annotations and static imports added

A.3.1. @Before and @After annotations

A.3.2. @BeforeClass and @AfterClass annotations

A.3.3. Differences in ignoring a test

A.3.4. Static imports

A.3.5. Exception testing

A.3.6. Timeout testing

A.4. New JUnit runners

A.4.1. Test runners

A.4.2. Test suites

A.4.3. Parameterized tests

A.5. New assertions and assumptions

A.5.1. Hamcrest assertions

A.5.2. Assumptions

A.5.3. New assertions

A.5.4. Assertion errors

Appendix B. Extending the JUnit API with custom runners and matchers

B.1. Introducing the Interceptor pattern

B.2. Implementing a custom runner

B.3. Implementing a custom matcher

Appendix C. The source code for the book

C.1. Getting the source code

C.2. Source code overview

C.3. External libraries

C.4. JAR versions

C.5. Directory structure conventions

Appendix D. JUnit IDE integration

D.1. JUnit integration with Eclipse

D.1.1. Installing Eclipse

D.1.2. Setting up Eclipse projects from the source

D.1.3. Running JUnit tests from Eclipse

D.1.4. Running Ant scripts from Eclipse

D.2. Introducing the JUnitMAX Eclipse plug-in

D.2.1. Integrated in your development cycle

D.2.2. Execution order

D.2.3. Reverting to last stable version

D.3. JUnit integration with NetBeans

D.3.1. Installing NetBeans

D.3.2. Setting up NetBeans projects from the source

D.3.3. Running JUnit tests from NetBeans

D.3.4. Running Ant scripts from NetBeans

Appendix E. Installing software

E.1. Installing HtmlUnit

E.1.1. Standard configuration

E.1.2. Eclipse configuration

E.2. Configuring Cactus with HtmlUnit

E.3. Installing Selenium

E.4. Installing RhinoUnit

E.5. Installing JsUnit

Index

List of Figures

List of Tables

List of Listings

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

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