6.12 More Typical Mock Objects


6.12 More Typical Mock Objects

The use of mock objects is convenient in various places in our code. The mock streams described in the previous section are just as typical as the examples we will see later in the realm of databases (see Chapter 9) and Web applications (see Chapter 12). Here are a few additional ideas:

  • We can use MockListeners to check for correct sending of events to appropriate listener objects. Depending on the complexity of event instances, we could describe the expected sequence of events by using String objects corresponding to the toString() representations of these events.

  • If we want to verify the exact order of messages received, then it would be meaningful, here again, to convert the messages into strings inside the mock object. Note that this approach may require frequent adaptations as you rename methods.

  • Validating a message or event sequence across several clients (and thus mock objects) can be done by furnishing all mock objects concerned with a message registrator. This registrator will then play the actual mock's role and compare the expected with the actual message sequence.

This type of testing is very sensitive to minor modifications of our CUT's implementation, therefore we recommend it exclusively for cases where the exact sequence is part of the specification. This is often the case with frameworks, where abstract framework superclasses guarantee the invocation sequence of abstract methods. [4]

[4]This corresponds to the Template Method design pattern [Gamma95].




Unit Testing in Java. How Tests Drive the Code
Unit Testing in Java: How Tests Drive the Code (The Morgan Kaufmann Series in Software Engineering and Programming)
ISBN: 1558608680
EAN: 2147483647
Year: 2003
Pages: 144
Authors: Johannes Link

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net