4

Quick JUnit question. I'm running some unit tests that involve starting up the GUI and doing a load of stuff.

I would like to see the results after the test to confirm it visually. However, it gets to the end of the code and exits, as it should. If I want to override this, I put a breakpoint on the last line of the test. This is pretty awkward though.

Is there some option to stop it from exiting?

bcoughlan
  • 25,987
  • 18
  • 90
  • 141

4 Answers4

3

Due to the fact you require a GUI and user interaction during the execution of the test, this is a "functional" test rather than a "unit" test.

You could write the results to a file at the end, this would have the added benefit that you could assert that the output is correct/present programatically at the end. If you really want to keep the test running, then you could insert an infinite loop at the end of your test:

JUnit 3:

public void tearDown() {
    while (true) { Thread.sleep(2000); };
}

JUnit 4:

@After
public void tearDown() {
    while (true) { Thread.sleep(2000); };
}

This will keep the JUnit thread running, but you will need to ensure that your GUI events are being handled in another thread.

seanhodges
  • 17,426
  • 15
  • 71
  • 93
1

In Eclipse: Run configurations... > Test > Keep JUnit running...

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • This was the answer to the title of your question. What is the nature of the GUI? For Swing there's http://www.jdemo.de/ which might be helpful. – lexicore Mar 29 '10 at 20:25
0

One possibility is that your JUnit test is executing a tearDown() method, perhaps in a base class, which shuts down the GUI. If so, you can override the tearDown() method in your JUnit test to prevent this behaviour. E.g.

protected void tearDown()
{
   //do nothing
}
Chris Knight
  • 24,333
  • 24
  • 88
  • 134
  • Good idea, but GUI still exits. I tried making the main class a field object just in case it was a garbage collector destroying the object. No luck there either – bcoughlan Mar 29 '10 at 20:24
0

How about using a countdownlatch which you count down.

private static CountDownLatch countdown = new CountDownLatch(1);

@AfterClass
public static void tearDownClass() throws Exception {
    countdown.await();
}

Then somewhere else in your code you count down latch on event.

countdown.countDown();

When countdown.countDown() is called then countdown.await() will continue.

Alfred
  • 60,935
  • 33
  • 147
  • 186