• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I Junit test getConnection/create methods?

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic