1

Excuse what may be a fairly trivial question:

I'm unit testing a 'setPercentage(int percent){...}' method. The requirements don't state anything about there being an 'acceptable range' or allowable values... but I know from the application in question that the percentage should only ever be between 0 and 100.

Should I be testing if the value being set makes sense (in the context of unit testing?)

i.e. should I write unit tests that fail if percentage is set > 100 or < 0?

2 Answers 2

2

If the documentation of the method does not explicitly say that such values are not allowed, you should definitely write unit tests for values outside the proper range. There will be users, who will provide incorrect values -- and rightly so, the documentation did not tell not to.

Sign up to request clarification or add additional context in comments.

Comments

1

You wouldn't write a test that fails if set > 100 or < 0. You'll test that setPercentage responds correctly if passed > 100 or < 0, whether this be setting an error property, or throwing IllegalArgumentException. The point of the unit test is to ensure expected results based on known input. If setting > 100 is invalid the following snippet could be an example of a unit test to verify the behavior:

try { 
    o.setPercentage(101);
    Assert.fail (
       "setPercentage(101) should of thrown an IllegalArgumentException");
} catch (IllegalArgumentException ex) {
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.