-
Notifications
You must be signed in to change notification settings - Fork 0
HowTo
Magnus Karlsson edited this page Jun 10, 2015
·
3 revisions
In the context of integration testing where we want to check a complex flow, a test case can represents a step which has to be successfully runned before executing the next test.
Three JUnit components in the library make sure that JUnit stops on the first failure
Listens to any failure event
Triggers the junit execution stop by the pleaseStop() method.
public class FailFastListener extends RunListener {
private RunNotifier delegate;
public FailFastListener(RunNotifier runNotifier) {
this.delegate = runNotifier;
}
@Override
public void testFailure(Failure failure) throws Exception {
this.delegate.pleaseStop();
}
}
Customized JUnit runner used by the framework to process execution.
public class StopOnFirstFailureSuite extends Suite {
public StopOnFirstFailureSuite(Class<?> delegateClass, Class<?>[] delegateSuiteClasses) throws InitializationError {
super(delegateClass, delegateSuiteClasses);
}
public StopOnFirstFailureSuite(Class<?> delegateClass) throws InitializationError {
super(delegateClass, delegateClass.getAnnotation(SuiteClasses.class).value());
}
@Override
public void run(RunNotifier runNotifier) {
runNotifier.addListener(new FailFastListener(runNotifier));
super.run(runNotifier);
}
}
During the initialization @BeforeClass the dummy test fails.
public class StopOnFirstFailureTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
assertFalse(false);
}
@Test(expected=AssertionError.class)
public void testStopOnFirstFailure() {
assertFalse(true);
}
}
The Testsuite that uses the Test Class that will fail during initialization.
@RunWith(StopOnFirstFailureSuite.class)
@SuiteClasses({StopOnFirstFailureTest.class})
public class SimpleTestSuite {
}