Re: Need help designing some JUnit tests
On 05/21/2010 08:28 PM, Rhino wrote:
Followup question. I just read up on assert and it is apparently not
supposed to be used in public methods. Since it is my getInstance() method
Wrong.
that invokes the private constructor, the getInstance() is where I'd put
the 'assert' right? After all, my constructor is completely empty.
If I follow the rule about not using 'assert' in public methods, I'm not
That's not a rule. You are not reading the advice carefully.
You're not supposed to use 'assert' to validate arguments to a public method.
There are plenty of use cases for 'assert' in a public method.
sure how to reconcile you advice with the rule in the assertions article
(http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html)....
Read the article again, more carefully.
The purpose of 'assert' is to prove that invariants hold. It's actually not
so far different from the JUnit 'assertXxx()' methods, where 'assert' proves
within the code itself that algorithmic invariants hold, and JUnit's
assertions prove that test invariants hold in a test class outside the code
itself. Both throw 'Error's when violated.
Is the assertNotNull in the test case a reasonable substitute for doing the
assert in the getInstance()?
Not really.
They don't really do the same thing, exactly.
--
Lew