How to use method of org.jmock.AbstractExpectations class

Best Jmock-library code snippet using org.jmock.AbstractExpectations.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock;
2
3import org.hamcrest.CoreMatchers;
4import org.hamcrest.Matcher;
5import org.hamcrest.core.*;
6import org.jmock.api.Action;
7import org.jmock.api.ExpectationCollector;
8import org.jmock.internal.*;
9import org.jmock.lib.action.*;
10import org.jmock.syntax.*;
11
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.List;
15
16/**
17 * Provides most of the syntax of jMock's "domain-specific language" API.
18 * The methods of this class don't make any sense on their own, so the
19 * Javadoc is rather sparse.  Consult the documentation on the jMock 
20 * website for information on how to use this API.
21 * 
22 * @author nat
23 *
24 */
25public abstract class AbstractExpectations implements ExpectationBuilder,
26    CardinalityClause, ArgumentConstraintPhrases, ActionClause 
27{
28    private List<InvocationExpectationBuilder> builders = new ArrayList<InvocationExpectationBuilder>();
29    private InvocationExpectationBuilder currentBuilder = null;
30
31    /**
32     * Syntactic sugar for specifying arguments that are matchers for primitive types
33     * or are untyped matchers.
34     */
35    protected final WithClause with = new WithClause() {
36        public boolean booleanIs(Matcher<?> matcher) {
37            addParameterMatcher(matcher);
38            return false;
39        }
40
41        public byte byteIs(Matcher<?> matcher) {
42            addParameterMatcher(matcher);
43            return 0;
44        }
45
46        public char charIs(Matcher<?> matcher) {
47            addParameterMatcher(matcher);
48            return 0;
49        }
50
51        public double doubleIs(Matcher<?> matcher) {
52            addParameterMatcher(matcher);
53            return 0;
54        }
55
56        public float floatIs(Matcher<?> matcher) {
57            addParameterMatcher(matcher);
58            return 0;
59        }
60
61        public int intIs(Matcher<?> matcher) {
62            addParameterMatcher(matcher);
63            return 0;
64        }
65
66        public long longIs(Matcher<?> matcher) {
67            addParameterMatcher(matcher);
68            return 0;
69        }
70
71        public short shortIs(Matcher<?> matcher) {
72            addParameterMatcher(matcher);
73            return 0;
74        }
75
76        public <T> T is(Matcher<?> matcher) {
77            addParameterMatcher(matcher);
78            return null;
79        }
80    };
81    
82    
83    private void initialiseExpectationCapture(Cardinality cardinality) {
84        checkLastExpectationWasFullySpecified();
85        
86        currentBuilder = new InvocationExpectationBuilder();
87        currentBuilder.setCardinality(cardinality);
88        builders.add(currentBuilder);
89    }
90    
91    public void buildExpectations(Action defaultAction, ExpectationCollector collector) {
92        checkLastExpectationWasFullySpecified();
93        
94        for (InvocationExpectationBuilder builder : builders) {
95            collector.add(builder.toExpectation(defaultAction));
96        }
97    }
98    
99    protected InvocationExpectationBuilder currentBuilder() {
100        if (currentBuilder == null) {
101            throw new IllegalStateException("no expectations have been specified " +
102                "(did you forget to to specify the cardinality of the first expectation?)");
103        }
104        return currentBuilder;
105    }
106    
107    private void checkLastExpectationWasFullySpecified() {
108        if (currentBuilder != null) {
109            currentBuilder.checkWasFullySpecified();
110        }
111    }
112    
113    /* 
114     * Syntactic sugar
115     */
116    
117    public ReceiverClause exactly(int count) {
118        initialiseExpectationCapture(Cardinality.exactly(count));
119        return currentBuilder;
120    }
121    
122    // Makes the entire expectation more readable than one
123    public <T> T oneOf(T mockObject) {
124        return exactly(1).of(mockObject);
125    }
126    
127    /**
128     * @deprecated Use {@link #oneOf(Object) oneOf} instead.
129     */
130    public <T> T one (T mockObject) {
131        return oneOf(mockObject);
132    }
133    
134    public ReceiverClause atLeast(int count) {
135        initialiseExpectationCapture(Cardinality.atLeast(count));
136        return currentBuilder;
137    }
138    
139    public ReceiverClause between(int minCount, int maxCount) {
140        initialiseExpectationCapture(Cardinality.between(minCount, maxCount));
141        return currentBuilder;
142    }
143    
144    public ReceiverClause atMost(int count) {
145        initialiseExpectationCapture(Cardinality.atMost(count));
146        return currentBuilder;
147    }
148    
149    public MethodClause allowing(Matcher<?> mockObjectMatcher) {
150        return atLeast(0).of(mockObjectMatcher);
151    }
152    
153    public <T> T allowing(T mockObject) {
154        return atLeast(0).of(mockObject);
155    }
156    
157    public <T> T ignoring(T mockObject) {
158        return allowing(mockObject);
159    }
160    
161    public MethodClause ignoring(Matcher<?> mockObjectMatcher) {
162        return allowing(mockObjectMatcher);
163    }
164    
165    public <T> T never(T mockObject) {
166        return exactly(0).of(mockObject);
167    }
168    
169    /*
170     * protected because the byte code injected values need to be able to call this
171     */
172    protected void addParameterMatcher(Matcher<?> matcher) {
173        currentBuilder().addParameterMatcher(matcher);
174    }
175    
176    /**
177     * For Matchers with primitive types use the <em>with</em> field, for example:
178     * <pre>with.intIs(equalTo(34));</pre>
179     * For untyped matchers use:
180     * <pre>with.&lt;T&gt;is(equalTo(anObject));</pre>
181     */
182    public <T> T with(Matcher<T> matcher) {
183        addParameterMatcher(matcher);
184        return null;
185    }
186
187    public boolean with(boolean value) {
188        addParameterMatcher(equal(value));
189        return false;
190    }
191    
192    public byte with(byte value) {
193        addParameterMatcher(equal(value));
194        return 0;
195    }
196    
197    public short with(short value) {
198        addParameterMatcher(equal(value));
199        return 0;
200    }
201    
202    public char with(char value) {
203        addParameterMatcher(equal(value));
204        return 0;
205    }
206    
207    public int with(int value) {
208        addParameterMatcher(equal(value));
209        return 0;
210    }
211    
212    public long with(long value) {
213        addParameterMatcher(equal(value));
214        return 0;
215    }
216    
217    public float with(float value) {
218        addParameterMatcher(equal(value));
219        return 0;
220    }
221    
222    public double with(double value) {
223        addParameterMatcher(equal(value));
224        return 0;
225    }
226    
227    public <T> T with(T value) {
228        addParameterMatcher(equal(value));
229        return value;
230    }
231    
232    public void will(Action action) {
233        currentBuilder().setAction(action);
234    }
235    
236    /* Common constraints
237     */
238    
239    public static <T> Matcher<T> equal(T value) {
240        return new IsEqual<T>(value);
241    }
242    
243    public static <T> Matcher<T> same(T value) {
244        return new IsSame<T>(value);
245    }
246    
247    public static <T> Matcher<T> any(Class<T> type) {
248        return CoreMatchers.any(type);
249    }
250    
251    public static <T> Matcher<T> anything() {
252        return new IsAnything<T>();
253    }
254    
255    /**
256     * @deprecated 
257     *  use {@link #aNonNull} or {@link #any} until type inference actually works in a future version of Java
258     * @param type Class to match. Do not use for native parameters.
259     * @return an IsInstanceOf matcher
260     */
261    @Deprecated
262    public static Matcher<Object> a(Class<?> type) {
263        return new IsInstanceOf(type);
264    }
265
266    /**
267     * @deprecated 
268     *  use {@link #aNonNull} or {@link #any} until type inference actually works in a future version of Java
269     * @param type Class to match. Do not use for native parameters. 
270     * @return an IsInstanceOf matcher
271     */
272    @Deprecated
273    public static Matcher<Object> an(Class<?> type) {
274        return new IsInstanceOf(type);
275    }
276    
277    public static <T> Matcher<T> aNull(Class<T> type) {
278        return new IsNull<T>();
279    }
280    
281    public static <T> Matcher<T> aNonNull(Class<T> type) {
282        return new IsNot<T>(new IsNull<T>());
283    }
284    
285    /* Common actions
286     */
287    
288    public static Action returnValue(Object result) {
289        return new ReturnValueAction(result);
290    }
291    
292    public static Action throwException(Throwable throwable) {
293        return new ThrowAction(throwable);
294    }
295    
296    public static Action returnIterator(Collection<?> collection) {
297        return new ReturnIteratorAction(collection);
298    }
299    
300    public static <T> Action returnIterator(T ... items) {
301        return new ReturnIteratorAction(items);
302    }
303    
304    public static Action returnEnumeration(Collection<?> collection) {
305        return new ReturnEnumerationAction(collection);
306    }
307    
308    public static <T> Action returnEnumeration(T ... items) {
309        return new ReturnEnumerationAction(items);
310    }
311    
312    public static Action doAll(Action...actions) {
313        return new DoAllAction(actions);
314    }
315    
316    public static Action onConsecutiveCalls(Action...actions) {
317        return new ActionSequence(actions);
318    }
319    
320    /* Naming and ordering
321     */
322    
323    public void when(StatePredicate predicate) {
324        currentBuilder().addOrderingConstraint(new InStateOrderingConstraint(predicate));
325    }
326    
327    public void then(State state) {
328        currentBuilder().addSideEffect(new ChangeStateSideEffect(state));
329    }
330    
331    public void inSequence(Sequence sequence) {
332        currentBuilder().addInSequenceOrderingConstraint(sequence);
333    }
334
335    public void inSequences(Sequence... sequences) {
336        for (Sequence sequence : sequences) {
337            inSequence(sequence);
338        }
339    }
340}
341
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
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)