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


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

Re: Question re testing constructor

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Question re testing constructor
References <Xns9FB3682A7DBF6jpnasty@94.75.214.39>
Message-ID <BAsDq.13110$LO2.932@newsfe13.iad> (permalink)
Date 2011-12-06 09:49 -0800

Show all headers | View raw


On 12/6/11 7:14 AM, Novice wrote:
> I'm writing JUnit test cases for a constructor but have hit a bit of a
> snag. I hope someone here can help.
>
> 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. That won't prove it created exactly the right object but it will
> prove that something got created. Testing the objects OTHER methods will
> verify that the correct object got created.
>
> If that is right, then it seems that this should test the existence of the
> object well enough:
>
>      	Set<String>  testValues = new HashSet<String>();
>      	testValues.add("FF0000");
>      	testValues.add("66CC99");
>      	
>      	for (String key : testValues) {
>      		HexColor hexColor = new HexColor(key);
>      		if (hexColor == null) {
>      			assertTrue("The HexColor has been created for input value,
> " + key + ", but has been found to be null.", false);
>      		}
>      	}
>
> Unfortunately, the assertTrue() statement gets flagged by the compiler as
> being dead code. Am I right in assuming that it is essentially looking at
> the instantiation of the HexColor class and reasoning that it will
> inevitably create SOMETHING so that hexColor can't possibly be null,
> therefore the assertTrue() can't ever be executed?
>
> If so, I don't have a problem with that but it leaves me a bit baffled
> about how to test that the constructor created something when I gave it
> good input values.
>
> Can someone enlighten me on a better way to test this aspect of the
> constructor? Or can I simply assume that the constructor worked as long as
> it didn't actually throw an exception and omit any existence tests?
>

Tests work best if you think about pre-conditions and post-conditions.

Test 1:
Pre condition: I pass these objects to a constructor...
Expected post conditions: I have an object with these state/behavior.

Test 2:
Pre condition: I pass these other objects to a constructor...
Expected post condition: Constructor throws NullPointerException.


Etc...

So, if your constructor is non-trivial, you should be able to interact 
with the object to verify the constructor did its job.

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