How to use method of org.jmock.Mockery class

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

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.auscope.portal.core.test.jmock;
2
3import org.jmock.api.ThreadingPolicy;
4import org.jmock.integration.junit4.JUnitRuleMockery;
5import org.jmock.internal.SingleThreadedPolicy;
6import org.jmock.lib.concurrent.Synchroniser;
7
8/**
9 * Extension to the normal Mockery for exposing some internal fields
10 * @author Josh Vote (CSIRO)
11 *
12 */
13public class PortalRuleMockery extends JUnitRuleMockery {
14    private ThreadingPolicy portalThreadingPolicy = new SingleThreadedPolicy();
15
16    /**
17     * Changes the policy by which the Mockery copes with multiple threads.
18     *
19     *  The default policy throws an exception if the Mockery is called from different
20     *  threads.
21     *
22     *  @see Synchroniser
23     *  @param threadingPolicy how to handle different threads.
24     */
25    @Override
26    public void setThreadingPolicy(ThreadingPolicy threadingPolicy) {
27        super.setThreadingPolicy(threadingPolicy);
28        portalThreadingPolicy = threadingPolicy;
29    }
30
31    public ThreadingPolicy getThreadingPolicy() {
32        return portalThreadingPolicy;
33    }
34}
35
Full Screen
copy
1package org.jmock.internal;
2
3import java.util.ConcurrentModificationException;
4
5import org.jmock.api.Invocation;
6import org.jmock.api.InvocationDispatcher;
7import org.jmock.api.Invokable;
8import org.jmock.api.ThreadingPolicy;
9import org.jmock.lib.concurrent.Synchroniser;
10import org.jmock.lib.concurrent.UnsynchronisedInvocationDispatcher;
11
12public class SingleThreadedPolicy implements ThreadingPolicy {
13    private final Thread testThread;
14    
15    public SingleThreadedPolicy() {
16        this.testThread = Thread.currentThread();
17    }
18
19    public Invokable synchroniseAccessTo(final Invokable mockObject) {
20        return new Invokable() {
21            public Object invoke(Invocation invocation) throws Throwable {
22                checkRunningOnTestThread();
23                return mockObject.invoke(invocation);
24            }
25        };
26    }
27    
28    private void checkRunningOnTestThread() {
29        if (Thread.currentThread() != testThread) {
30            reportError("the Mockery is not thread-safe: use a " + 
31                        Synchroniser.class.getSimpleName() + " to ensure thread safety");
32        }
33    }
34    
35    private void reportError(String error) {
36        System.err.println(error);
37        throw new ConcurrentModificationException(error);
38    }
39
40    public InvocationDispatcher dispatcher() {
41        return new UnsynchronisedInvocationDispatcher();
42    }
43}
44
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)