Junit 3 suite setup tear down

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Junit 3 suite setup tear down

by lars-38 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi
At my company we have alot of automated test cases and in all our
suite we have a setup testcase and a teardown test case where we setup
network connection that takes some time and can not be done in every TC. ,
But i think that there should exist a suite setup and a teardown and
not be in a setup/teardown TC.
Are there any why of doing this or how could i best be solve this by
extending TestSuite?

Regards Lars


Re: Junit 3 suite setup tear down

by toalexsmail :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In junit@..., "lars" <lars.carlsson@...> wrote:
>
> Hi
> At my company we have alot of automated test cases and in all our
> suite we have a setup testcase and a teardown test case where we setup
> network connection that takes some time and can not be done in every
TC. ,
> But i think that there should exist a suite setup and a teardown and
> not be in a setup/teardown TC.
> Are there any why of doing this or how could i best be solve this by
> extending TestSuite?
>
> Regards Lars
>
I think that there is some related thread on this topic, but never mind.

* There are @Before and @After annotation that are equivalent of use
of setUp() and tearDown().
* There are @BeforeClass and @AfterClass annotation that are supposed
to substitute the use of TestSuite. In fact, this method is called
once per class. You can see on it as your TestSuite was just a single
class. AFAIK, there is no build in support for TestSuite that is not
so trivial.

I had similar requirement, namely I wanted to ensure that JDK 1.4+
assert statement are on, I wanted to start JBoss microcontainer at the
beginning and shutdown it at the end.

Lets start from the simplest one.
*  I wanted to ensure that JDK 1.4+ assert statement are on.
I created class with name BaseTestCase and all my test classes
inherits from it. In this class I put the following method:


@BeforeClass
public static void oneTimeSetup(){
        // do your one time set-up here!
        log.info("BaseTestCase.oneTimeSetup()");
        log.debug("Going to check whether asserts are enabled");
        boolean assertsEnabled = false;
        assert (assertsEnabled = true); // Intentional side effect!!!
        if (!assertsEnabled) {
                throw new AssertionError("Asserts must be enabled!");
                }
        }
}

Because all my test classes inherit from BaseTestCase and has their
base class BaseTestCase, it is guaranteed that first thing that will
be run (beside JUnit itself) is oneTimeSetup() method.
       
* I wanted to start JBoss microcontainer at the beginning and shutdown
it at the end.

Every test class inherit from some base test class, say
BaseHelloWorldEJBTest  (that eventually inherit from BaseTestCase). In
BaseHelloWorldEJBTest  there is method

@BeforeClass
public static void setUpClass() throws Exception ;

This method basically does the following:

final Bootstrap jbossInstance =  Bootstrap.getInstance();
if (!jbossInstance.isStarted()) {
        log.debug("starting JBoss microcontainer");
        jbossInstance.bootstrap();
        log.debug("JBoss microcontainer is up");

        log.debug("Going to deploy scanClasspath");
        deploy(scanClasspath);
        log.debug("End of deploy scanClasspath");
        ... <HERE there is more code>
}

Until now everything is pretty straight-forward. But how can I stop
JBoss when I finished testing? I don't want to stop it after every
class, but after whole TestSuite is done. I didn't find something
built-in, so I use ShutdownHook...

In the above setUpClass() mehtod in the place that noted as "<HERE
there is more code>" I have:

log.debug("Now add hook to shutdown server");
Runtime.getRuntime().addShutdownHook(new Thread() {
 public void run() {
 assert lRunnables!=null;
 Runnable runnable =null;
 String runnableI = null;
 try {
  for(int i=0;i<lRunnables.length;i++){
  runnable = lRunnables[i];
  runnableI = "runnable "+i;
  log.debug("Going to run "+runnableI);
  if(runnable==null){
   log.warn(runnableI+" is null, skipping");
        } else {
           try {
            runnable.run();
            log.debug(runnableI+" ends run succesfully");
            }
           catch(Throwable e){log.error(runnableI+" fails", e);}
             
           }
        }
                                   
  }

  catch(Throwable e){
    log.error("This should never happened. Some runnables Throwable
(exception) propogates outside the loop", e);
   }
                           
                           
   synchronized (jbossInstance) {
        jbossInstance.shutdown();
   }
 String junitStatus = junitStatusOs.toString();
 log.debug(junitStatus);
                               
  }
});
I will talk about junitStatusPs a bit later. What is actually is done
here that in @BeforeClass that it is before running every class, there
is check whether JBoss is run or no. If it is run nothing special is
done. If it is not run, and this will be true only first time this
check will be done, JBoss is started and ShutDownHook is added. Take a
look on the JavaDoc on ShutDownHook, but basically, on the call on
System.exit() JVM will be run your hook. In that shutdown hook I make
jbossInstance.shutdown().


There is one issue here. One JBoss is shutting down it write some
thing to the log. What is happen, that I see JUnit report (90000
tested passes, 100 fails, and 10 ignored) and after this report I see
the output from shitting down process. This happen, because the
shutdown hook happens after last JUnit class finished to run.
I found some work around. In the BaseTestCase I have:
       // the logger
        private static final Log log = LogFactory.getLog(BaseTestCase.class);
        protected static final OutputStream junitStatusOs = new
ByteArrayOutputStream(16*1024);
       
This junitStatusPs is actually passed to as PrintStream (instead
System.out) to the JUnit classes. Note, that I have

 String junitStatus = junitStatusOs.toString();
 log.debug(junitStatus);
                               
as the last row of the shutdown hook. This ensure, that I will see the
output of the shutdown hook first, and the JUnit report will be the
last rows in the standard output.
And the last issue is:

If I want to do some other thing I just wrap it as lRunnables and pass
to the method mentioned above. It is looks like this:

    //we want to call cleanUp() method that ear application calls
    //moment before we shutting down JBoss
        Runnable runnable = new Runnable(){
        public void run() {
        try {
        cleanUp();        
        }
        catch(HelloWorldException e){
        log.error("Failed to cleanUp", e);
        }
        }
        };
   
        setUpClass(System.getProperty("user.dir") + "/target/classes",
runnable);        

In order to shutdown JBoss we use
        @AfterClass
        public static void tearDownClass() throws Exception {
        log.info("one time tearDownClass()");
                undeploy();
                Bootstrap.getInstance().shutdown();
        }



Re: Junit 3 suite setup tear down

by toalexsmail :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In junit@..., "lars" <lars.carlsson@...> wrote:
>
> Hi
> At my company we have alot of automated test cases and in all our
> suite we have a setup testcase and a teardown test case where we setup
> network connection that takes some time and can not be done in every
TC. ,
> But i think that there should exist a suite setup and a teardown and
> not be in a setup/teardown TC.
> Are there any why of doing this or how could i best be solve this by
> extending TestSuite?
>
> Regards Lars
>
I think that there is some related thread on this topic, but never mind.

* There are @Before and @After annotation that are equivalent of use
of setUp() and tearDown().
* There are @BeforeClass and @AfterClass annotation that are supposed
to substitute the use of TestSuite. In fact, this method is called
once per class. You can see on it as your TestSuite was just a single
class. AFAIK, there is no build in support for TestSuite that is not
so trivial.

I had similar requirement, namely I wanted to ensure that JDK 1.4+
assert statement are on, I wanted to start JBoss microcontainer at the
beginning and shutdown it at the end.

Lets start from the simplest one.
*  I wanted to ensure that JDK 1.4+ assert statement are on.
I created class with name BaseTestCase and all my test classes
inherits from it. In this class I put the following method:


@BeforeClass
public static void oneTimeSetup(){
        // do your one time set-up here!
        log.info("BaseTestCase.oneTimeSetup()");
        log.debug("Going to check whether asserts are enabled");
        boolean assertsEnabled = false;
        assert (assertsEnabled = true); // Intentional side effect!!!
        if (!assertsEnabled) {
                throw new AssertionError("Asserts must be enabled!");
                }
        }
}

