Skip to content
Magnus Karlsson edited this page Jun 10, 2015 · 3 revisions

Case Integration Tests

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.

Modifying JUnit behavior

Three JUnit components in the library make sure that JUnit stops on the first failure

RunListener

Listens to any failure event

RunNotifier

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 Suite component

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);
    }
}

The Test Class

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

The Testsuite that uses the Test Class that will fail during initialization.

@RunWith(StopOnFirstFailureSuite.class)
@SuiteClasses({StopOnFirstFailureTest.class})
public class SimpleTestSuite {

}

Clone this wiki locally