Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:http://martinfowler.com/bliki/JunitNewInstance.html


Thanks, that was exactly what I was looking for :)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic