1package junitparams.internal;
2
3import java.lang.reflect.Method;
4
5import org.junit.runner.Description;
6import org.junit.runner.notification.RunNotifier;
7import org.junit.runners.model.Statement;
8
9/**
10 * Encapsulates a {@link Throwable} that was caught during initialization so that it can be
11 * thrown during execution in order to preserve previous behavior.
12 */
13public class DeferredErrorFrameworkMethod extends InvokableFrameworkMethod {
14
15    private final Throwable throwable;
16
17    DeferredErrorFrameworkMethod(Method method, Description description,
18            Throwable throwable) {
19        super(method, description);
20        this.throwable = throwable;
21    }
22
23    @Override
24    public Statement getInvokeStatement(Object test) {
25        return new Statement() {
26            @Override
27            public void evaluate() throws Throwable {
28                throw throwable;
29            }
30        };
31    }
32
33    @Override
34    public void run(MethodBlockSupplier supplier, RunNotifier notifier) {
35        // Do not call the MethodBlockSupplier as that could introduce additional errors, simply
36        // throw the encapsulated Throwable immediately.
37        runMethodInvoker(notifier, getInvokeStatement(notifier), getDescription());
38    }
39}
40