November 17, 2004

One-time setup and tear down for unit tests in JUnit

Filed under: Java — Barry Hawkins @ 9:46 pm

I had been wrestling with an odd problem for days in my current project at work. We have a project using the Spring framework, the Struts framework, and Hibernate. When trying run a JUnit TestCase
that has more than one test, I kept getting an error saying that more than one database connection pool with the same name was attempting to be registered with Hibernate through the Spring application context.

Pragmatic Unit Testing in Java with JUnit to the rescue.

I had mistakenly thought that the setUp() and tearDown() methods for a TestCase ran once each, before and after any of the tests had run. The code sample on page 31 of the book had an easily-understood and concise code sample of how to wrap your tests in a TestSuite within the TestCase class and then provide methods to contain work that is called for the setUp() and tearDown() methods of the suite. Needless to say, the approach worked like a champ.

God bless those Pragmatic Programmers.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress