dcsimg
Initialization in setUp or in constructor?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Per_Velschow
Posted On:   Saturday, December 21, 2002 12:25 AM

I generally believe it is much safer to do initialization in the constructor (or in instance initializers). One advantage of this is that you can make fields final so that the compiler makes sure that you didn't forget to initialize these fields. Most JUnit test samples performs the common initialization in the setUp method of the TestCase subclass. Are there any good reasons for doing that? I can think of one reason (don't know if it is a good one). I tried to put my initializations into the constructor. But it seems that the constructor gets called at a very early state by the TestRunner. So if these initializations fail, they don't show up as failures . Also, will the TestCase constru   More>>

I generally believe it is much safer to do initialization in the constructor (or in instance initializers). One advantage of this is that you can make fields final so that the compiler makes sure that you didn't forget to initialize these fields.


Most JUnit test samples performs the common initialization in the setUp method of the TestCase subclass. Are there any good reasons for doing that?


I can think of one reason (don't know if it is a good one). I tried to put my initializations into the constructor. But it seems that the constructor gets called at a very early state by the TestRunner. So if these initializations fail, they don't show up as failures .


Also, will the TestCase constructor be called exactly once for each single test method? Or just once for the class? In other words, does JUnit instantiate one TestCase instance per subclass or one per test method in that subclass? Or is this undefined?

   <<Less

Re: Initialization in setUp or in constructor?

Posted By:   Anonymous  
Posted On:   Sunday, December 22, 2002 12:57 PM

It is specified to create exactly one instance per test method.

So you are totally right with your two reasons to use setUp over the constructor.

Re: Initialization in setUp or in constructor?

Posted By:   Brian_Kapellusch  
Posted On:   Saturday, December 21, 2002 07:24 AM

The setUp method is called before each test (as the tearDown method is called after each test). The constructor is called only once (upon construction of the class).
About | Sitemap | Contact