views:

133

answers:

6

I was testing a String multiplier class with a multiply() method that takes 2 numbers as inputs (as String) and returns the result number (as String)

public String multiply(String num1, String num2);      

I have done the implementation and created a test class with the following test cases involving the input String parameter as

  1. valid numbers
  2. characters
  3. special symbol
  4. empty string
  5. Null value
  6. 0
  7. Negative number
  8. float
  9. Boundary values
  10. Numbers that are valid but their product is out of range
  11. numbers will + sign (+23)

Now my questions are these:

  1. I'd like to know if "each and every" assertEquals() should be in it's own test method? Or, can I group similar test cases like testInvalidArguments() to contains all asserts involving invalid characters since ALL of them throw the same NumberFormatException ?

  2. If testing an input value like character ("a"), do I need to include test cases for ALL scenarios? "a" as the first argument "a" as the second argument "a" and "b" as the 2 arguments

  3. As per my understanding, the benefit of these unit tests is to find out the cases where the input from a user might fail and result in an exception. And, then we can give the user with a meaningful message (asking them to provide valid input) instead of an exception. Is that the correct? And, is it the only benefit?

  4. Are the 11 test cases mentioned above sufficient? Did I miss something? Did I overdo? When is enough?

  5. Following from the above point, have I successfully tested the multiply() method?

+4  A: 

1) I do think it's a good idea to limit the number of assertions you make in each test. JUnit only reports the first failure in a test, so if you have multiple assertions some problems may be masked. It's more useful to be able to see everything that passed and everything that failed. If you have 10 assertEquals in one test and the first one fails, then you just don't know what would have happened with the other 9. Those would be good data points to have when debugging.

2) Yes, you should include tests for all of your inputs.

3) It's not just end-user input that needs to be tested. You'll want to write tests for any public methods that could possibly fail. There are some good guidelines for this, particularly concerning getters and setters, at the JUnit FAQ.

4) I think you've got it pretty well covered. (At least I can't think of anything else, but see #5).

5) Give it to some users to test out. They always find sample data that I never think of testing. :)

Bill the Lizard
@Bill - this is multiplication. Division by zero should not be relevant unless the implementation is doing something strange.
Stephen C
@Stephen C: Oh, right. I got a little off track there. Thanks, and I've edited my response.
Bill the Lizard
+2  A: 

1) It's best to keep your tests small and focused. That way, when a test fails, it's clear why the test failed. This usually results in a single assertion per test, but not always.

However, instead of hand-coding a test for each individual "invalid scenario", you might want to take a look at JUnit 4.4 Theories (see the JUnit 4.4 release notes and this blog post), or the JUnit Parameterized test runner.

Parametrized tests and Theories are perfect for "calculation" methods like this one. In addition, to keep things organized, I might make two test classes, one for "good" inputs, and one for "bad" inputs.

2) You only need to include the test cases that you think are most likely to expose any bugs in your code, not all possible combinations of all inputs (that would be impossible as WizardOfOdds points out in his comments). The three sets that you proposed are good ones, but I probably wouldn't test more than those three. Using theories or parametrized tests, however, would allow you to add even more scenarios.

3) There are many benefits to writing unit tests, not just the one you mention. Some other benefits include:

  • Confidence in your code - You have a high decree of certainty that your code is correct.
  • Confidence to Refactor - you can refactor your code and know that if you break something, your tests will tell you.
  • Regressions - You will know right away if a change in one part of the system breaks this particular method unintentionally.
  • Completeness - The tests forced you to think about the possible inputs your method can receive, and how the method should respond.

5) It sounds like you did a good job with coming up with possible test scenarios. I think you got all the important ones.

Jim Hurne
+1 - Good stuff on point #3.
Bill the Lizard
@Jim Hurne: It is not because a unit test passes that *"You KNOW it works"* [sic]. When a unit test is passing you know that *"it does not not work"* and that is **very** different. This is one of the most common mistake people make with unit testing: they are overly confident that their code works... (btw I've got as many lines of unit tests as lines of code, so don't mistake my point on unit testing)
Webinator
@Jim Hurne: In the same way, you are in no way *guaranteed* to catch every single regression. There are definitely a lot of projects (and lots of very high-profile projects that are very well unit tested) where regression bug appears that are sadly not caught by any unit test. However, when a test fails, you *know* you just had a regression. But there's no guaranteed **at all** that you're going to catch your regression right away.
Webinator
@Jim Hurne: And it is impossible to test all the possible inputs. A method taking, say, a *long*? Well, **good luck** testing all the possible inputs. I'm sorry but your post is utterly misleading: you mistake *"know it works"* for *"know it does not not work"*, you think you'll hit every single regression, and you think somehow can test every single input, which is completely delusional.
Webinator
@WizardofOdds - Good comments. Of course I know that it is impossible to test every possible inupt, hence my answer to #2. And of course, you can never be 100% confident that the code is correct, or that every regression will be caught, but you can certinaly be more confident than if you had no unit tests at all. I'll edit the answer to make this more obvious.
Jim Hurne
+3  A: 

1) There is a tradeoff between granularity of tests (and hence ease of diagnosis) and verbosity of your unit test code. I'm personally happy to go for relatively coarse-grained test methods, especially once the tests and tested code have stabilized. The granularity issue is only relevant when tests fail. (If I get a failure in a multi-assertion testcase, I either fix the first failure and repeat, or I temporarily hack the testcase as required to figure out what is going on.)

2) Use your common sense. Based on your understanding of how the code is written, design your tests to exercise all of the qualitatively different subcases. Recognize that it is impossible to test all possible inputs in all but the most trivial cases.

3) The point of unit testing is to provide a level of assurance that the methods under test do what they are required to do. What this means depends on the code being tested. For example, if I am unit testing a sort method, validation of user input is irrelevant.

4) The coverage seems reasonable. However, without a detailed specification of what your class is required to do, and examination of the actual unit tests, it is impossible to say if you ave covered everything. For example, is your method supposed to cope with leading / trailing whitespace characters, numbers with decimal points, numbers like "123,456", numbers expressed using non-latin digits, numbers in base 42?

5) Define "successfully tested". If you mean, do my tests prove that the code has no errors, then the answer is a definite "NO". Unless the unit tests enumerate each and every possible input, they cannot constitute a proof of correctness. (And in some circumstances, not even testing all inputs is sufficient.)

In all but the most trivial cases, testing cannot prove the absence of bugs. The only thing it can prove is that bugs are present. If you need to prove that a program has no bugs, you need to resort to "formal methods"; i.e. applying formal theorem proving techniques to your program.

And, as another answer points out, you need to give it to real users to see what they might come up with in the way of unexpected input. In other words ... whether the stated or inferred user requirements are actually complete and valid.

Stephen C
+3  A: 

True numbers of tests are, of course, infinite. That is not practical. You have to choose valid representative cases. You seem to have done that. Good job.

fastcodejava
+6  A: 

Unit testing is great (in the 200 KLOC project I'm working I've got as many unit test code as regular code) but (assuming a correct unit test):

  • a unit test that passes does not guarantee that your code works

Think of it this way:

  • a unit test that fails proves your code is broken

It is really important to realize this.

In addition to that:

  • it is usually impossible to test every possible input

And then, when you're refactoring:

  • if all your unit tests are passing does not mean you didn't introduce a regression

But:

  • if one of your unit test fails you know you have introduced a regression

This is really fundamental and should be unit testing 101.

Webinator
+1 to Stephen C and to fastcodejava
Webinator
+1 to you also, well said.
fastcodejava
+1  A: 

I just want to add, that with unit testing, you can gain even more if you think first of the possible cases and after that implement in the test driven development fashion, because this will help you stay focuesed on the current case and this will enable you to create easiest implementation possible in DRY fashion. You might also be usng some test coverage tool, e.g. in Eclipse EclEmma, which is really easy to use and will show you if tests have executed all of your code, which might help you to determine when it is enough (although this is not a proof, just a metric). Generally when it comes to unit testing I was much inspired by Kent Becks's Test Driven Development by Example book, I strongly recommend it.

Gabriel Ščerbák