InvokableFrameworkMethod.java revision c27747cb9107b2e81c50baccf20a96719f96baf1
1package junitparams.internal;
2
3import java.lang.reflect.Method;
4import junitparams.JUnitParamsRunner;
5import org.junit.internal.AssumptionViolatedException;
6import org.junit.internal.runners.model.EachTestNotifier;
7import org.junit.runner.Description;
8import org.junit.runner.notification.RunNotifier;
9import org.junit.runners.model.FrameworkMethod;
10import org.junit.runners.model.Statement;
11
12/**
13 * Base for {@link FrameworkMethod} classes that provide a {@link Statement} for invoking.
14 */
15public abstract class InvokableFrameworkMethod extends DescribableFrameworkMethod {
16
17    private final Description description;
18
19    InvokableFrameworkMethod(Method method, Description description) {
20        super(method);
21        this.description = description;
22    }
23
24    @Override
25    public Description getDescription() {
26        return description;
27    }
28
29    /**
30     * Create a {@link Statement} that when called will invoke the method.
31     *
32     * <p>This is usually called from the
33     * {@link JUnitParamsRunner#methodInvoker(FrameworkMethod, Object)} method via the
34     * {@link MethodBlockSupplier} which is usually called from within the
35     * {@link #run(MethodBlockSupplier, RunNotifier)} method.
36     *
37     * @param test
38     *         the object on which the method will be invoked.
39     * @return the {@link Statement}.
40     */
41    public abstract Statement getInvokeStatement(Object test);
42
43    void runMethodInvoker(RunNotifier notifier, Statement methodInvoker,
44            Description methodWithParams) {
45        EachTestNotifier eachNotifier = new EachTestNotifier(notifier, methodWithParams);
46        eachNotifier.fireTestStarted();
47        try {
48            methodInvoker.evaluate();
49        } catch (AssumptionViolatedException e) {
50            eachNotifier.addFailedAssumption(e);
51        } catch (Throwable e) {
52            eachNotifier.addFailure(e);
53        } finally {
54            eachNotifier.fireTestFinished();
55        }
56    }
57
58    public abstract void run(MethodBlockSupplier supplier, RunNotifier notifier);
59}
60