minuteTest is a collection of utility code for unit testing. If you do write a lot of unit test code in your projects, certain problems tend to surface again and again. This toolbox provides a generic solution to some of these little itches, because repeating yourself is so DRY.
This is a subproject of minuteTools, but can be used independently.
Download the current version of minuteTools.
Everytime you generate a sizeable amount of text content (can be everything from an elaborate error message to a complex XML fragment), you will not want to verify the outcome "on foot". GoldenMasterFile enables you to easily verify your string against a static text file. E.g. using JUnit, you can write:
assertEquals(hugeString, goldenMasterFile(this, "SampleGoldenMaster.txt"));
See the unit test GoldenMasterFileTest for a more complete example.
If you are serious about your logging activities, you will want to unit test at least the more critical parts of them. But how can you verify, the content of your log in your unit test? Using a specialized appender implementation for log4j accessing the log content becomes easy:
getDefaultTestAppender().getLogAsString()
You can also define more than one appender and access them by their names:
getTestAppender("infoTestAppender").getLogAsString()
A sample log4j configuration for testing could look like this:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="unitTestAppender" class="com.minutefforts.test.util.logging.UnitTestAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p [%c{1}] %m %n" /> </layout> </appender> <appender name="infoTestAppender" class="com.minutefforts.test.util.logging.UnitTestAppender"> <param name="Threshold" value="INFO" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p [%c{1}] %m %n" /> </layout> </appender> <logger name="com.minutefforts.test.util.logging.UnitTestAppenderTest"> <appender-ref ref="infoTestAppender"/> </logger> <root> <priority value="debug" /> <appender-ref ref="unitTestAppender" /> </root> </log4j:configuration>
See the unit test UnitTestAppenderTest for a complete usage example.
One final remark: Exceptions are logged without stack trace to make verification easier.
Any feedback is welcome - questions, critique, praise and suggestions alike. :-)
Sent email to achim[at]minutefforts.com.