ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <>
Subject RE: JUnit: setUp() method called multiple times ?
Date Thu, 03 Oct 2002 14:24:49 GMT
Just be aware that the constructor is called way early, when JUnit is
building the test suite of all the tests. It may not matter with
<junit>/<batchtest>, but it does with the text TestRunner

Either use junit.extensions.TestSetup, or more easily ensure yourself the
initialization happens only once with a static boolean of your own. Just
keep in mind that JUnit was designed to have each test run in its own test
fixture, i.e. testing environment, which should be identical for all the
tests of a single class (but not the same instance, see below). If any of
your tests have side effects on your initialized-once-only resource, then
you're violating this JUnit design principle. It's OK in many cases, you
just have to be aware of it...

Finally, I'll mention that each testXYZ method (and the setUp/teardown
around it) runs in a separate instance of the test class (again, to ensure
no side effects from one test to another). So state kept in non-static
member is *not* shared by the different testXYZ methods.

Does that make sense? --DD

-----Original Message-----
From: Conor MacNeill [] 
Sent: Thursday, October 03, 2002 5:22 AM
To: Ant Users List
Subject: Re: JUnit: setUp() method called multiple times ?

P. Fleury wrote:
> Erik Hatcher wrote:
>> Correct.  This is just the way JUnit works (regardless of it being run 
>> from Ant or otherwise).  setUp(), testXXX, tearDown().... setUp(), 
>> testZZZ(), tearDown() - that is the JUnit-way, call setUp(), then a 
>> test method, then tearDown() repeatedly.
>>     Erik
> Mmm. Well, if in my class, I have 3 test functions which needs some 
> object to be initialized to run properly. Now it takes a looong time to 
> setup that object, so in the current situation, if my class is

For one-off stuff just put it into the test class constructor.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message