Roel De Nijs wrote:A simple example: assume you'll have a utility method isEmpty(String s) which returns true if s is null, empty or whitespace; false otherwise. And you need to test this method:Will you write tests for empty type values like null, "" and " "? Or do you mock the isEmpty call returning true if you pass "ty0" and false with all other values?
Hi Roel--
Right--if you assume that
isEmpty is already tested (I wrote unit tests for it elsewhere), the job in testing the
create method is to verify the two possible paths based on the two possible outcomes of the
if conditional. As you suggest, you could mock the static (if you were using, for example, PowerMock). You could also set the value of
type to a value that answers
false for the conditional for one test, and to a value that answers
true for a second test. Those two tests, perhaps named:
createReturnsNullWhenTypeIsEmpty
createReturnsAnimalWhenTypeNotEmpty
are sufficient, since
isEmpty is already tested. Any more tests around how type might be "empty" would be redundant.
I think I lean toward using mocks if the tool is right (probably Mockito--i.e. a tool where I can clearly express the stubbing in a single line) and it's not a static (I don't like the extra complexity of bringing in PowerMock). Otherwise, I'm ok with passing a representative value. The test names are the same either way.
Jeff