Discussion:
@BeforeClass vs. TestCase's constructor?
kk_oop
2010-07-26 18:38:06 UTC
Permalink
Hi. I have a JUnit 4 TestCase, let's call it MyTestCase. I want to initialize some state once prior to running any of MyTestCase's tests. I see my options are to give MyTestCase some static attributes and initialize them with @BeforeClass or to give MyTestCase some instance attributes and initialize them with MyTestCase's constructor.

Any recommendations for one approach over the other?

Thanks!

Ken



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/junit/join
(Yahoo! ID required)

<*> To change settings via email:
junit-***@yahoogroups.com
junit-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
David Saff
2010-07-26 18:49:00 UTC
Permalink
@BeforeClass runs only once ever, whereas the constructor runs once
per test method. Does that help?

David
Post by kk_oop
Any recommendations for one approach over the other?
Thanks!
Ken
------------------------------------
Yahoo! Groups Links
------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/junit/join
(Yahoo! ID required)

<*> To change settings via email:
junit-***@yahoogroups.com
junit-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
kk_oop
2010-07-26 23:46:36 UTC
Permalink
Hi. I see! Thanks for clearing that up!

That being said, is there a time I should use the test case's constructor instead of @Before?

Thanks again,

Ken
Post by David Saff
@BeforeClass runs only once ever, whereas the constructor runs once
per test method. Does that help?
David
<snip>



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/junit/join
(Yahoo! ID required)

<*> To change settings via email:
junit-***@yahoogroups.com
junit-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
David Saff
2010-07-27 02:42:12 UTC
Permalink
There's a slight difference between constructors and @Before: an
exception thrown from the constructor permanently kills the test,
without further cleanup (there's no object to clean up), whereas an
exception thrown from a @Before can still be cleaned up in an @After.

David Saff
Hi.  I see!  Thanks for clearing that up!
Thanks again,
Ken
Post by David Saff
@BeforeClass runs only once ever, whereas the constructor runs once
per test method.  Does that help?
   David
<snip>
------------------------------------
Yahoo! Groups Links
------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/junit/join
(Yahoo! ID required)

<*> To change settings via email:
junit-***@yahoogroups.com
junit-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/

Loading...