Channels ▼
RSS

Testing

Testing OO Systems, Part 1


Testing the Extended Logger

The next set of tests are for the ExtendedLogger from my article Custom Configuring Java Apps: Extending log4j. The test class (ExtendedLoggerTest) is in ExtendedLoggerTest.java. The ExtendedLogger works like a standard log4j logger, but it also supports a printf() file variant for all the log methods. I'm testing that functionality by actually logging something to a known log file, then reading the file to make sure that the logged string is what I expected.

Most of the work is in the setup() method at the top of the file, which creates a temporary file into which we'll log, then reconfigures log4j to log to that file rather than to it's normal target file.

The extended logger gets the location of the target log file from it's log4j.properties file, the location of which is defined in the CONFIG environment. As was the case with the Places tests we looked at a moment ago, I'm spying on the System class and telling it to return the location of the log4j.properties file created in setup() rather than the normal properties file. This way, all logging will go to the temporary file created at the top of setup().

The tests just log something, read back the logged text from the temporary file we created, and succeed if they've found the expected log message.

Note that here, as was the case earlier, I'm testing the ExtendedLogger as a black box. The object under test is just working in the normal way, but I'm using the neighbors to control its behavior so that I can test it.

Testing Configurations

Just to round out the set, I've also included the test file for the file-based configuration system described in my article Type-Safe File-Based Configuration (ConfigurationTest in ConfigurationsTest.java).

There are no new testing principles demonstrated here, so I'll just give you the file to read without much in the way of comment. ConfigurationsTest is just a straight JUnit test, with no mocking at all, so is pretty boring. I'm actually testing Configurations.Support object, but to do that, I have to implementing a fake configuration (TestConfiguration in TestConfiguration.java). I use the Configuations.Support.createConfigFileUsingDefaults() method to create physical configuration file (on the disk) that matches the defined in Java.

The one interesting method is createPropertiesFile() at the top of ConfigurationsTest.java. calls Configuations.Support.createConfigFileUsingDefaults() to create the properties file in memory by creating to a StringWriter rather than a FileWriter. It then goes through the file, one line at a time, replacing all strings that match a regular-expression argument with a replacement string. This way, the various tests can effectively replace one or more key=value pairs in the generated files with the custom strings that they need to test whatever they're testing.

For example, the createPropertiesFile("intBound.*", "intBound=0") call in the following test replaces the legal intBound=2 statement in the properties file with the string intBound=0 (which is out of bounds). The TestConfiguration.intBound.value() call will then force the out-of-bounds value to be read, and the test succeeds when the resulting ConfigurationError is thrown at file-load time.

    @Test( expected=ConfigurationError.class )
    public void detectValueTooLow() throws IOException
    {   //...

        createPropertiesFile("intBound.*", "intBound=0" );
        TestConfiguration.reset();                  // reset to force reload on next access
        TestConfiguration.intBound.value();         // load the bogus value.
    }

The other tests are similar. They either make sure that the expected values are the ones that are actually loaded and that the values are the right type and are in range, or that the load fails as expected when something's wrong.

Wrapping Up

So, that's a few basic tests that demonstrate the core principle of testing an object as a black box. This sort of testing wouldn't be possible if the object had an inappropriate interface (lots of getters and setters, for example) because the internal state of the object would be too easy to change, so we couldn't test every possible behavior. More to the point, these tests won't have to be modified if I need to modify the objects that I'm testing, so they should remain useful for a long time. Next month, we'll move on to more interesting uses of mocks.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video