• 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

One time pass setUp()/tearDown() in JUnit

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Is there any way in JUnit to perform something once before (and after) all test cases are executed ?
For instance, say you need to connect to a database, run all the test cases and then close the connection.
setUp() and tearDown() if used, I believe, will open and close the connection for every test case.
By the way, I'm using the JUnit 3.7 (WSAD plug-in).
Thanks.
 
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the best way would be for you to connect to the database and after all your tests are over restore your database to the original state ( when youstarted running your test cases). We have a method which restores the databse to its previous state after the test casees are run. In our test class( or a suite ) the teardwon method makes a call to this method. The setUp will make a call to a method which will make the database set up for you for your test cases for running.
Hope this helps.

Vasu
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Maybe I misunderstood the way setUp() and tearDown() methods work. Don't they get called before and after, respectively, every test case execution ?
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct that they get called before/after every execution. You can accomplish the kind of thing that you propose by creating a flag attribute for your TestCase class, and then checking whether that flag's been set before doing setup or teardown. That assumes that you have total control of your test database - if several developers can access the database and run tests against it, you will want a more robust solution. (But then, you'd really have needed a more robust solution anyway - someone could have changed the data out from under you mid-test.)
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Thanks for the tips.
Maybe it would be a good idea to have initialize() and terminate() (or any meaningful names...) methods in the framework then. These could get executed once right after the unit test starts and right before it ends, respectively. Could be handy for apps connecting to DB's, messaging systems...
An idea I had was to have the connection being established in the constructor of the test case class. This way it will be called just once. However the test case class wouldn't know when to close it, when 'the end' happens. I may be wrong but the same thing would happen if using a flag as you suggested.
For this question, please assume database contents is not a concern.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what junit.extensions.TestSetup is for.
 
Tina Coleman
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ilja! Can you point us to some documentation or notes on appropriate usage of the Junit extensions? (I hadn't run across the set before. . .)
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja, thank you very much !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic