• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junitparams.internal;
2 
3 import java.lang.reflect.Method;
4 import junitparams.JUnitParamsRunner;
5 import org.junit.internal.AssumptionViolatedException;
6 import org.junit.internal.runners.model.EachTestNotifier;
7 import org.junit.runner.Description;
8 import org.junit.runner.notification.RunNotifier;
9 import org.junit.runners.model.FrameworkMethod;
10 import org.junit.runners.model.Statement;
11 
12 /**
13  * Base for {@link FrameworkMethod} classes that provide a {@link Statement} for invoking.
14  */
15 public abstract class InvokableFrameworkMethod extends DescribableFrameworkMethod {
16 
17     private final Description description;
18 
InvokableFrameworkMethod(Method method, Description description)19     InvokableFrameworkMethod(Method method, Description description) {
20         super(method);
21         this.description = description;
22     }
23 
24     @Override
getDescription()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      */
getInvokeStatement(Object test)41     public abstract Statement getInvokeStatement(Object test);
42 
runMethodInvoker(RunNotifier notifier, Statement methodInvoker, Description methodWithParams)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 
run(MethodBlockSupplier supplier, RunNotifier notifier)58     public abstract void run(MethodBlockSupplier supplier, RunNotifier notifier);
59 }
60