• 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

test classes for constructors and successful database connection

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class with the only method being the constructor that I have included below. I have been given a task of writing a test method for the constructor but I am not sure on how to go about it. I am confused on how to test the constructor below or how to test for a successful database connection?
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post UserDao's constructor? That's where all the work gets done so you are really testing that.
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Can you post UserDao's constructor? That's where all the work gets done so you are really testing that.

Here is the class below. The test should not involve any database testing
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before you even try to test you need to add finally blocks and close all those statements, result sets, and connections. If you don't, you are going to have horrendous memory leaks and consume all the server resources.
 
misheck Mberi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:Before you even try to test you need to add finally blocks and close all those statements, result sets, and connections. If you don't, you are going to have horrendous memory leaks and consume all the server resources.



Thanks Kevin, I would have to look at how can add the finally blocks too.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll find a good tutorial here.

I do mine like so:

Some people leave out the null tests, I prefer to do it this way. Always close them in this order; ResultSet first if you have one. If you did an excuteUpdate then of course you didn't get a ResultSet returned to you. Then the Statement whether a Prepared or Callable, and finally the Connection. Remember that the finally block always gets executed whether or not the code throws an exception so this is where cleanup code goes. By doing this you ensure that the objects can be garbage collected.
 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
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
All the constructor does is get a connection. I wouldn't write a test just for that. I'd write an integration test to test the actual DAO methods. Which would call the database so I would want an actual database connection.

You could meet the literal request by checking that the dao object is set (using reflection). Or even just asserting that the code didn't blow up on getting a connection.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic