• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I Junit test getConnection/create methods?

 
Billy Sclater
Ranch Hand
Posts: 145
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've heard a variety of opinions on this, but I wonder what your take on it is.

Here is my getConnection method:


And here is my create method:


Here is my getConnection Junit test:


And here is my create method Junit test:

I should point out that the database under test is just a testDB created by other code and not any real commercial DB!
I get the nagging feeling that these are terrible tests! Any suggestions?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35274
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you wrote is called a "smoke test". It tests that the code doesn't blow up - a reasonable thing to do.

Question: Do you really want to exit your program if there is an exception?
 
Billy Sclater
Ranch Hand
Posts: 145
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks(: About your question, what I'd like to happen if my create method throws an exception is for the test to fail. By executing system.exit the test ends in a grey colored bar (not quite a fail I know), but not a pass either!

The only way I can think to fail the test on an exception is to perhaps not 'catch' it in the create method itself, but just to throw it, and then to enclose the invocation of create in a try catch block, with the catch block executing fail("failed").

Not sure if this is good practice though, what do you think?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your problem correctly, you don't have to handle the exception at all. When an exception is thrown during the test (checked or unchecked), the test fails. You don't need to do anything to let the test fail when an exception is thrown.
 
Billy Sclater
Ranch Hand
Posts: 145
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys(: That brings me to another question. I have a 'closeResultSet' method which obviously closes a ResultSet(!), handles likely exceptions etc. In order to test it I need to execute an SQL statement inside the test method, and add the result to a ResultSet, then close it using the closeResultSet method.

Of course now the test method now needs to throw or handle the potential SQLException.

Is it bad form to add the 'throws' clause to a junit test method?



 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Billy Sclater wrote:Is it bad form to add the 'throws' clause to a junit test method?

In my opinion, it's quite ok for unit test methods. It actually documents that the method can throw the exception, in which case the test fails.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic