views:

53

answers:

3

Hi
I have a question which is described below:

What problems would arise for testing a Java class which counts number of words in a file?

The function's signature is below:

public int wordCount(String filename)

Well, this is a junit testing question.
If you know the problem, what is the solution of that?

+1  A: 

So your question is what to test for? If yes, I'd say you should check if the definition of "word" is implemented correctly (e.g. is "stack-overflow" one word or two), are new lines handled correctly, are numbers counted as words (e.g. difference between "8" and "eight"), are (groups of special) characters (e.g. a hyphen) counted correctly.

Additionally, you should test whether the method returns the expected value (or exception) if the file does not exist.

This should be a good starting point.

sfussenegger
A: 

To sfussenegger's list, I'd add the file handling checks: does the method respond correctly to files not found (including null filename), or lacking read permission?

Also, to sfussenegger's correctness list, I'd add whether duplicates count and case sensitivity rules, as well.

Of course, all of this requires that you know how the method is supposed to behave for all of these specifics. It's easy to tell someone to "go count words", but there are subtleties to that assignment.

Which is one of the big benefits of writing a good set of unit tests.

CPerkins
A: 
furtelwart