How to use methodBlock method of org.junit.experimental.theories.Theories class

Best junit code snippet using org.junit.experimental.theories.Theories.methodBlock

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.junit.experimental.theories;
2
3import java.lang.reflect.Field;
4import java.lang.reflect.InvocationTargetException;
5import java.lang.reflect.Modifier;
6import java.util.ArrayList;
7import java.util.List;
8import org.junit.Assert;
9import org.junit.experimental.theories.PotentialAssignment.CouldNotGenerateValueException;
10import org.junit.experimental.theories.internal.Assignments;
11import org.junit.experimental.theories.internal.ParameterizedAssertionError;
12import org.junit.internal.AssumptionViolatedException;
13import org.junit.runners.BlockJUnit4ClassRunner;
14import org.junit.runners.model.FrameworkMethod;
15import org.junit.runners.model.InitializationError;
16import org.junit.runners.model.Statement;
17import org.junit.runners.model.TestClass;
18
19public class Theories extends BlockJUnit4ClassRunner {
20
21    public static class TheoryAnchor extends Statement {
22        private List<AssumptionViolatedException> fInvalidParameters;
23        private TestClass fTestClass;
24        private FrameworkMethod fTestMethod;
25        private int successes;
26
27        /* renamed from: org.junit.experimental.theories.Theories.TheoryAnchor.2 */
28        class C12512 extends Statement {
29            final /* synthetic */ Assignments val$complete;
30            final /* synthetic */ Object val$freshInstance;
31            final /* synthetic */ FrameworkMethod val$method;
32
33            C12512(Assignments assignments, FrameworkMethod frameworkMethod, Object obj) {
34                this.val$complete = assignments;
35                this.val$method = frameworkMethod;
36                this.val$freshInstance = obj;
37            }
38
39            public void evaluate() throws Throwable {
40                try {
41                    this.val$method.invokeExplosively(this.val$freshInstance, this.val$complete.getMethodArguments(TheoryAnchor.this.nullsOk()));
42                } catch (CouldNotGenerateValueException e) {
43                }
44            }
45        }
46
47        /* renamed from: org.junit.experimental.theories.Theories.TheoryAnchor.1 */
48        class C15521 extends BlockJUnit4ClassRunner {
49            final /* synthetic */ Assignments val$complete;
50
51            /* renamed from: org.junit.experimental.theories.Theories.TheoryAnchor.1.1 */
52            class C12501 extends Statement {
53                final /* synthetic */ Statement val$statement;
54
55                C12501(Statement statement) {
56                    this.val$statement = statement;
57                }
58
59                public void evaluate() throws Throwable {
60                    try {
61                        this.val$statement.evaluate();
62                        TheoryAnchor.this.handleDataPointSuccess();
63                    } catch (AssumptionViolatedException e) {
64                        TheoryAnchor.this.handleAssumptionViolation(e);
65                    } catch (Throwable e2) {
66                        TheoryAnchor.this.reportParameterizedError(e2, C15521.this.val$complete.getArgumentStrings(TheoryAnchor.this.nullsOk()));
67                    }
68                }
69            }
70
71            C15521(Class x0, Assignments assignments) {
72                this.val$complete = assignments;
73                super(x0);
74            }
75
76            protected void collectInitializationErrors(List<Throwable> list) {
77            }
78
79            public Statement methodBlock(FrameworkMethod method) {
80                return new C12501(super.methodBlock(method));
81            }
82
83            protected Statement methodInvoker(FrameworkMethod method, Object test) {
84                return TheoryAnchor.this.methodCompletesWithParameters(method, this.val$complete, test);
85            }
86
87            public Object createTest() throws Exception {
88                return getTestClass().getOnlyConstructor().newInstance(this.val$complete.getConstructorArguments(TheoryAnchor.this.nullsOk()));
89            }
90        }
91
92        public TheoryAnchor(FrameworkMethod method, TestClass testClass) {
93            this.successes = 0;
94            this.fInvalidParameters = new ArrayList();
95            this.fTestMethod = method;
96            this.fTestClass = testClass;
97        }
98
99        private TestClass getTestClass() {
100            return this.fTestClass;
101        }
102
103        public void evaluate() throws Throwable {
104            runWithAssignment(Assignments.allUnassigned(this.fTestMethod.getMethod(), getTestClass()));
105            if (this.successes == 0) {
106                Assert.fail("Never found parameters that satisfied method assumptions.  Violated assumptions: " + this.fInvalidParameters);
107            }
108        }
109
110        protected void runWithAssignment(Assignments parameterAssignment) throws Throwable {
111            if (parameterAssignment.isComplete()) {
112                runWithCompleteAssignment(parameterAssignment);
113            } else {
114                runWithIncompleteAssignment(parameterAssignment);
115            }
116        }
117
118        protected void runWithIncompleteAssignment(Assignments incomplete) throws InstantiationException, IllegalAccessException, Throwable {
119            for (PotentialAssignment source : incomplete.potentialsForNextUnassigned()) {
120                runWithAssignment(incomplete.assignNext(source));
121            }
122        }
123
124        protected void runWithCompleteAssignment(Assignments complete) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, Throwable {
125            new C15521(getTestClass().getJavaClass(), complete).methodBlock(this.fTestMethod).evaluate();
126        }
127
128        private Statement methodCompletesWithParameters(FrameworkMethod method, Assignments complete, Object freshInstance) {
129            return new C12512(complete, method, freshInstance);
130        }
131
132        protected void handleAssumptionViolation(AssumptionViolatedException e) {
133            this.fInvalidParameters.add(e);
134        }
135
136        protected void reportParameterizedError(Throwable e, Object... params) throws Throwable {
137            if (params.length == 0) {
138                throw e;
139            }
140            throw new ParameterizedAssertionError(e, this.fTestMethod.getName(), params);
141        }
142
143        private boolean nullsOk() {
144            Theory annotation = (Theory) this.fTestMethod.getMethod().getAnnotation(Theory.class);
145            if (annotation == null) {
146                return false;
147            }
148            return annotation.nullsAccepted();
149        }
150
151        protected void handleDataPointSuccess() {
152            this.successes++;
153        }
154    }
155
156    public Theories(Class<?> klass) throws InitializationError {
157        super(klass);
158    }
159
160    protected void collectInitializationErrors(List<Throwable> errors) {
161        super.collectInitializationErrors(errors);
162        validateDataPointFields(errors);
163    }
164
165    private void validateDataPointFields(List<Throwable> errors) {
166        for (Field field : getTestClass().getJavaClass().getDeclaredFields()) {
167            if (field.getAnnotation(DataPoint.class) != null) {
168                if (!Modifier.isStatic(field.getModifiers())) {
169                    errors.add(new Error("DataPoint field " + field.getName() + " must be static"));
170                }
171                if (!Modifier.isPublic(field.getModifiers())) {
172                    errors.add(new Error("DataPoint field " + field.getName() + " must be public"));
173                }
174            }
175        }
176    }
177
178    protected void validateConstructor(List<Throwable> errors) {
179        validateOnlyOneConstructor(errors);
180    }
181
182    protected void validateTestMethods(List<Throwable> errors) {
183        for (FrameworkMethod each : computeTestMethods()) {
184            if (each.getAnnotation(Theory.class) != null) {
185                each.validatePublicVoid(false, errors);
186            } else {
187                each.validatePublicVoidNoArg(false, errors);
188            }
189        }
190    }
191
192    protected List<FrameworkMethod> computeTestMethods() {
193        List<FrameworkMethod> testMethods = super.computeTestMethods();
194        List<FrameworkMethod> theoryMethods = getTestClass().getAnnotatedMethods(Theory.class);
195        testMethods.removeAll(theoryMethods);
196        testMethods.addAll(theoryMethods);
197        return testMethods;
198    }
199
200    public Statement methodBlock(FrameworkMethod method) {
201        return new TheoryAnchor(method, getTestClass());
202    }
203}
204
Full Screen
copy
1class org.junit.experimental.theories.Theories$TheoryAnchor$1 extends org.junit.runners.BlockJUnit4ClassRunner {
2  final org.junit.experimental.theories.internal.Assignments val$complete;
3  final org.junit.experimental.theories.Theories$TheoryAnchor this$0;
4  org.junit.experimental.theories.Theories$TheoryAnchor$1(org.junit.experimental.theories.Theories$TheoryAnchor, org.junit.runners.model.TestClass, org.junit.experimental.theories.internal.Assignments);
5  protected void collectInitializationErrors(java.util.List<java.lang.Throwable>);
6  public org.junit.runners.model.Statement methodBlock(org.junit.runners.model.FrameworkMethod);
7  protected org.junit.runners.model.Statement methodInvoker(org.junit.runners.model.FrameworkMethod, java.lang.Object);
8  public java.lang.Object createTest() throws java.lang.Exception;
9}
10
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger methodBlock code on LambdaTest Cloud Grid

Execute automation tests with methodBlock on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)