Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Failing a test in a separate thread

diyfiesta

2009-12-20

Replies: Find Java Web Hosting

Author LoginPost Reply
Bit of an odd question, but bear with me...

I'm playing with running test methods from JUnit in their own thread (just for fun) but of course, any Assert.fail will not make its way back to the main JUnit thread and so not fail the test. I was wondering if JMock has come across the same issue, any suggestions on an approach?

The closest I've got to so far is using the very naughty Thread.stop(Throwable), something like this (although I know I can't really use this, it does illustrate the kind of thing I want);

   @Test
    public void failureInAnotherThread() throws InterruptedException {
        final Thread main = Thread.currentThread();
        Thread thread = new Thread(new Runnable() {
            public void run() {
                fail("grrr!");
            }
        });
        thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(Thread t, Throwable e) {
                main.stop(e);
            }
        });
        thread.start();
        thread.join();
    }
 
Cheers
--
Toby

©2008 gg3721.com - Jax Systems, LLC, U.S.A.