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

com.crosstreelabs.junited.core.WrappingRule Maven / Gradle / Ivy

The newest version!
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.crosstreelabs.junited.core;

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

/**
 * Effectively the same as {@link ExternalResource}, just with a nicer, more
 * sensible name.
 */
public class WrappingRule implements TestRule {
    
    @Override
    public Statement apply(final Statement base,
            final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                before(base, description);
                try {
                    base.evaluate();
                } finally {
                    after(base, description);
                }
            }
        };
    }

    /**
     * Perform actions prior to test execution.
     * Override to set up your specific external resource.
     *
     * @param statement The statement being wrapped
     * @param description The description of the test being wrapped
     * @throws java.lang.Throwable If setup fails, disabling {@code after}
     */
    protected void before(final Statement statement,
            final Description description) throws Throwable {}

    /**
     * Perform actions after test execution.
     * @param statement The statement being wrapped
     * @param description The description of the test being wrapped
     */
    protected void after(final Statement statement,
            final Description description) {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy