aspose file tools*
The moose likes Testing and the fly likes Looking for a best practice to deal with numerous similar JUnit Test Cases Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Looking for a best practice to deal with numerous similar JUnit Test Cases" Watch "Looking for a best practice to deal with numerous similar JUnit Test Cases" New topic
Author

Looking for a best practice to deal with numerous similar JUnit Test Cases

Suhrid Karthik
Ranch Hand

Joined: Aug 31, 2008
Posts: 58

I have different Test cases that need to be run. They are all of similar nature, except for the input and the expected output. See example below:



I do want different TestCases to be available for different sets of input, but I want to avoid duplicating the test case code. I am looking at something like generating a different test case automatically for each set of input. Does JUnit offer something like this ? Any pointers will be appreciated. Thanks.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

There's two common technqiues. One is to delegate to a private method that does all the work. The other is to use the Parameterized Test Case pattern. See the fifth example here.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1770
    
    7

I guess a @DataPoint / @Theory combo would work here as well.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looking for a best practice to deal with numerous similar JUnit Test Cases