You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by Benson Margulies <bi...@basistech.com> on 2007/11/17 19:02:08 UTC

Junit 4 versus us

We have a fair number of junit tests that have code in their constructor
that should only be run once.
 
Imagine my surprise to discover that this is really not a good idea,
since JUnit reconstructs for each test function.
 

RE: Junit 4 versus us

Posted by Benson Margulies <bi...@basistech.com>.
@BeforeClass requires a static method. I'm trying to make a Junit4
replacement for the AbstractDependencyInjecting.... class from Spring,
which is Junit 3, and doesn't support @Ignore on tests. A static method,
of course, can't be overidden. So, I ended up with an @Before that uses
a static boolean to get it done.
> -----Original Message-----
> From: Jiang, Ning (Willem) [mailto:Ning.Jiang@iona.com] 
> Sent: Tuesday, November 20, 2007 4:57 PM
> To: cxf-dev@incubator.apache.org
> Subject: RE: Junit 4 versus us
> 
> Hi Dan,
> 
> @BeforeClass just can be added before the static public method.
> Maybe Benson want to do that thing in a non-static method.
> 
> Willem.
> 
> 
> -----Original Message-----
> From: Daniel Kulp [mailto:dkulp@apache.org]
> Sent: Wed 11/21/2007 5:33
> To: cxf-dev@incubator.apache.org
> Cc: Benson Margulies
> Subject: Re: Junit 4 versus us
>  
> 
> Umm...  
> 
> Couldn't it be in a @BeforeClass annotated method?   Why have 
> the static 
> things?
> 
> Dan
> 
> 
> On Monday 19 November 2007, Benson Margulies wrote:
> > And if one wants a protocol using overriden functions, one 
> has to have 
> > an @Before that uses a static boolean to make it run once.
> >
> > > -----Original Message-----
> > > From: Christopher Moesel [mailto:Christopher_Moesel@avid.com]
> > > Sent: Monday, November 19, 2007 7:26 AM
> > > To: cxf-dev@incubator.apache.org
> > > Subject: RE: Junit 4 versus us
> > >
> > > Yes, I've found that any code I want only to run once (per test 
> > > class) needs to go in a static block outside the 
> constructor.  That 
> > > way it only gets processed the first time the JVM loads the class.
> > >
> > > -Chris
> > >
> > > -----Original Message-----
> > > From: Benson Margulies [mailto:bim2007@basistech.com]
> > > Sent: Saturday, November 17, 2007 1:02 PM
> > > To: cxf-dev@incubator.apache.org
> > > Subject: Junit 4 versus us
> > >
> > >
> > > We have a fair number of junit tests that have code in their 
> > > constructor that should only be run once.
> > >
> > > Imagine my surprise to discover that this is really not a 
> good idea, 
> > > since JUnit reconstructs for each test function.
> 
> 
> 
> --
> J. Daniel Kulp
> Principal Engineer
> IONA
> P: 781-902-8727    C: 508-380-7194
> daniel.kulp@iona.com
> http://www.dankulp.com/blog
> 
> 

RE: Junit 4 versus us

Posted by "Jiang, Ning (Willem)" <Ni...@iona.com>.
Hi Dan,

@BeforeClass just can be added before the static public method.
Maybe Benson want to do that thing in a non-static method.

Willem.


-----Original Message-----
From: Daniel Kulp [mailto:dkulp@apache.org]
Sent: Wed 11/21/2007 5:33
To: cxf-dev@incubator.apache.org
Cc: Benson Margulies
Subject: Re: Junit 4 versus us
 

Umm...  

Couldn't it be in a @BeforeClass annotated method?   Why have the static 
things?

Dan


On Monday 19 November 2007, Benson Margulies wrote:
> And if one wants a protocol using overriden functions, one has to have
> an @Before that uses a static boolean to make it run once.
>
> > -----Original Message-----
> > From: Christopher Moesel [mailto:Christopher_Moesel@avid.com]
> > Sent: Monday, November 19, 2007 7:26 AM
> > To: cxf-dev@incubator.apache.org
> > Subject: RE: Junit 4 versus us
> >
> > Yes, I've found that any code I want only to run once (per
> > test class) needs to go in a static block outside the
> > constructor.  That way it only gets processed the first time
> > the JVM loads the class.
> >
> > -Chris
> >
> > -----Original Message-----
> > From: Benson Margulies [mailto:bim2007@basistech.com]
> > Sent: Saturday, November 17, 2007 1:02 PM
> > To: cxf-dev@incubator.apache.org
> > Subject: Junit 4 versus us
> >
> >
> > We have a fair number of junit tests that have code in their
> > constructor
> > that should only be run once.
> >
> > Imagine my surprise to discover that this is really not a good idea,
> > since JUnit reconstructs for each test function.



-- 
J. Daniel Kulp
Principal Engineer
IONA
P: 781-902-8727    C: 508-380-7194
daniel.kulp@iona.com
http://www.dankulp.com/blog


RE: Junit 4 versus us

Posted by Benson Margulies <bi...@basistech.com>.
Dan,

Here's the soggy history of this:

Step 1: define a base class @Before that protocols with derived classes
to read in some beans.

Step 2: Get completely hosed by the engine lifecycle department.

Step 3: Get the idea that all the @Tests in the class could really just
share a set of bean-defined clients and services, they don't need them
to be reconstructed for each @Test. 

