How to use method of org.jmock.internal.InvocationExpectationBuilder class

Best Jmock-library code snippet using org.jmock.internal.InvocationExpectationBuilder.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.internal;
2
3import java.lang.reflect.Method;
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.List;
7
8import org.hamcrest.Matcher;
9import org.jmock.Sequence;
10import org.jmock.api.Action;
11import org.jmock.api.Expectation;
12import org.jmock.api.Invocation;
13import org.jmock.internal.matcher.MethodNameMatcher;
14import org.jmock.internal.matcher.MockObjectMatcher;
15import org.jmock.internal.matcher.AllParametersMatcher;
16import org.jmock.syntax.MethodClause;
17import org.jmock.syntax.ParametersClause;
18import org.jmock.syntax.ReceiverClause;
19
20public class InvocationExpectationBuilder 
21    implements ExpectationCapture, 
22               ReceiverClause, MethodClause, ParametersClause
23{
24    private final InvocationExpectation expectation = new InvocationExpectation();
25    
26    private boolean isFullySpecified = false;
27    private boolean needsDefaultAction = true;
28    private List<Matcher<?>> capturedParameterMatchers = new ArrayList<Matcher<?>>();
29    
30    public Expectation toExpectation(Action defaultAction) {
31        if (needsDefaultAction) {
32            expectation.setDefaultAction(defaultAction);
33        }
34        
35        return expectation;
36    }
37    
38    public void setCardinality(Cardinality cardinality) {
39        expectation.setCardinality(cardinality);
40    }
41    
42    public void addParameterMatcher(Matcher<?> matcher) {
43        capturedParameterMatchers.add(matcher);
44    }
45    
46    public void addOrderingConstraint(OrderingConstraint constraint) {
47        expectation.addOrderingConstraint(constraint);
48    }
49    
50    public void addInSequenceOrderingConstraint(Sequence sequence) {
51        sequence.constrainAsNextInSequence(expectation);
52    }
53    
54    public void setAction(Action action) {
55        expectation.setAction(action);
56        needsDefaultAction = false;
57    }
58    
59    public void addSideEffect(SideEffect sideEffect) {
60        expectation.addSideEffect(sideEffect);
61    }
62    
63    private <T> T captureExpectedObject(T mockObject) {
64        if (!(mockObject instanceof CaptureControl)) {
65            throw new IllegalArgumentException("can only set expectations on mock objects");
66        }
67        
68        expectation.setObjectMatcher(new MockObjectMatcher(mockObject));
69        isFullySpecified = true;
70        
71        Object capturingImposter = ((CaptureControl)mockObject).captureExpectationTo(this);
72        
73        return asMockedType(mockObject, capturingImposter);
74    }
75    
76    // Damn you Java generics! Damn you to HELL!
77    @SuppressWarnings("unchecked")
78    private <T> T asMockedType(@SuppressWarnings("unused") T mockObject, 
79                               Object capturingImposter) 
80    {
81        return (T) capturingImposter;
82    }
83    
84    public void createExpectationFrom(Invocation invocation) {
85        expectation.setMethod(invocation.getInvokedMethod());
86        
87        if (capturedParameterMatchers.isEmpty()) {
88            expectation.setParametersMatcher(new AllParametersMatcher(invocation.getParametersAsArray()));
89        }
90        else {
91            checkParameterMatcherCount(invocation);
92            expectation.setParametersMatcher(new AllParametersMatcher(capturedParameterMatchers));
93        }
94    }
95    
96    private void checkParameterMatcherCount(Invocation invocation) {
97        if (capturedParameterMatchers.size() != invocation.getParameterCount()) {
98            throw new IllegalArgumentException("not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values");
99        }
100    }
101    
102    public void checkWasFullySpecified() {
103        if (!isFullySpecified) {
104            throw new IllegalStateException("expectation was not fully specified");
105        }
106    }
107
108    /* 
109     * Syntactic sugar
110     */
111    
112    public <T> T of(T mockObject) {
113        return captureExpectedObject(mockObject);
114    }
115
116    public MethodClause of(Matcher<?> objectMatcher) {
117        expectation.setObjectMatcher(objectMatcher);
118        isFullySpecified = true;
119        return this;
120    }
121
122    public ParametersClause method(Matcher<Method> methodMatcher) {
123        expectation.setMethodMatcher(methodMatcher);
124        return this;
125    }
126    
127    public ParametersClause method(String nameRegex) {
128        return method(new MethodNameMatcher(nameRegex));
129    }
130    
131    public void with(Matcher<?>... parameterMatchers) {
132        expectation.setParametersMatcher(new AllParametersMatcher(Arrays.asList(parameterMatchers)));
133    }
134    
135    public void withNoArguments() {
136        with();
137    }
138}
139
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 code on LambdaTest Cloud Grid

Execute automation tests with 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)