Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10557

Re: Question re testing constructor

Date 2011-12-06 07:34 -0800
From Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
Newsgroups comp.lang.java.programmer
Subject Re: Question re testing constructor
References <Xns9FB3682A7DBF6jpnasty@94.75.214.39>
Message-ID <s8adnYgtFe-Nq0PTnZ2dnUVZ_oGdnZ2d@posted.palinacquisition> (permalink)

Show all headers | View raw


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.

Pete

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question re testing constructor Novice <novice@example..com> - 2011-12-06 15:14 +0000
  Re: Question re testing constructor markspace <-@.> - 2011-12-06 07:23 -0800
    Re: Question re testing constructor Novice <novice@example..com> - 2011-12-06 17:17 +0000
    Re: Question re testing constructor Tom Anderson <twic@urchin.earth.li> - 2011-12-06 21:25 +0000
    Re: Question re testing constructor Ian Shef <invalid@avoiding.spam> - 2011-12-07 00:29 +0000
      Re: Question re testing constructor Patricia Shanahan <pats@acm.org> - 2011-12-06 19:28 -0800
      Re: Question re testing constructor Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-12-06 22:57 -0600
      Re: Question re testing constructor Lew <lewbloch@gmail.com> - 2011-12-07 07:12 -0800
      Re: Question re testing constructor Lew <lewbloch@gmail.com> - 2011-12-07 07:14 -0800
        Re: Question re testing constructor Ian Shef <invalid@avoiding.spam> - 2011-12-07 15:59 +0000
          Re: Question re testing constructor Ian Shef <invalid@avoiding.spam> - 2011-12-08 19:19 +0000
      Re: Question re testing constructor ilAn <idonot@wantspam.net> - 2011-12-07 20:41 +0200
  Re: Question re testing constructor Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-12-06 07:34 -0800
    Re: Question re testing constructor Novice <novice@example..com> - 2011-12-06 17:15 +0000
  Re: Question re testing constructor Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-06 09:49 -0800
  Re: Question re testing constructor Roedy Green <see_website@mindprod.com.invalid> - 2011-12-06 16:40 -0800
  Re: Question re testing constructor Henk van Voorthuijsen <voorth@xs4all.nl> - 2011-12-07 00:33 -0800

csiph-web