Elliott
Elliott

Reputation: 5609

Specifying data for JUNIT test cases

I am learning about how to use JUnit but there is something that I still don't understand. When you set up test cases, how do you determine the range of values to use to test a method out. I realize that it depends on the method, but given the huge number of possibilities with many methods (say one where the signature of the method is an int) how do you define a range of values that will adequately test the method. Are there guidelines, rules of thumb, etc. that might be useuful?

Thank you

Upvotes: 1

Views: 363

Answers (2)

dimitrisli
dimitrisli

Reputation: 21381

Rather than all range of possibilities I would suggest you set some sample test cases from the range along with some (or many) edge cases.

Upvotes: 0

Jihed Amine
Jihed Amine

Reputation: 2208

Depending on the logic of the method you want to test, a rule of thumb is to have one test for the 'regular' case and one test for every corner case. For example, getSquareRoot(int n) you would create unit tests for a positive number that has an integer square root, one that has a square root with decimals, getSquareRoot of 0 and getSquareRoot of a negative number. Every test has to assert that the behavior equals what's expected. So there's no really added value to test with two values belonging to the same "case" for that method's logic (e.g. getSquareRoot(4) and getSquareRoot(16) as they both return an integer). The example I gave maybe not the best one, but it's just to illustrate the rule of thumb.

Upvotes: 1

Related Questions