6.4 Extending Our Mansion


6.4 Extending Our Mansion

Was this worth all the effort? Let's look at a few extensions of our small framework and think about the impact they may have on our tests.

Extension 1: We extend our Logging interface by a log method with an additional module parameter. This parameter specifies which module of the application is currently logging.

Impact: One additional test method and some minor refactoring in the LogServerTest, along the following lines:

 public class LogServerTest extends TestCase {    private LogServer logServer;    private DummyLogger logger;    protected void setUp() {       logger = new DummyLogger();       logServer = new LogServer(logger);    }    public void testLoggingWithModule() {       logServer.log(0, "first line", "test");       assertEquals("test(0): first line",                     logger.getLogString(0));    }    public void testSimpleLogging() {       logServer.log(0, "first line");       logServer.log(1, "second line");       logServer.log("third line");       assertEquals("(0): first line", logger.getLogString(0));       assertEquals("(1): second line", logger.getLogString(1));       assertEquals("(2): third line", logger.getLogString(2));    } } 

The tests for the actual loggers remain untouched.

Extension 2: We allow a log server to accommodate several loggers, to which it will distribute all log messages.

Impact: A few tests here to check for adding and removing of the loggers, and a few tests there to check whether or not each logger receives all messages. Even this bigger functional extension leaves the tests for the actual loggers untouched.

Separating LogServer from Logger and introducing a dummy implementation brought us the additional advantage that, if a test failure occurs, we will be able to precisely locate the problem in the source code. This is possible because we know for a fact that a test failure would be due solely to our log server and not to an error when accessing the file system.




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