-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Description
Overview
In JUnit 4, we use a specific test runner that runs the test code on a dedicated UI thread, such as the AWT/Swing thread (or the JavaFX thread). This avoids cluttering your code with EventQueue.invokeAndWait all over the place. As far as I understood, the current extensions proposal does not allow to do such thing.
The below runner simply wraps all calls to before, after, and test methods in an EventQueue.invokeAndWait call. Tests simply have to declare @RunWith(AWTThreadRunner.class) to get this behavior.
public class AWTThreadRunner extends BlockJUnit4ClassRunner {
public AWTThreadRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected Statement classBlock(RunNotifier notifier) {
return onAWTThreadStatement(super.classBlock(notifier));
}
@Override
protected Statement methodBlock(FrameworkMethod method) {
return onAWTThreadStatement(super.methodBlock(method));
}
private static Statement onAWTThreadStatement(final Statement aStatement) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
aStatement.evaluate();
// left out exception handling code improve readability
}
});
}
};
}
}Note: the actual implementation is a bit more complex in that it does proper exception handling and refuses JUnit's time-outs as that doesn't work together with running on a dedicated thread.
Related Issues
SimY4, igorzep and lukeu