Uncle Bob's Agile Development Corner
OBSERVER—Evolving into a pattern, Part 2
by Robert C. Martin
Listing 7.

import junit.framework.*;

public class ObserverTest extends TestCase
    private MockTimeSource source;
    private MockTimeSink sink;

    public ObserverTest(String name)

    public void setUp()
        source = new MockTimeSource();
        sink = new  MockTimeSink(source);

    private void assertSinkEquals(
        MockTimeSink sink, int hours, int minutes, int seconds)
        assertEquals(hours, sink.getHours());
        assertEquals(minutes, sink.getMinutes());
        assertEquals(seconds, sink.getSeconds());

    public void testTimeChange()
        assertSinkEquals(sink, 3,4,5);

        assertSinkEquals(sink, 7,8,9);

    public void testMultipleSinks()
        MockTimeSink sink2 = new MockTimeSink(source);

        assertSinkEquals(sink, 12,13,14);
        assertSinkEquals(sink2, 12,13,14);

About the Author

Robert C. Martin is president of Object Mentor Inc., a firm that offers high-level, OO software design consulting, training, and development services. He is the author of Designing Object Oriented C++ Applications Using the Booch Method. He can be contacted at [email protected].