About Juri Strumpflohner

Juri Strumpflohner mainly operates in the web sector developing rich applications with HTML5 and JavaScript. Beside having a Java background and developing Android applications he currently works as a software architect in the e-government sector.

Unit Testing Tip: Create Descriptive Tests

Your unit tests should be as descriptive as possible. The feedback they give you should be so clear you don’t even need to fire up the debugger and step one by one through the code to inspect your local variables. Why? Because that takes time and we’re lazy, right? In order to do so you need to create descriptive tests. There are different approaches to realize that. Here are two of them.

Add Assert Messages

Assert messages can usually be specified as an additional parameter in your test assert and they appear as the failure message when that specific test case fails.

In jUnit for instance you’d specify your assert message like:

assertEquals("The firstname of the two people should match if the clone was successful", "Fritz", person1.getFirstname());

In MSTest on the other hand, the assert message has to be added as the last parameter.

Assert.AreEqual("Fritz", person1.Firstname, "The firstname of the two people should match if the clone was successful");

Caution, don’t “over-engineer”. I treat these comments similarly as with other code comments: just add them if they add meaningful information. Otherwise they’re waste and hence treat them as such.

Prefer Explicit Asserts

What I mean with explicit asserts is to use the correct asserts for the kind of operation you’re performing. For instance, if you need to perform an equality check as in the assert mentioned before, don’t use a boolean assert statement. (Here an example of a dummy QUnit test case)

test('Should correctly clone two people', 1, function(){
    //...

    ok(person1.firstname === 'Fritz', 'I expect both names to match if the clone operation succeeded');
});

…and the corresponding result in the output window:

This outcome isn't really descriptive...

This outcome isn’t really descriptive…

The outcome doesn’t tell us much, right? What you can say is that the person’s firstname didn’t match what you expected and as such that that clone operation probably didn’t succeed. But why?? What was its actual value then?

Use the equals instead:

test('Should correctly clone two people', 1, function(){
    //...

    equal(person1.firstname, 'Fritz', 'I expect both names to match if the clone operation succeeded');
});

…and again, the outcome:

Oh, look there, a descriptive outcome.

Oh, look there, a descriptive outcome.

Now this test case’s outcome is much more descriptive. It doesn’t only tell you that the operation failed, but also exactly shows you the expected as well as actual value. This might give you an immediate a hint where the problem could be.
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Unit Testing Tip: Create Descriptive Tests"

  1. vico says:

    I use assertJ to do descriptive tests. It’s a fork of festAssert and it provides fluent methods and nice format errors.

Leave a Reply


− two = 4



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books