aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/junitparams/internal/InvokableFrameworkMethod.java
blob: 0ed0bdbe7a86e9d3e4e9fd25803f4c55f3d77355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package junitparams.internal;

import java.lang.reflect.Method;
import junitparams.JUnitParamsRunner;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

/**
 * Base for {@link FrameworkMethod} classes that provide a {@link Statement} for invoking.
 */
public abstract class InvokableFrameworkMethod extends DescribableFrameworkMethod {

    private final Description description;

    InvokableFrameworkMethod(Method method, Description description) {
        super(method);
        this.description = description;
    }

    @Override
    public Description getDescription() {
        return description;
    }

    /**
     * Create a {@link Statement} that when called will invoke the method.
     *
     * <p>This is usually called from the
     * {@link JUnitParamsRunner#methodInvoker(FrameworkMethod, Object)} method via the
     * {@link MethodBlockSupplier} which is usually called from within the
     * {@link #run(MethodBlockSupplier, RunNotifier)} method.
     *
     * @param test
     *         the object on which the method will be invoked.
     * @return the {@link Statement}.
     */
    public abstract Statement getInvokeStatement(Object test);

    void runMethodInvoker(RunNotifier notifier, Statement methodInvoker,
            Description methodWithParams) {
        EachTestNotifier eachNotifier = new EachTestNotifier(notifier, methodWithParams);
        eachNotifier.fireTestStarted();
        try {
            methodInvoker.evaluate();
        } catch (AssumptionViolatedException e) {
            eachNotifier.addFailedAssumption(e);
        } catch (Throwable e) {
            eachNotifier.addFailure(e);
        } finally {
            eachNotifier.fireTestFinished();
        }
    }

    public abstract void run(MethodBlockSupplier supplier, RunNotifier notifier);
}