Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess a @DataPoint / @Theory combo would work here as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic