aspose file tools*
The moose likes Testing and the fly likes why does junit create a instance of the testcase class each time it wants to execute testXXX() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "why does junit create a instance of the testcase class each time it wants to execute testXXX()" Watch "why does junit create a instance of the testcase class each time it wants to execute testXXX()" New topic
Author

why does junit create a instance of the testcase class each time it wants to execute testXXX()

Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

Hello All,

I am new to Junit and have been reading up on the tutorials I understand that junit creates a instance of the testcase class each time it wants to execute testXXX() method, I was wondering why ?? Why not just create one instance and then execute

1) setUp
2) testXXX
3) teardown

in the above order for each testXXX method ?

and I also I read that it uses a collecting parameter design pattern to fill in the TestResult object with the test results, It uses the collecting parameter design pattern to collect the output of the multiple methods it executes, (This is what I have understood from the text I have gone through)

The text I was talking about

The Smalltalk Best Practice Patterns (see Beck, K. Smalltalk Best Practice Patterns, Prentice Hall, 1996) has a pattern that is applicable. It is called Collecting Parameter. It suggests that when you need to collect results over several methods, you should add a parameter to the method and pass an object that will collect the results for you. We create a new object, TestResult, to collect the results of running tests.


Now my question
if each testXXX is executed through a different instance how does it collect the results ??

My questions are inter linked I sort of feel that I am misunderstanding something here !



Thanks in advance
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

Sid,
1) I like this behavior as it doesn't allow the tests to interfere with each other across runs. I use a lot of instance variables and certainly don't null them all out in the tear down.

2) The framework remembers it. I think I'm not understanding this part of the question.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

Hi All,

I did edit my previous post so that it becomes more clear, Just letting you all know

Thanks
Sudarshan
Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

Hello All,

This is the answer for my own question , just putting it here in case some one else finds it usefull.

Now if each test case method is executed through a different instance how does it collect the results? By using the Collecting Parameter pattern of course!

Like I said before, a TestSuite is created with different instances of your TestCase class each executing a different test case method. It is your TestSuite that makes use of the above pattern and passes a TestResult object as a parameter to every TestCase it runs to collect the results.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

Thanks Sid!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why does junit create a instance of the testcase class each time it wants to execute testXXX()