Step 4: discover that the object is constructed over and over for each
@Test, so setting up in a constructor won't work.

Step 5: resort to a static boolean.

If we had the engine lifecycle set up so that a Before/After class could
start/stop http services reliably, then I could, as it were, go back to
step 1.

 

> -----Original Message-----
> From: Daniel Kulp [mailto:dkulp@apache.org] 
> Sent: Tuesday, November 20, 2007 4:34 PM
> To: cxf-dev@incubator.apache.org
> Cc: Benson Margulies
> Subject: Re: Junit 4 versus us
> 
> 
> Umm...  
> 
> Couldn't it be in a @BeforeClass annotated method?   Why have 
> the static 
> things?
> 
> Dan
> 
> 
> On Monday 19 November 2007, Benson Margulies wrote:
> > And if one wants a protocol using overriden functions, one 
> has to have 
> > an @Before that uses a static boolean to make it run once.
> >
> > > -----Original Message-----
> > > From: Christopher Moesel [mailto:Christopher_Moesel@avid.com]
> > > Sent: Monday, November 19, 2007 7:26 AM
> > > To: cxf-dev@incubator.apache.org
> > > Subject: RE: Junit 4 versus us
> > >
> > > Yes, I've found that any code I want only to run once (per test 
> > > class) needs to go in a static block outside the 
> constructor.  That 
> > > way it only gets processed the first time the JVM loads the class.
> > >
> > > -Chris
> > >
> > > -----Original Message-----
> > > From: Benson Margulies [mailto:bim2007@basistech.com]
> > > Sent: Saturday, November 17, 2007 1:02 PM
> > > To: cxf-dev@incubator.apache.org
> > > Subject: Junit 4 versus us
> > >
> > >
> > > We have a fair number of junit tests that have code in their 
> > > constructor that should only be run once.
> > >
> > > Imagine my surprise to discover that this is really not a 
> good idea, 
> > > since JUnit reconstructs for each test function.
> 
> 
> 
> --
> J. Daniel Kulp
> Principal Engineer
> IONA
> P: 781-902-8727    C: 508-380-7194
> daniel.kulp@iona.com
> http://www.dankulp.com/blog
> 

Re: Junit 4 versus us

Posted by Daniel Kulp <dk...@apache.org>.
Umm...  

Couldn't it be in a @BeforeClass annotated method?   Why have the static 
things?

Dan


On Monday 19 November 2007, Benson Margulies wrote:
> And if one wants a protocol using overriden functions, one has to have
> an @Before that uses a static boolean to make it run once.
>
> > -----Original Message-----
> > From: Christopher Moesel [mailto:Christopher_Moesel@avid.com]
> > Sent: Monday, November 19, 2007 7:26 AM
> > To: cxf-dev@incubator.apache.org
> > Subject: RE: Junit 4 versus us
> >
> > Yes, I've found that any code I want only to run once (per
> > test class) needs to go in a static block outside the
> > constructor.  That way it only gets processed the first time
> > the JVM loads the class.
> >
> > -Chris
> >
> > -----Original Message-----
> > From: Benson Margulies [mailto:bim2007@basistech.com]
> > Sent: Saturday, November 17, 2007 1:02 PM
> > To: cxf-dev@incubator.apache.org
> > Subject: Junit 4 versus us
> >
> >
> > We have a fair number of junit tests that have code in their
> > constructor
> > that should only be run once.
> >
> > Imagine my surprise to discover that this is really not a good idea,
> > since JUnit reconstructs for each test function.



-- 
J. Daniel Kulp
Principal Engineer
IONA
P: 781-902-8727    C: 508-380-7194
daniel.kulp@iona.com
http://www.dankulp.com/blog

RE: Junit 4 versus us

Posted by Benson Margulies <bi...@basistech.com>.
And if one wants a protocol using overriden functions, one has to have
an @Before that uses a static boolean to make it run once. 

> -----Original Message-----
> From: Christopher Moesel [mailto:Christopher_Moesel@avid.com] 
> Sent: Monday, November 19, 2007 7:26 AM
> To: cxf-dev@incubator.apache.org
> Subject: RE: Junit 4 versus us
> 
> Yes, I've found that any code I want only to run once (per 
> test class) needs to go in a static block outside the 
> constructor.  That way it only gets processed the first time 
> the JVM loads the class.
> 
> -Chris
> 
> -----Original Message-----
> From: Benson Margulies [mailto:bim2007@basistech.com]
> Sent: Saturday, November 17, 2007 1:02 PM
> To: cxf-dev@incubator.apache.org
> Subject: Junit 4 versus us
> 
> 
> We have a fair number of junit tests that have code in their 
> constructor
> that should only be run once.
>  
> Imagine my surprise to discover that this is really not a good idea,
> since JUnit reconstructs for each test function.
>  
> 

RE: Junit 4 versus us

Posted by Christopher Moesel <Ch...@avid.com>.
Yes, I've found that any code I want only to run once (per test class)
needs to go in a static block outside the constructor.  That way it only
gets processed the first time the JVM loads the class.

-Chris

-----Original Message-----
From: Benson Margulies [mailto:bim2007@basistech.com] 
Sent: Saturday, November 17, 2007 1:02 PM
To: cxf-dev@incubator.apache.org
Subject: Junit 4 versus us


We have a fair number of junit tests that have code in their constructor
that should only be run once.
 
Imagine my surprise to discover that this is really not a good idea,
since JUnit reconstructs for each test function.