0

If I had the following code

public static <T> int count(List<T> list, T elem) {
  int count = 0; // the result
  for (int i=0; i < list.size(); i++) {
  if (list.get(i).equals(elem))
  count++;
  }
 return count;
}

Lets say I need to run a black box test,

Like so,

Input

[null], null Output should be 0

Firstly, is this a worthwhile test case? ( for black box testing)

If not,

Then I ask, are null values worth while tests for black box testing in any case?(in general)

Thanks

2
  • As a side note, your method could be replaced by int count = Collections.frequency(list, elem);...
    – assylias
    Jun 1, 2013 at 2:44
  • @assylias has an ensure result >=0
    – Jim
    Jun 1, 2013 at 3:36

1 Answer 1

2

How code reacts to null inputs is something that should be documented and tested, yes.

4
  • 1
    It doesn't hurt. And, it documents for the reader that null is permissible as an argument. Why not use a foreach loop though? Jun 1, 2013 at 2:36
  • Alright great, was kind of unsure as to whether they were worthwhile or not
    – Jim
    Jun 1, 2013 at 2:37
  • +1 for "use a foreach loop". If that is a non-random-access list, then access by index might be quite slow.
    – Thilo
    Jun 1, 2013 at 2:39
  • Oh that isn't my code it was just the example in a text book, I was more wondering the best way to structure black box tests... But I can see how a for each would be more appropriate
    – Jim
    Jun 1, 2013 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.