Extend The Java Unit Testing Framework

This is part of JavaUnitTestChallengeSolved. -- DonWells


The first thing I want to then is run the actual test in a thread of it's own. This way if it waits forever I can continue on and declare it a deadlock. I am also going to add setUp and tearDown. These two will be important to killing off extra threads that the test itself will create. I am going to change the name of the test code to runTest() from run() since that is needed to get the Thread going. This is the new definition. Test subclasses will override these three messages.

 public class Test extends Thread {
    public static String DeadlockMessage? = "Deadlock : Test timed out";
    public boolean success = false;
    public String result = "not run";

public void setUp(){}

public void runTest()throws RuntimeException {}

public void tearDown(){}

I can now go back into the unit TestingFramework and change runOneTest to this:

void runOneTest(int anIndex) {
    tests[anIndex].setResultToBeDeadlock();
    tests[anIndex].setUp();
    tests[anIndex].start();
    waitTillTestFinishesOrDeadlock(anIndex);
    tests[anIndex].stop();
    tests[anIndex].tearDown();}

private void waitTillTestFinishesOrDeadlock(int anIndex){ try { tests[anIndex].join(5000);} catch (InterruptedException exception){ return;}}

What this will do for us is start the test running and wait for it to finish. The result will be what ever was set when the thread finished. But if it does not finish in 5 seconds then we just stop it. The result will be what ever it was originally set to, which we have cleverly decided should be set to a deadlock error message.

We create and additional TestingFramework test:

 public class DeadLockTest? extends Test {

synchronized public void runTest(){ try { wait();} catch (Exception exception){};} }

This should run forever and thus should show up as a deadlock. We test our TestingFramework and it works fine.

I press the test button twice and it doesn't work fine at all. They all come out deadlocks. I think the problem is in running the test threads a second time after they have already terminated. I fix that by always getting a fresh set of tests to run. I also remove the run a single test button since I can live with out it and now it will not work unless I spend allot of time on it. This is the whole thing in one place JavaUnitTestFrameworkWithTimeout.


I was using tearDown() to send stop to all the threads that get started by a test. It turns out this is a bad idea since sending stop to a thread which is waiting will hang forever. I can see why Thread.stop() is considered depreciated. -- DonWells


EditText of this page (last edited March 4, 2002) or FindPage with title or text search