File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 99
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: 99
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 :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does JUnit create a new instance of a class each time it calls a test method?
 
Similar Threads
Static Method/Non-Static Method, RTTI
Shallow in static method ?
Any decent tutorials on mocking methods
Questions abt Groovy environment
Junit - inconsitant objects