Sharing database connection across multiple tests
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Steve_Leach
Posted On:   Thursday, August 7, 2003 01:34 PM

I have a DataStore class which is an abstraction of my SQL database, and I am testing it using JUnit. When running under Tomcat it gets a connection pool from the web server, and I have given it a "test" initialisation mode that just opens a single direct connection instead. What I want to do is have all the tests in a test class use this same connection, to avoid opening/closing connections over and over again. I realise that sharing context between tests is generally considered a bad idea, but for DB connections it seems to make sense. My question is, what is the best way of sharing a database connection amongst all the tests in a test class ? Should I open the connection in a constructor ?    More>>

I have a DataStore class which is an abstraction of my SQL database, and I am testing it using JUnit. When running under Tomcat it gets a connection pool from the web server, and I have given it a "test" initialisation mode that just opens a single direct connection instead.


What I want to do is have all the tests in a test class use this same connection, to avoid opening/closing connections over and over again. I realise that sharing context between tests is generally considered a bad idea, but for DB connections it seems to make sense.


My question is, what is the best way of sharing a database connection amongst all the tests in a test class ? Should I open the connection in a constructor ?


Any advice would be greatly appreciated.

   <<Less

Re: Sharing database connection across multiple tests

Posted By:   Anonymous  
Posted On:   Friday, August 8, 2003 07:25 AM

Opening it in the constructor wouldn't help, as for every test-method a new instance of the test class is created.

What you could do is wrap your suite in a TestSetup which opens the connection in its setUp method.

Re: Sharing database connection across multiple tests

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, August 7, 2003 02:43 PM

Personally I would think that it is no longer a "unit test" if it shares anything with prior or subsequent tests...


Now if you recreate the connection pool from scratch, for each test, that's a different story.

About | Sitemap | Contact