Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Novice Newsgroups: comp.lang.java.programmer Subject: Re: Question re testing constructor Date: Tue, 6 Dec 2011 17:15:09 +0000 (UTC) Organization: Your Company Lines: 39 Message-ID: References: NNTP-Posting-Host: Fs5DYasnrq9r1nxSJvGAbQ.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: Xnews/5.04.25 X-Antivirus-Status: Clean X-Notice: Filtered by postfilter v. 0.8.2 X-Antivirus: avast! (VPS 111206-1, 2011-12-06), Outbound message Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10561 Peter Duniho wrote in news:s8adnYgtFe-Nq0PTnZ2dnUVZ_oGdnZ2d@posted.palinacquisition: > On 12/6/11 7:14 AM, Novice wrote: >> [...] >> I've satisfied myself via googling that it makes sense to test >> constructors if they do something that might fail or if they can >> throw exceptions. The constructor in question can throw exceptions so >> I'm trying to write some test cases for it. I've got cases that cause >> each of the exceptions to be thrown but I'm having a bit of a problem >> with what seemed like the easiest test of all: existence. >> >> It seems to me - correct me if I'm wrong - that I can easily test to >> make sure the constructor actually built SOMETHING by testing the >> object against null. [...] > > Sorry. You're wrong. :) Simply executing the next statement > following the use of "new" is all the confirmation you need "to make > sure the constructor actually built SOMETHING". Testing against > "null" yields no additional information. > > As "markspace" says, the "new" operator always returns a new object. > The only way for it to fail is to throw an exception, in which case > the "new" operator never actually returns per se (and so any code that > follows it is reachable only the case where it returns a non-null > value). > > So you don't need to test for existence. Checking for a "null" return > value will needlessly verify that "new" succeeded, but cannot detect > failure because that's not how "new" reports a failure. Just test the > exceptions as you're already doing. > Thanks Pete! You and markspace have clarified things for me and saved me some work to boot since I can now delete the existence tests. -- Novice