To complete the JUnit cookbook, the recipe 'JUnit 2: How to report test results' describes how to modify the default mechanism of collecting and presenting test results. The design of the TestResult class relies on the Separation construction principle. Bear in mind the abstractly coupled JUnit classes TestCase and TestResult (see Figure 6.6). JUnit provides several subclasses of TestResult featuring textual or graphical output. However, other variants might be required, for example to save results to a database.
The definition of another TestResult variant is probably only done once in a JUnit adaptation. As this chapter concentrates mainly on demonstrating the feasibility of the cookbook approach in combination with the UML-F profile, we keep this recipe short and refer the interested reader to a more detailed description (Beck and Gamma, 1998a). Table 6.7 describes this recipe it reuses the generic recipe for the Separation construction principle given in Chapter 5.
Recipe 'JUnit 2: How to report test results' | |
---|---|
Intent | Providing addtional mechanisms for reporting tests. |
Classes | TestResult. |
Steps to Apply |
|
Discussion | This recipe should be applied rarely. In most cases the default features provided by JUnit are sufficient. |
Figure 6.13 shows the UML-F class diagram of a TestResult adaptation for the generation of HTML files. If an error or a failure occurs, a line is added to the HTML file. The notes attached to the hook methods describe the method behavior.
Figure 6.14 shows a browser that displays an HTML file that reports a failure when testing the ComplexNumber class with an assertion that (0, 1) added to (1, 1) would wrongly sum up to (1, 1).