All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testcontainers.containers.FailureDetectingExternalResource Maven / Gradle / Ivy

There is a newer version: 1.20.3
Show newest version
package org.testcontainers.containers;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.List;

/**
 * {@link TestRule} which is called before and after each test, and also is notified on success/failure.
 *
 * This mimics the behaviour of TestWatcher to some degree, but failures occurring in this rule do not
 * contribute to the overall failure count (which can otherwise cause strange negative test success
 * figures).
 */
public class FailureDetectingExternalResource implements TestRule {

    @Override
    public Statement apply(Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                List errors = new ArrayList();

                try {
                    starting(description);
                    base.evaluate();
                    succeeded(description);
                } catch (Throwable e) {
                    errors.add(e);
                    failed(e, description);
                } finally {
                    finished(description);
                }

                MultipleFailureException.assertEmpty(errors);
            }
        };
    }

    protected void starting(Description description) {}

    protected void succeeded(Description description) {}

    protected void failed(Throwable e, Description description) {}

    protected void finished(Description description) {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy