Five Unit Tests Tips #3: Parametrized test methods

The following is a trick I don’t use very often, but when I do need it, it comes in very handy. It’s a trick that many developers aren’t aware of, even though it’s been possible to do with JUnit at least since version 3.

Sometimes you want to have a lot of tests that are almost the same, but that contain different arguments. For example, for a yahtzee calculator, you might want to have the following tests:

This is possible with JUnit, but a little trick. You have to construct a test suite manually and add special subclasses of TestCase to it:

The code creates an anonymous inner subclass of TestCase that instead of calling all methods annotated with @Test just executes our specific test. These tests are collected in a normal JUnit TestSuite.

The test is plain JUnit and will run in Maven or your favorite IDE, just as any other tests. The fact that the suite is named after the test class will let Eclipse know where to go when you double click on the test from the test runner.

This trick can be very useful for tests of logic that calculates a results or validates input.

Copyright © 2009 Johannes Brodwall. All Rights Reserved.

About Johannes Brodwall

Johannes is Principal Software Engineer in SopraSteria. In his spare time he likes to coach teams and developers on better coding, collaboration, planning and product understanding.
This entry was posted in English, Java, Software Development. Bookmark the permalink.
  • andreb

    Hi Johannes.

    What you’re doing here is supported by TestNG, using the @DataProvider annotation. Unfortunately, the project I’m currently working on is still using JUnit 3.8.1, so I’m definitely going to give your tip a try.

    André

  • http://brodwall.com/johannes/ Johannes Brodwall

    Good reference, Andre. Personally, I’ve never liked TestNG much, but it does solve this problem in a built in fashion. You should be able to do what I show with JUnit 3.8.1.

  • http://gleichmann.wordpress.com Mario Gleichmann

    Nice idea,

    when confronted with a quite similar problem, i remembered FIT as a valuable tool for testing different 'input' combinations, coming up with a little TestCase extension that behaves like FITs ColumnFixture but only in the context of JUnit.

    You may want to take a look for an example as well as the extended TestCase at

    http://gleichmann.wordpress.com/2007/11/21/jfit

    Greetings

    Mario

  • Me

    I prefer


    @Test
    public void checkSmallStraight() {
    checkRollScoredAsCategoryGives(1, 2, 3, 4, 5, "straight", 15);
    checkRollScoredAsCategoryGives(1, 2, 3, 4, 4, "straight", 0);
    }

    @Test
    public void checkFullHouse() {
    checkRollScoredAsCategoryGives(3, 3, 3, 4, 4, "full house", 17);
    checkRollScoredAsCategoryGives(1, 2, 3, 4, 4, "full house", 0);
    checkRollScoredAsCategoryGives(4, 4, 4, 4, 4, "full house", 0);
    }

  • http://www.brodwall.com/johannes/blog/ jhannes

    In this example, I agree with you that the technique isn't too useful in this particular instance. The places I've used this has been for things like String validation rules, complex calculations with many simple input data but many special rules etc. The example is meant to illustrate the technique, but it might be a poor example.

  • http://www.mysecuremovers.com/ Boston Movers

    The test seems pretty dependable..
    Having that posted means a lot for those who are using the program.
    Thanks for sharing it with us.

  • ikayes

    Nativity Memorials:I've been searching for that perfect gift for my dad ever since my mom past away I wanted to find a gift that would memorialize her life and found this site http://www.nativitymemorials.com you buy a plaque with an inscription and they put it up at the church of nativity in Bethlehem where Jesus was born. Its a great way of supporting Christianity and its a memorial that will last forever to my mother. I can't wait to give my dad the certificate this Christmas. I also found a discount coupon use promo code FALL05 and saved 5% on my order.

  • ikayes

    Nativity Memorials:I came across a site that offers a really unique memorial product for people that have died.. its http://www.nativitymemorials.com I bought a plaque for my parents, both of whom were very Catholic. A portion of the proceeds go to the Francisan Foundation who watch over the Church Of Nativity where Jesus was born. There is a photo on the site of the Pope receiving his plaque so I know my folks are in good company. I also found a discount coupon use promo code FALL05 and saved 5% on my order.

  • ikayes

    Wall Stickers, Wall Quotes & Wall Art for home & office. Offering Interior Wall Stickers & Quotes from small hallway stickers to large master bedroom stickers. http://www.adagiointeriors.com

  • ikayes

    HotelTattler.com is a leading online community for the hotel traveler. The website specializes in discounted hotel bookings for both business and leisure travelers. We offer competitive rates from over 30 different travel sites, and 900,000 hotels including all of the popular destinations in North America, Latin America, Europe, Asia, Africa and the Middle East..http://www.hoteltattler.com

  • marzia99

    Getting support on your POS & PMS system !

    POS
    Opening a restaurant can be one of the most daunting challenges an entrepreneur can take on. The sheer mountain of equipment to shop and acquire These days, a restaurant's point of sale system (POS) is almost as important as the fridge and oven. The question is no longer, 'should a restaurant have a point of sale system' but, 'what kind of point of sale system should my restaurant have'?

    As a restaurant computer consultant, I've found that there is a lot of confusion from new restaurant owners on what kind of system to invest in. To summarize the answers to some of the most common questions that I get at our website – http://www.cookcomputers.com/ . Here are 4 considerations you should look at before choosing a point of sale (POS) system.

    Management features :

    A restaurant with a POS system logically performs every transaction on that POS system. So more than making a restaurant run more smoothly, a good POS system can provide data that an owner can use to find ways to improve and be more profitable.

    A good POS application can even help a restaurant owner market his business. For instance some POS systems can generate mailing lists of customers, email lists of customers and have loyalty rewards and gift card programs. Even more practically, some point of sale applications have fast audit and anti theft measures built in helping curb employee dishonesty. Whether or not you go with a system that has anti-theft features, any POS system that you look at should track employee hours and function as their time clock weather with their own employee code or with a magnetic swipe card.
    A system should be configured by a supplier for the work flow that the restaurant has. A restaurant with a sushi bar for example might need some order tickets routed to the sushi chef and some orders routed to the kitchen. Furthermore, a fast service place with a drive through should have the ability to run a video kitchen display with bump bar whereas a coffee house on the corner may only need to be able to ring up items quickly and accurately but needs little else. A one size fits all system probably won't be an exact fit for anyone. So look for a provider that can customize hardware and software for your needs.

    For more please visit http://www.cookcomputers.com

  • rsanta2010

    Health And Safety Advice Health And Safety Advisor Competent Person Documents Risk Assessment Software £19 Per Month To Hire Competent Person

    http://www.citadel-uk.com/