Because all my test classes inherit from BaseTestCase and has their
base class BaseTestCase, it is guaranteed that first thing that will
be run (beside JUnit itself) is oneTimeSetup() method.
       
* I wanted to start JBoss microcontainer at the beginning and shutdown
it at the end.

Every test class inherit from some base test class, say
BaseHelloWorldEJBTest  (that eventually inherit from BaseTestCase). In
BaseHelloWorldEJBTest  there is method

@BeforeClass
public static void setUpClass() throws Exception ;

This method basically does the following:

final Bootstrap jbossInstance =  Bootstrap.getInstance();
if (!jbossInstance.isStarted()) {
        log.debug("starting JBoss microcontainer");
        jbossInstance.bootstrap();
        log.debug("JBoss microcontainer is up");

        log.debug("Going to deploy scanClasspath");
        deploy(scanClasspath);
        log.debug("End of deploy scanClasspath");
        ... <HERE there is more code>
}

Until now everything is pretty straight-forward. But how can I stop
JBoss when I finished testing? I don't want to stop it after every
class, but after whole TestSuite is done. I didn't find something
built-in, so I use ShutdownHook...

In the above setUpClass() mehtod in the place that noted as "<HERE
there is more code>" I have:

log.debug("Now add hook to shutdown server");
Runtime.getRuntime().addShutdownHook(new Thread() {
 public void run() {
 assert lRunnables!=null;
 Runnable runnable =null;
 String runnableI = null;
 try {
  for(int i=0;i<lRunnables.length;i++){
  runnable = lRunnables[i];
  runnableI = "runnable "+i;
  log.debug("Going to run "+runnableI);
  if(runnable==null){
   log.warn(runnableI+" is null, skipping");
        } else {
           try {
            runnable.run();
            log.debug(runnableI+" ends run succesfully");
            }
           catch(Throwable e){log.error(runnableI+" fails", e);}
             
           }
        }
                                   
  }

  catch(Throwable e){
    log.error("This should never happened. Some runnables Throwable
(exception) propogates outside the loop", e);
   }
                           
                           
   synchronized (jbossInstance) {
        jbossInstance.shutdown();
   }
 String junitStatus = junitStatusOs.toString();
 log.debug(junitStatus);
                               
  }
});
I will talk about junitStatusPs a bit later. What is actually is done
here that in @BeforeClass that it is before running every class, there
is check whether JBoss is run or no. If it is run nothing special is
done. If it is not run, and this will be true only first time this
check will be done, JBoss is started and ShutDownHook is added. Take a
look on the JavaDoc on ShutDownHook, but basically, on the call on
System.exit() JVM will be run your hook. In that shutdown hook I make
jbossInstance.shutdown().


There is one issue here. One JBoss is shutting down it write some
thing to the log. What is happen, that I see JUnit report (90000
tested passes, 100 fails, and 10 ignored) and after this report I see
the output from shitting down process. This happen, because the
shutdown hook happens after last JUnit class finished to run.
I found some work around. In the BaseTestCase I have:
       // the logger
        private static final Log log = LogFactory.getLog(BaseTestCase.class);
        protected static final OutputStream junitStatusOs = new
ByteArrayOutputStream(16*1024);
       
This junitStatusPs is actually passed to as PrintStream (instead
System.out) to the JUnit classes. Note, that I have

 String junitStatus = junitStatusOs.toString();
 log.debug(junitStatus);
                               
as the last row of the shutdown hook. This ensure, that I will see the
output of the shutdown hook first, and the JUnit report will be the
last rows in the standard output.
And the last issue is:

If I want to do some other thing I just wrap it as lRunnables and pass
to the method mentioned above. It is looks like this:

    //we want to call cleanUp() method that ear application calls
    //moment before we shutting down JBoss
        Runnable runnable = new Runnable(){
        public void run() {
        try {
        cleanUp();        
        }
        catch(HelloWorldException e){
        log.error("Failed to cleanUp", e);
        }
        }
        };
   
        setUpClass(System.getProperty("user.dir") + "/target/classes",
runnable);        

In order to shutdown JBoss we use
        @AfterClass
        public static void tearDownClass() throws Exception {
        log.info("one time tearDownClass()");
                undeploy();
                Bootstrap.getInstance().shutdown();
        }



Re: Re: Junit 3 suite setup tear down

by Cédric Beust ♔ :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That's a lot of work and therefore a good candidate for a new feature:
@BeforeSuite/@AfterSuite.

--
Cédric


On Wed, Jul 9, 2008 at 12:47 PM, toalexsmail <toalexsmail@...> wrote:

>   --- In junit@... <junit%40yahoogroups.com>, "lars"
> <lars.carlsson@...> wrote:
> >
> > Hi
> > At my company we have alot of automated test cases and in all our
> > suite we have a setup testcase and a teardown test case where we setup
> > network connection that takes some time and can not be done in every
> TC. ,
> > But i think that there should exist a suite setup and a teardown and
> > not be in a setup/teardown TC.
> > Are there any why of doing this or how could i best be solve this by
> > extending TestSuite?
> >
> > Regards Lars
> >
> I think that there is some related thread on this topic, but never mind.
>
> * There are @Before and @After annotation that are equivalent of use
> of setUp() and tearDown().
> * There are @BeforeClass and @AfterClass annotation that are supposed
> to substitute the use of TestSuite. In fact, this method is called
> once per class. You can see on it as your TestSuite was just a single
> class. AFAIK, there is no build in support for TestSuite that is not
> so trivial.
>
> I had similar requirement, namely I wanted to ensure that JDK 1.4+
> assert statement are on, I wanted to start JBoss microcontainer at the
> beginning and shutdown it at the end.
>
> Lets start from the simplest one.
> * I wanted to ensure that JDK 1.4+ assert statement are on.
> I created class with name BaseTestCase and all my test classes
> inherits from it. In this class I put the following method:
>
> @BeforeClass
> public static void oneTimeSetup(){
> // do your one time set-up here!
> log.info("BaseTestCase.oneTimeSetup()");
> log.debug("Going to check whether asserts are enabled");
> boolean assertsEnabled = false;
> assert (assertsEnabled = true); // Intentional side effect!!!
> if (!assertsEnabled) {
> throw new AssertionError("Asserts must be enabled!");
> }
> }
> }
>
> Because all my test classes inherit from BaseTestCase and has their
> base class BaseTestCase, it is guaranteed that first thing that will
> be run (beside JUnit itself) is oneTimeSetup() method.
>
> * I wanted to start JBoss microcontainer at the beginning and shutdown
> it at the end.
>
> Every test class inherit from some base test class, say
> BaseHelloWorldEJBTest (that eventually inherit from BaseTestCase). In
> BaseHelloWorldEJBTest there is method
>
> @BeforeClass
> public static void setUpClass() throws Exception ;
>
> This method basically does the following:
>
> final Bootstrap jbossInstance = Bootstrap.getInstance();
> if (!jbossInstance.isStarted()) {
> log.debug("starting JBoss microcontainer");
> jbossInstance.bootstrap();
> log.debug("JBoss microcontainer is up");
>
> log.debug("Going to deploy scanClasspath");
> deploy(scanClasspath);
> log.debug("End of deploy scanClasspath");
> ... <HERE there is more code>
> }
>
> Until now everything is pretty straight-forward. But how can I stop
> JBoss when I finished testing? I don't want to stop it after every
> class, but after whole TestSuite is done. I didn't find something
> built-in, so I use ShutdownHook...
>
> In the above setUpClass() mehtod in the place that noted as "<HERE
> there is more code>" I have:
>
> log.debug("Now add hook to shutdown server");
> Runtime.getRuntime().addShutdownHook(new Thread() {
> public void run() {
> assert lRunnables!=null;
> Runnable runnable =null;
> String runnableI = null;
> try {
> for(int i=0;i<lRunnables.length;i++){
> runnable = lRunnables[i];
> runnableI = "runnable "+i;
> log.debug("Going to run "+runnableI);
> if(runnable==null){
> log.warn(runnableI+" is null, skipping");
> } else {
> try {
> runnable.run();
> log.debug(runnableI+" ends run succesfully");
> }
> catch(Throwable e){log.error(runnableI+" fails", e);}
>
> }
> }
>
> }
>
> catch(Throwable e){
> log.error("This should never happened. Some runnables Throwable
> (exception) propogates outside the loop", e);
> }
>
>
> synchronized (jbossInstance) {
> jbossInstance.shutdown();
> }
> String junitStatus = junitStatusOs.toString();
> log.debug(junitStatus);
>
> }
> });
> I will talk about junitStatusPs a bit later. What is actually is done
> here that in @BeforeClass that it is before running every class, there
> is check whether JBoss is run or no. If it is run nothing special is
> done. If it is not run, and this will be true only first time this
> check will be done, JBoss is started and ShutDownHook is added. Take a
> look on the JavaDoc on ShutDownHook, but basically, on the call on
> System.exit() JVM will be run your hook. In that shutdown hook I make
> jbossInstance.shutdown().
>
> There is one issue here. One JBoss is shutting down it write some
> thing to the log. What is happen, that I see JUnit report (90000
> tested passes, 100 fails, and 10 ignored) and after this report I see
> the output from shitting down process. This happen, because the
> shutdown hook happens after last JUnit class finished to run.
> I found some work around. In the BaseTestCase I have:
> // the logger
> private static final Log log = LogFactory.getLog(BaseTestCase.class);
> protected static final OutputStream junitStatusOs = new
> ByteArrayOutputStream(16*1024);
>
> This junitStatusPs is actually passed to as PrintStream (instead
> System.out) to the JUnit classes. Note, that I have
>
> String junitStatus = junitStatusOs.toString();
> log.debug(junitStatus);
>
> as the last row of the shutdown hook. This ensure, that I will see the
> output of the shutdown hook first, and the JUnit report will be the
> last rows in the standard output.
> And the last issue is:
>
> If I want to do some other thing I just wrap it as lRunnables and pass
> to the method mentioned above. It is looks like this:
>
> //we want to call cleanUp() method that ear application calls
> //moment before we shutting down JBoss
> Runnable runnable = new Runnable(){
> public void run() {
> try {
> cleanUp();
> }
> catch(HelloWorldException e){
> log.error("Failed to cleanUp", e);
> }
> }
> };
>
> setUpClass(System.getProperty("user.dir") + "/target/classes",
> runnable);
>
> In order to shutdown JBoss we use
> @AfterClass
> public static void tearDownClass() throws Exception {
> log.info("one time tearDownClass()");
> undeploy();
> Bootstrap.getInstance().shutdown();
> }
>
>  
>


[Non-text portions of this message have been removed]


Re: Junit 3 suite setup tear down

by Joakim Ohlrogge :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It sounds from your description that you are using JUnit 3.8.x ? In
that case it should not be too difficult to do by subclassing
TestSuite. The problem is that your test will not work if you run them
outside the suite (unless you cater for that in some clever way).

Subclassing TestSuite in principle (have not tested this but the
correct answer should be similar enough)

NetworkTestSuite extends TestSuite {
     public run(TestResult result) {
          startNetwork();
          super.run(result);
          stopNetwork();
     }
}

Then you can just use NetworkTestSuite in place of TestSuite in the
root of the tests that need a running network (or whatever it was that
you wanted like this:

public static Test suite() {

    TestSuite suite= new NetworkTestSuite();

    suite.addTest(MoneyTest.class);

    return suite;

}

I hope this helps.
/J

On Wed, Jul 9, 2008 at 8:53 AM, lars <lars.carlsson@...> wrote:

> Hi
> At my company we have alot of automated test cases and in all our
> suite we have a setup testcase and a teardown test case where we setup
> network connection that takes some time and can not be done in every TC. ,
> But i think that there should exist a suite setup and a teardown and
> not be in a setup/teardown TC.
> Are there any why of doing this or how could i best be solve this by
> extending TestSuite?
>
> Regards Lars
>
>



--
-----------------------------------------------------
Joakim Ohlrogge
Agical AB
Västerlånggatan 79, 2 tr
111 29 Stockholm, SWEDEN

Mobile: +46-708-754004
Blog: johlrogge.wordpress.com
E-mail: joakim.ohlrogge@...

Re: Junit 3 suite setup tear down

by David Beidle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Lars,

It seems like the obvious suggestion is missing from this thread (or I  
at least missed it)...

Since you're using JUnit 3, have you tried the TestSetup decorator  
available in that version to provide one-time suite setUp and tearDown  
methods. For example:


     public static Test suite(Class testClass) {
         TestSetup suite = new junit.extensions.TestSetup(new  
TestSuite(testClass)) {
             protected void setUp() {
                 // set up at the start o suite execution
             }
             protected void tearDown() {
                 // tear down at the end of suite execution
             }
         };

         return suite;
     }

I've used this technique in a variety of scenarios to perform one-time  
initialization for a suite. I've used it in test case base classes to  
provide similar behavior across derivative classes. In the example  
above, I used TestSetup as an anonymous class, but you could extend it  
or just add additional methods to provide the behavior needed.

-Dave Beidle
  The aKamali Group, Inc.


On Jul 10, 2008, at 8:37 AM, Joakim Ohlrogge wrote:

> It sounds from your description that you are using JUnit 3.8.x ? In
> that case it should not be too difficult to do by subclassing
> TestSuite. The problem is that your test will not work if you run them
> outside the suite (unless you cater for that in some clever way).
>
> Subclassing TestSuite in principle (have not tested this but the
> correct answer should be similar enough)
>
> NetworkTestSuite extends TestSuite {
> public run(TestResult result) {
> startNetwork();
> super.run(result);
> stopNetwork();
> }
> }
>
> Then you can just use NetworkTestSuite in place of TestSuite in the
> root of the tests that need a running network (or whatever it was that
> you wanted like this:
>
> public static Test suite() {
>
> TestSuite suite= new NetworkTestSuite();
>
> suite.addTest(MoneyTest.class);
>
> return suite;
>
> }
>
> I hope this helps.
> /J
>
> On Wed, Jul 9, 2008 at 8:53 AM, lars <lars.carlsson@...>  
> wrote:
> > Hi
> > At my company we have alot of automated test cases and in all our
> > suite we have a setup testcase and a teardown test case where we  
> setup
> > network connection that takes some time and can not be done in  
> every TC. ,
> > But i think that there should exist a suite setup and a teardown and
> > not be in a setup/teardown TC.
> > Are there any why of doing this or how could i best be solve this by
> > extending TestSuite?
> >
> > Regards Lars
> >
> >
>
> --
> -----------------------------------------------------
> Joakim Ohlrogge
> Agical AB
> Västerlånggatan 79, 2 tr
> 111 29 Stockholm, SWEDEN
>
> Mobile: +46-708-754004
> Blog: johlrogge.wordpress.com
> E-mail: joakim.ohlrogge@...
>
>



[Non-text portions of this message have been removed]


Re: Junit 3 suite setup tear down

by toalexsmail :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have used the same technique with JUnit 3.8.x. In fact you can found
rudiments in my previous post - the name of methods are setUp() and
tearDown() and even the comments is "do your one time set-up here!" :-)
What I think is that the author of this thread is familiar with this
technique and used it in his previous job win JUnit 3.8.x. What he is
actually asking is how to do the same with JUnit 4.x.
I remember that I was surprised when I migrated to JUnit 4.x with the
lack of such a feature, also searching the web, didn't succeed, so I
decided to "invent the wheel". I will be be grateful if indeed
@BeforeSuite and @AfterSuite feature will be implemented.

--- In junit@..., David Beidle <dabeidle@...> wrote:

>
> Lars,
>
> It seems like the obvious suggestion is missing from this thread (or I  
> at least missed it)...
>
> Since you're using JUnit 3, have you tried the TestSetup decorator  
> available in that version to provide one-time suite setUp and tearDown  
> methods. For example:
>
>
>      public static Test suite(Class testClass) {
>          TestSetup suite = new junit.extensions.TestSetup(new  
> TestSuite(testClass)) {
>              protected void setUp() {
>                  // set up at the start o suite execution
>              }
>              protected void tearDown() {
>                  // tear down at the end of suite execution
>              }
>          };
>
>          return suite;
>      }
>
> I've used this technique in a variety of scenarios to perform one-time  
> initialization for a suite. I've used it in test case base classes to  
> provide similar behavior across derivative classes. In the example  
> above, I used TestSetup as an anonymous class, but you could extend it  
> or just add additional methods to provide the behavior needed.
>
> -Dave Beidle
>   The aKamali Group, Inc.
>
>
> On Jul 10, 2008, at 8:37 AM, Joakim Ohlrogge wrote:
>
> > It sounds from your description that you are using JUnit 3.8.x ? In
> > that case it should not be too difficult to do by subclassing
> > TestSuite. The problem is that your test will not work if you run them
> > outside the suite (unless you cater for that in some clever way).
> >
> > Subclassing TestSuite in principle (have not tested this but the
> > correct answer should be similar enough)
> >
> > NetworkTestSuite extends TestSuite {
> > public run(TestResult result) {
> > startNetwork();
> > super.run(result);
> > stopNetwork();
> > }
> > }
> >
> > Then you can just use NetworkTestSuite in place of TestSuite in the
> > root of the tests that need a running network (or whatever it was that
> > you wanted like this:
> >
> > public static Test suite() {
> >
> > TestSuite suite= new NetworkTestSuite();
> >
> > suite.addTest(MoneyTest.class);
> >
> > return suite;
> >
> > }
> >
> > I hope this helps.
> > /J
> >
> > On Wed, Jul 9, 2008 at 8:53 AM, lars <lars.carlsson@...>  
> > wrote:
> > > Hi
> > > At my company we have alot of automated test cases and in all our
> > > suite we have a setup testcase and a teardown test case where we  
> > setup
> > > network connection that takes some time and can not be done in  
> > every TC. ,
> > > But i think that there should exist a suite setup and a teardown and
> > > not be in a setup/teardown TC.
> > > Are there any why of doing this or how could i best be solve this by
> > > extending TestSuite?
> > >
> > > Regards Lars
> > >
> > >
> >
> > --
> > -----------------------------------------------------
> > Joakim Ohlrogge
> > Agical AB
> > Västerlånggatan 79, 2 tr
> > 111 29 Stockholm, SWEDEN
> >
> > Mobile: +46-708-754004
> > Blog: johlrogge.wordpress.com
> > E-mail: joakim.ohlrogge@...
> >
> >
>
>
>
> [Non-text portions of this message have been removed]
>



Re: Re: Junit 3 suite setup tear down

by David Saff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

@BeforeClass and @AfterClass on the @RunWith(Suite) class have exactly
this behavior in JUnit 4.4 and beyond.  However, I think the original
poster was using 3.x.

   David Saff

On Wed, Jul 9, 2008 at 6:45 PM, Cédric Beust ♔ <cbeust@...> wrote:

> That's a lot of work and therefore a good candidate for a new feature:
> @BeforeSuite/@AfterSuite.
>
> --
> Cédric
>
> On Wed, Jul 9, 2008 at 12:47 PM, toalexsmail <toalexsmail@...> wrote:
>
>> --- In junit@... <junit%40yahoogroups.com>, "lars"
>
>> <lars.carlsson@...> wrote:
>> >
>> > Hi
>> > At my company we have alot of automated test cases and in all our
>> > suite we have a setup testcase and a teardown test case where we setup
>> > network connection that takes some time and can not be done in every
>> TC. ,
>> > But i think that there should exist a suite setup and a teardown and
>> > not be in a setup/teardown TC.
>> > Are there any why of doing this or how could i best be solve this by
>> > extending TestSuite?
>> >
>> > Regards Lars
>> >
>> I think that there is some related thread on this topic, but never mind.
>>
>> * There are @Before and @After annotation that are equivalent of use
>> of setUp() and tearDown().
>> * There are @BeforeClass and @AfterClass annotation that are supposed
>> to substitute the use of TestSuite. In fact, this method is called
>> once per class. You can see on it as your TestSuite was just a single
>> class. AFAIK, there is no build in support for TestSuite that is not
>> so trivial.
>>
>> I had similar requirement, namely I wanted to ensure that JDK 1.4+
>> assert statement are on, I wanted to start JBoss microcontainer at the
>> beginning and shutdown it at the end.
>>
>> Lets start from the simplest one.
>> * I wanted to ensure that JDK 1.4+ assert statement are on.
>> I created class with name BaseTestCase and all my test classes
>> inherits from it. In this class I put the following method:
>>
>> @BeforeClass
>> public static void oneTimeSetup(){
>> // do your one time set-up here!
>> log.info("BaseTestCase.oneTimeSetup()");
>> log.debug("Going to check whether asserts are enabled");
>> boolean assertsEnabled = false;
>> assert (assertsEnabled = true); // Intentional side effect!!!
>> if (!assertsEnabled) {
>> throw new AssertionError("Asserts must be enabled!");
>> }
>> }
>> }
>>
>> Because all my test classes inherit from BaseTestCase and has their
>> base class BaseTestCase, it is guaranteed that first thing that will
>> be run (beside JUnit itself) is oneTimeSetup() method.
>>
>> * I wanted to start JBoss microcontainer at the beginning and shutdown
>> it at the end.
>>
>> Every test class inherit from some base test class, say
>> BaseHelloWorldEJBTest (that eventually inherit from BaseTestCase). In
>> BaseHelloWorldEJBTest there is method
>>
>> @BeforeClass
>> public static void setUpClass() throws Exception ;
>>
>> This method basically does the following:
>>
>> final Bootstrap jbossInstance = Bootstrap.getInstance();
>> if (!jbossInstance.isStarted()) {
>> log.debug("starting JBoss microcontainer");
>> jbossInstance.bootstrap();
>> log.debug("JBoss microcontainer is up");
>>
>> log.debug("Going to deploy scanClasspath");
>> deploy(scanClasspath);
>> log.debug("End of deploy scanClasspath");
>> ... <HERE there is more code>
>> }
>>
>> Until now everything is pretty straight-forward. But how can I stop
>> JBoss when I finished testing? I don't want to stop it after every
>> class, but after whole TestSuite is done. I didn't find something
>> built-in, so I use ShutdownHook...
>>
>> In the above setUpClass() mehtod in the place that noted as "<HERE
>> there is more code>" I have:
>>
>> log.debug("Now add hook to shutdown server");
>> Runtime.getRuntime().addShutdownHook(new Thread() {
>> public void run() {
>> assert lRunnables!=null;
>> Runnable runnable =null;
>> String runnableI = null;
>> try {
>> for(int i=0;i<lRunnables.length;i++){
>> runnable = lRunnables[i];
>> runnableI = "runnable "+i;
>> log.debug("Going to run "+runnableI);
>> if(runnable==null){
>> log.warn(runnableI+" is null, skipping");
>> } else {
>> try {
>> runnable.run();
>> log.debug(runnableI+" ends run succesfully");
>> }
>> catch(Throwable e){log.error(runnableI+" fails", e);}
>>
>> }
>> }
>>
>> }
>>
>> catch(Throwable e){
>> log.error("This should never happened. Some runnables Throwable
>> (exception) propogates outside the loop", e);
>> }
>>
>>
>> synchronized (jbossInstance) {
>> jbossInstance.shutdown();
>> }
>> String junitStatus = junitStatusOs.toString();
>> log.debug(junitStatus);
>>
>> }
>> });
>> I will talk about junitStatusPs a bit later. What is actually is done
>> here that in @BeforeClass that it is before running every class, there
>> is check whether JBoss is run or no. If it is run nothing special is
>> done. If it is not run, and this will be true only first time this
>> check will be done, JBoss is started and ShutDownHook is added. Take a
>> look on the JavaDoc on ShutDownHook, but basically, on the call on
>> System.exit() JVM will be run your hook. In that shutdown hook I make
>> jbossInstance.shutdown().
>>
>> There is one issue here. One JBoss is shutting down it write some
>> thing to the log. What is happen, that I see JUnit report (90000
>> tested passes, 100 fails, and 10 ignored) and after this report I see
>> the output from shitting down process. This happen, because the
>> shutdown hook happens after last JUnit class finished to run.
>> I found some work around. In the BaseTestCase I have:
>> // the logger
>> private static final Log log = LogFactory.getLog(BaseTestCase.class);
>> protected static final OutputStream junitStatusOs = new
>> ByteArrayOutputStream(16*1024);
>>
>> This junitStatusPs is actually passed to as PrintStream (instead
>> System.out) to the JUnit classes. Note, that I have
>>
>> String junitStatus = junitStatusOs.toString();
>> log.debug(junitStatus);
>>
>> as the last row of the shutdown hook. This ensure, that I will see the
>> output of the shutdown hook first, and the JUnit report will be the
>> last rows in the standard output.
>> And the last issue is:
>>
>> If I want to do some other thing I just wrap it as lRunnables and pass
>> to the method mentioned above. It is looks like this:
>>
>> //we want to call cleanUp() method that ear application calls
>> //moment before we shutting down JBoss
>> Runnable runnable = new Runnable(){
>> public void run() {
>> try {
>> cleanUp();
>> }
>> catch(HelloWorldException e){
>> log.error("Failed to cleanUp", e);
>> }
>> }
>> };
>>
>> setUpClass(System.getProperty("user.dir") + "/target/classes",
>> runnable);
>>
>> In order to shutdown JBoss we use
>> @AfterClass
>> public static void tearDownClass() throws Exception {
>> log.info("one time tearDownClass()");
>> undeploy();
>> Bootstrap.getInstance().shutdown();
>> }
>>
>>
>>
>
> [Non-text portions of this message have been removed]
>
>

Re: Junit 3 suite setup tear down

by toalexsmail :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

No, you're wrong. Suppose you want to run suite with 2 different
classes and you want to make one time setup only once. That is you
want to do your-one-time-setup, run class A, run class B, and to do
your-one-time-teardown. How can you do it?
Morevoer, suppose, you want to run only some specific methods from
those classes, not all the methods, how can you do it? In JUnit 3.8.x
there was a way to do it.

--- In junit@..., "David Saff" <david@...> wrote:

>
> @BeforeClass and @AfterClass on the @RunWith(Suite) class have exactly
> this behavior in JUnit 4.4 and beyond.  However, I think the original
> poster was using 3.x.
>
>    David Saff
>
> On Wed, Jul 9, 2008 at 6:45 PM, Cédric Beust â™" <cbeust@...> wrote:
> > That's a lot of work and therefore a good candidate for a new feature:
> > @BeforeSuite/@AfterSuite.
> >
> > --
> > Cédric
> >
> > On Wed, Jul 9, 2008 at 12:47 PM, toalexsmail <toalexsmail@...> wrote:
> >
> >> --- In junit@... <junit%40yahoogroups.com>, "lars"
> >
> >> <lars.carlsson@> wrote:
> >> >
> >> > Hi
> >> > At my company we have alot of automated test cases and in all our
> >> > suite we have a setup testcase and a teardown test case where
we setup
> >> > network connection that takes some time and can not be done in
every
> >> TC. ,
> >> > But i think that there should exist a suite setup and a
teardown and
> >> > not be in a setup/teardown TC.
> >> > Are there any why of doing this or how could i best be solve
this by
> >> > extending TestSuite?
> >> >
> >> > Regards Lars
> >> >
> >> I think that there is some related thread on this topic, but
never mind.

> >>
> >> * There are @Before and @After annotation that are equivalent of use
> >> of setUp() and tearDown().
> >> * There are @BeforeClass and @AfterClass annotation that are supposed
> >> to substitute the use of TestSuite. In fact, this method is called
> >> once per class. You can see on it as your TestSuite was just a single
> >> class. AFAIK, there is no build in support for TestSuite that is not
> >> so trivial.
> >>
> >> I had similar requirement, namely I wanted to ensure that JDK 1.4+
> >> assert statement are on, I wanted to start JBoss microcontainer
at the

> >> beginning and shutdown it at the end.
> >>
> >> Lets start from the simplest one.
> >> * I wanted to ensure that JDK 1.4+ assert statement are on.
> >> I created class with name BaseTestCase and all my test classes
> >> inherits from it. In this class I put the following method:
> >>
> >> @BeforeClass
> >> public static void oneTimeSetup(){
> >> // do your one time set-up here!
> >> log.info("BaseTestCase.oneTimeSetup()");
> >> log.debug("Going to check whether asserts are enabled");
> >> boolean assertsEnabled = false;
> >> assert (assertsEnabled = true); // Intentional side effect!!!
> >> if (!assertsEnabled) {
> >> throw new AssertionError("Asserts must be enabled!");
> >> }
> >> }
> >> }
> >>
> >> Because all my test classes inherit from BaseTestCase and has their
> >> base class BaseTestCase, it is guaranteed that first thing that will
> >> be run (beside JUnit itself) is oneTimeSetup() method.
> >>
> >> * I wanted to start JBoss microcontainer at the beginning and
shutdown

> >> it at the end.
> >>
> >> Every test class inherit from some base test class, say
> >> BaseHelloWorldEJBTest (that eventually inherit from BaseTestCase). In
> >> BaseHelloWorldEJBTest there is method
> >>
> >> @BeforeClass
> >> public static void setUpClass() throws Exception ;
> >>
> >> This method basically does the following:
> >>
> >> final Bootstrap jbossInstance = Bootstrap.getInstance();
> >> if (!jbossInstance.isStarted()) {
> >> log.debug("starting JBoss microcontainer");
> >> jbossInstance.bootstrap();
> >> log.debug("JBoss microcontainer is up");
> >>
> >> log.debug("Going to deploy scanClasspath");
> >> deploy(scanClasspath);
> >> log.debug("End of deploy scanClasspath");
> >> ... <HERE there is more code>
> >> }
> >>
> >> Until now everything is pretty straight-forward. But how can I stop
> >> JBoss when I finished testing? I don't want to stop it after every
> >> class, but after whole TestSuite is done. I didn't find something
> >> built-in, so I use ShutdownHook...
> >>
> >> In the above setUpClass() mehtod in the place that noted as "<HERE
> >> there is more code>" I have:
> >>
> >> log.debug("Now add hook to shutdown server");
> >> Runtime.getRuntime().addShutdownHook(new Thread() {
> >> public void run() {
> >> assert lRunnables!=null;
> >> Runnable runnable =null;
> >> String runnableI = null;
> >> try {
> >> for(int i=0;i<lRunnables.length;i++){
> >> runnable = lRunnables[i];
> >> runnableI = "runnable "+i;
> >> log.debug("Going to run "+runnableI);
> >> if(runnable==null){
> >> log.warn(runnableI+" is null, skipping");
> >> } else {
> >> try {
> >> runnable.run();
> >> log.debug(runnableI+" ends run succesfully");
> >> }
> >> catch(Throwable e){log.error(runnableI+" fails", e);}
> >>
> >> }
> >> }
> >>
> >> }
> >>
> >> catch(Throwable e){
> >> log.error("This should never happened. Some runnables Throwable
> >> (exception) propogates outside the loop", e);
> >> }
> >>
> >>
> >> synchronized (jbossInstance) {
> >> jbossInstance.shutdown();
> >> }
> >> String junitStatus = junitStatusOs.toString();
> >> log.debug(junitStatus);
> >>
> >> }
> >> });
> >> I will talk about junitStatusPs a bit later. What is actually is done
> >> here that in @BeforeClass that it is before running every class,
there
> >> is check whether JBoss is run or no. If it is run nothing special is
> >> done. If it is not run, and this will be true only first time this
> >> check will be done, JBoss is started and ShutDownHook is added.
Take a

> >> look on the JavaDoc on ShutDownHook, but basically, on the call on
> >> System.exit() JVM will be run your hook. In that shutdown hook I make
> >> jbossInstance.shutdown().
> >>
> >> There is one issue here. One JBoss is shutting down it write some
> >> thing to the log. What is happen, that I see JUnit report (90000
> >> tested passes, 100 fails, and 10 ignored) and after this report I see
> >> the output from shitting down process. This happen, because the
> >> shutdown hook happens after last JUnit class finished to run.
> >> I found some work around. In the BaseTestCase I have:
> >> // the logger
> >> private static final Log log = LogFactory.getLog(BaseTestCase.class);
> >> protected static final OutputStream junitStatusOs = new
> >> ByteArrayOutputStream(16*1024);
> >>
> >> This junitStatusPs is actually passed to as PrintStream (instead
> >> System.out) to the JUnit classes. Note, that I have
> >>
> >> String junitStatus = junitStatusOs.toString();
> >> log.debug(junitStatus);
> >>
> >> as the last row of the shutdown hook. This ensure, that I will
see the
> >> output of the shutdown hook first, and the JUnit report will be the
> >> last rows in the standard output.
> >> And the last issue is:
> >>
> >> If I want to do some other thing I just wrap it as lRunnables and
pass

> >> to the method mentioned above. It is looks like this:
> >>
> >> //we want to call cleanUp() method that ear application calls
> >> //moment before we shutting down JBoss
> >> Runnable runnable = new Runnable(){
> >> public void run() {
> >> try {
> >> cleanUp();
> >> }
> >> catch(HelloWorldException e){
> >> log.error("Failed to cleanUp", e);
> >> }
> >> }
> >> };
> >>
> >> setUpClass(System.getProperty("user.dir") + "/target/classes",
> >> runnable);
> >>
> >> In order to shutdown JBoss we use
> >> @AfterClass
> >> public static void tearDownClass() throws Exception {
> >> log.info("one time tearDownClass()");
> >> undeploy();
> >> Bootstrap.getInstance().shutdown();
> >> }
> >>
> >>
> >>
> >
> > [Non-text portions of this message have been removed]
> >
> >
>



Re: Re: Junit 3 suite setup tear down

by David Saff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 30, 2008 at 4:58 PM, toalexsmail <toalexsmail@...> wrote:
> No, you're wrong.

Good morning.

> Suppose you want to run suite with 2 different
> classes and you want to make one time setup only once. That is you
> want to do your-one-time-setup, run class A, run class B, and to do
> your-one-time-teardown. How can you do it?

@RunWith(Suite.class)
@SuiteClasses(A.class, B.class)
public class MySuite {
  @BeforeClass public static void doYourOneTimeSetup() {}
  @AfterClass public static void doYourOneTimeTeardown() {}
}

> Morevoer, suppose, you want to run only some specific methods from
> those classes, not all the methods, how can you do it

This isn't probably easy enough yet, but still possible.  Are you
hoping to specify the methods in a Java file, on the command line, or
some other way?

> In JUnit 3.8.x
> there was a way to do it.

What was your favorite?

   David Saff

>
> --- In junit@..., "David Saff" <david@...> wrote:
>>
>> @BeforeClass and @AfterClass on the @RunWith(Suite) class have exactly
>> this behavior in JUnit 4.4 and beyond. However, I think the original
>> poster was using 3.x.
>>
>> David Saff

Re: Junit 3 suite setup tear down

by toalexsmail :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In junit@..., "David Saff" <david@...> wrote:
>
> On Wed, Jul 30, 2008 at 4:58 PM, toalexsmail <toalexsmail@...> wrote:
> > No, you're wrong.
>
> Good morning.
It is actually evening now.

> > Suppose you want to run suite with 2 different
> > classes and you want to make one time setup only once. That is you
> > want to do your-one-time-setup, run class A, run class B, and to do
> > your-one-time-teardown. How can you do it?
>
> @RunWith(Suite.class)
> @SuiteClasses(A.class, B.class)
> public class MySuite {
>   @BeforeClass public static void doYourOneTimeSetup() {}
>   @AfterClass public static void doYourOneTimeTeardown() {}
> }
First of all, I happy to know about this way of testing. Does it
available in JUnit 4.4? (I don't have the code to look on it right now)
Actually it is not very convenient method to do it. For example, I
want to run JBoss microcontainer in doYourOneTimeSetup(), so I want to
do it for ANY suite that I will run and they very. I want to test some
aspect of the system, and afterwards, another aspect and so on. This
will force me to change this class every time. See also next point.

> > Morevoer, suppose, you want to run only some specific methods from
> > those classes, not all the methods, how can you do it
>
> This isn't probably easy enough yet, but still possible.  Are you
> hoping to specify the methods in a Java file, on the command line, or
> some other way?
>
> > In JUnit 3.8.x
> > there was a way to do it.
>
> What was your favorite?

public static Test suite() {

TestSuite suite= new TestSuite();

suite.addTest(MoneyTest.class, "fooName");

return suite;
}

>
> >
> > --- In junit@..., "David Saff" <david@> wrote:
> >>
> >> @BeforeClass and @AfterClass on the @RunWith(Suite) class have
exactly
> >> this behavior in JUnit 4.4 and beyond. However, I think the original
> >> poster was using 3.x.
> >>
> >> David Saff
>



Re: Re: Junit 3 suite setup tear down

by David Saff :: Rate this Message: