aspose file tools*
The moose likes Testing and the fly likes Why does JUnit create a new instance of a class each time it calls a test method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Why does JUnit create a new instance of a class each time it calls a test method?" Watch "Why does JUnit create a new instance of a class each time it calls a test method?" New topic
Author

Why does JUnit create a new instance of a class each time it calls a test method?

Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
Hi,

I am asking this question only to learn better the logic behind things. I noticed from the logs of the test classes I'm writing that JUnit actually creates a class for each test method call. In other words, if I have a class MyTest that has the methods testA(), testB(), and testC() then Junit will create an instance of MyTest for each of the method calls.

Now one could argue that it is so that each test is treated as starting from fresh; however aren't the setUp() and tearDown() method exactly for that?

Regards,
Sim085
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://martinfowler.com/bliki/JunitNewInstance.html
Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
David Newton wrote:http://martinfowler.com/bliki/JunitNewInstance.html


Thanks, that was exactly what I was looking for :)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No problem :)
 
 
subject: Why does JUnit create a new instance of a class each time it calls a test method?