Posts Tagged ‘Tips’

JUnit 4 Test Logging Tips using SLF4J

Friday, February 26th, 2010

SLF4J LogoWhen writing JUnit tests developers often add log statements that can help provide information on test failures. During the initial attempt to find a failure a simple System.out.println() statement is usually the first resort of most developers.

Replacing these System.out.println() statements with log statements is the first improvement on this technique. Using SLF4J (Simple Logging Facade for Java) provides some neat improvements using parameterized messages. Combining SLF4J with JUnit 4 rule implementations can provide more efficient test class logging techniques.

Continue reading “JUnit 4 Test Logging Tips using SLF4J” »

JUnit Goodness

Friday, November 13th, 2009
Testing: The Dark Side?

Testing: The Dark Side?

It feels like an eternity since I last sat down to write some blog content!!

The last month or so I have been busy updating a ‘JUnit Goodness’ presentation for use at the Auckland JUG October 2009 meeting. The presentation focuses on 3 aspects of JUnit 4 that I feel may have slipped under the radar of developers introduced to JUnit at version 3. Parameterized tests, matcher assertions and the @Ignore annotation were the 3 aspects I chose to include in this presententation.

The parameterzied test section of the presentation attempts to outline a process that may lead a developer to utilize a parameterized test. The realization that duplicated code can be replaced by a looping test leads the devloper naturally in  the direction of a parameterized test. The presentation aims to offer answers regarding why and when to use parameterized tests and how to write them. Also included in the presentation is a named parameterized test extension and an XML data driven parametrized test extension that I had recently implemented.

Matcher assertions arrived in JUnit 4 via JMock. The presentation aims to demonstrate the advantages of using matcher assertions compared to the legacy JUnit assertions. Matcher assertion readability, clarity of failure messages, combination matcher usage and the benefits of list matchers are all presented. The creation of custom matchers is also present, albeit through a crude over simplified example.  To finish the presentation there is a look at the somewhat under utilized IMHO @Ignore annotation and how it can fit in to improved testing practices.

Auckland JUG Duke

Auckand JUG Duke

The majority of those present at the presentation were still using JUnit even though TestNG has built up quite a following and viewed by some as the next generation test tool. TestNG has useful features such as parallel testing and data driven testing using the @DataProvider annotation that JUnit does not. Why has there not been a shift to this next generation test tool? Is it down to the lazy developer syndrome? The we know this tool, why waste time learning a similar tool attitude? Some of the attendees were aware of TestNG and the benefits it offers when writing certain types of tests – yet still used JUnit for there project test needs.

Many of those present were unaware of the benefits that the 3 aspects presented provided. There was the odd attendee who had dabbled with one or two of the aspects, but none had really dug too deep. Hopefully I provided everyone with something to think about whenever they are writing JUnit tests in the future.

I had expected the parameterized tests section, particularly the named and XML data-driven extensions would be the ‘WOW’ factor of this particular presentation. This was not the case though. The actual ‘WOW’ factor turned out to be the clarity of the matcher assertion failure messages compared to their legacy equivalents. Flicking between the two versions on the big projector screen seemed to exacerbate the clarity.

I would be surprised to see any of the developers present not using matcher assertions from now on based on what they saw. The @Ignore annotation usage did lead into a group discussion regarding how useful it was, how it could be abused and the best ways to use it.

I plan to upload a slightly updated version of the presentation to SlideShare later this week.

Purchase JUnit In Action 2nd Edition MEAP Edition

Purchase JUnit In Action 2nd Edition MEAP Edition

Eclipse Tip #1 – Static Import Code Completion

Thursday, October 1st, 2009

If like me Eclipse is your IDE of choice and you find yourself frustrated by having to add an import statement in order to use common static imports then help is at hand. If you use JUnit 4′s assertThat method, the core Hamcrest matchers or even EasyMock verifiers etc, then this tip will make your life easier.

In order to simplify using these static methods add them to your Eclipse static import favourites. This little gem of functionality is found under Preferences -> Java -> Editor -> Content Assist -> Favorites.

Eclipse Static Import Favouries

Eclipse Static Import Favouries

In the above example I have added the org.junit.Assert.* package to my favourites. Now when I am working on some java code and want to use a static import from this package it will be available in the code completion box, as can be seen below.

Code Completion Static Imports in Eclipse

Code Completion Static Imports in Eclipse

Acknowledgment for this simple tip and a few more must go to Benjamin Winterberg who provided this in his 5 Tips for Customizing Eclipse blog entry. Vote his entry up on DZone by voting here.  Hopefully this tip will make your Eclipse static import use a pleasure and not a frustration from now on.