unit testing in java
unit testing in java by Johannes Link and Peter Fröhlich introduces the reader to unit testing with JUnit. It is divided into two parts. The first part teaches how to use JUnit. The second part shows how to test GUI, persistence, servlets, EJB, RMI, ...
Chapter 1 is an introduction to unit testing and its importance. It begins with the many excuses programmers have to do no testing, and gives brilliant answers to these excuses. Following is a glossary for unit testing, an overview of Extreme Programming, and finally compares 'Classic Testing' to 'Test-First Development'.
Chapter 2 is about automating unit tests: what we want to automate, requirements for an automation framework and an introduction to JUnit. It explains what test cases and test suites are, the difference between a failure and an error, and what a fixture is.
Chapter 3 is about the test-first approach. It briefly explains TDD like in Kent Becks Test Driven Development By Example. Principles handled are evolutionary design, faking the correct implementation, refactoring tests, extracting a class and many more. It continues with the organisation and running of tests, like the importance of putting the tests in the same package as the code they test.
Chapter 4 is called Test Ideas and Heuristics. It discusses things like the length of a test, what a test should contain and what it shouldn't contain, threshold values and equivalence classes, error cases and catching exceptions, interaction tests, usage of Design by Contract with unit tests and refactoring of code and tests (sometimes it's necessary to throw away complicated existing tests and write new ones).
Chapter 5 is a short chapter about the inner workings of a typical test framework. These test frameworks, like JUnit, are usually small but efficient and expandable, so this can be covered briefly.
Chapter 6 is an extensive chapter that introduces the important technique known as Mock Objects. In testing it often occurs that the object you're testing needs another object of a class that you haven't written yet, or setting up such an object is too complex or takes too much resources. Mock objects allow you to give a replacement object instead, either by implementing an interface, or by using a proxy. An additional advantage is that the mock object can remember which of its methods were called by the object under test, which you can then check for correctness; or you could even specify in advance what methods should be called with which arguments, so the mock object can check this during the test. This is called endoscopic testing.
Actually it is advised to always use mock objects, so the success of a test doesn't depend on the correct working of another class.
The chapter finally discusses two libraries of mock objects, how to get the mock in your tests, the problem with mocking singletons, file dummies and mocking third party classes. It concludes with the pros and cons of mock objects.
Chapter 7 discusses testing inheritance and polymorphism. In these cases, one can write tests for an interface or base class, and run these tests with implementing or derived classes. This chapter discusses when this is possible, and how to do it.
Chapter 8 discusses how much we should test. It's important to test enough, but we should also know when to stop. A well known rule is 'Test everything that could possibly break'. Test coverage is an interesting technique to know how well a unit is tested. Both specification-based and code-based coverage are discussed.
Chapter 9 shows how to test persistent objects. Dummy objects are important here, and this chapter also shows how to design a database interface.
Chapter 10 discusses the testing of concurrent programs. This chapter discusses two types of testing here: testing asynchronous services, i.e. the test calls a method that starts another thread and checks whether the thread does what it's supposed to do, and testing for synchronization, i.e. the test launches different tests and checks whether objects in different tests behave threadsafe.
Chapter 11 discusses distributed applications. Especially interesting is the testing of EJBs inside the container, and testing on the application server with a test runner servlet. The chapter also warns that EJBs are often used in projects where they're not really necessary.
Chapter 12 discusses the testing of web applications. There are a few libraries that can be used with JUnit to test websites, like Cactus, HtmlUnit and HttpUnit. This is especially useful for testing the processing of forms. Cactus can even interact with the server. However, none of these can handle javascript or cookies. It also describes how to use dummies to test servlets.
Chapter 13 explains how to test the GUI. It starts with an example of how to do this directly, by simply creating the window (e.g. a dialog box), calling methods on it and testing whether the widgets have the expected content. It shows the drawbacks of this method, and gives a better way to do it, namely with JFCUnit, a framework that takes care of many issues (there are other similar frameworks).
Chapter 14 discusses how unit tests fit in the software process. It discusses what its cost and value is, how to use it with sequential models, incremental models, evolutionary models, continuous integration, Rational Unified Process and Extreme Programming.
Chapter 15 covers two small topics, unit testing for existing software and introducing unit tests to the development team. It also tells which topics couldn't be handled in the book.
Order unit testing in java by David Astels on Amazon.