JUnit - stop him from reaching the finish line?

JUnit quick question. I am running some unit tests that include starting a graphical user interface and performing a load.

I would like to see the results after the test in order to confirm it visually. However, it reaches the end of the code and exits as it should. If I want to override this, I will put a breakpoint on the last line of the test. It is rather inconvenient, though.

Is there any option to stop it?

+3
source share
4 answers

Due to the fact that you need a GUI and a user interface during the execution of the test, this is a "functional" test, not a "unit" test.

You can write the results to a file at the end, this will have an additional advantage, which could be argued that the result is correct / present at the end. If you really want the test to run, you can insert an endless loop at the end of the test:

JUnit 3:

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

JUnit 4:

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

This will cause the JUnit thread to start, but you will need to make sure that your GUI events are being processed in another thread.

+2
source

In Eclipse: Run Configuration ...> Test> Hold JUnit ...

+1
source

One possibility is that your JUnit test executes the tearDown () method, possibly in a base class that disables the GUI. If so, you can override the tearDown () method in your JUnit test to prevent this behavior. For instance.

 protected void tearDown() { //do nothing } 
0
source

How about using the countdownlatch you consider.

private static CountDownLatch countdown = new CountDownLatch (1);

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

Then, somewhere else in your code, you are counting the event commit.

 countdown.countDown(); 

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

0
source

All Articles