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 com.lewisd.maven.lint;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Set;
7
8import org.apache.maven.project.MavenProject;
9import org.jmock.Expectations;
10import org.jmock.Mockery;
11import org.jmock.integration.junit4.JMock;
12import org.junit.Test;
13import org.junit.runner.RunWith;
14
15@RunWith(JMock.class)
16public class RuleInvokerTest {
17	
18	private final Mockery mockery = new Mockery();
19
20	@Test
21	public void shouldInvokeRuleWithRequiredModels() {
22		final MavenProject mavenProject = new MavenProject();
23		final ModelFactory modelFactory = mockery.mock(ModelFactory.class);
24		final ResultCollector resultCollector = mockery.mock(ResultCollector.class);
25		final Rule rule = mockery.mock(Rule.class);
26		
27		final Set<String> requiredModels = new HashSet<String>();
28		final Map<String,Object> models = new HashMap<String,Object>();
29		
30		RuleInvoker ruleInvoker = new RuleInvoker(mavenProject, modelFactory);
31		
32		mockery.checking(new Expectations()
33		{{
34			one(rule).getRequiredModels();
35			will(returnValue(requiredModels));
36			
37			one(modelFactory).getModels(mavenProject, requiredModels);
38			will(returnValue(models));
39			
40			one(rule).invoke(mavenProject, models, resultCollector);
41		}});
42
43		
44		ruleInvoker.invokeRule(rule, resultCollector);
45	}
46
47}
48
Full Screen
copy
1package basis.testsupport;
2
3import org.jmock.Mockery;
4import org.jmock.Sequence;
5import org.jmock.api.Invocation;
6import org.jmock.integration.junit4.JUnit4Mockery;
7import org.jmock.internal.ReturnDefaultValueAction;
8import org.junit.After;
9
10import basis.util.concurrent.Latch;
11
12
13
14public abstract class TestWithMocks extends CleanTestBase {
15
16	public class Expectations extends org.jmock.Expectations {
17		private final Sequence defaultSequence = _mockery.sequence("Default Sequence");
18		
19		/** Used to indicate methods have to be called in the correct sequence. */
20		protected void inSequence() {
21			inSequence(defaultSequence);
22		}
23
24		protected void willOpen(final Latch latch) {
25			will(new ReturnDefaultValueAction() { @Override public Object invoke(Invocation invocation) throws Throwable {
26				latch.open();
27				return super.invoke(invocation);
28			}});
29		}
30	}
31
32	protected TestWithMocks() {
33		super();
34	}
35	
36	private final Mockery _mockery = new JUnit4Mockery();
37
38	
39	@After
40	public void afterTestWithMocks() {
41		_mockery.assertIsSatisfied();
42	}
43	
44	protected <T> T mock(Class<T> type) {
45		return _mockery.mock(type);
46	}
47
48	protected <T> T mock(String name, Class<T> type) {
49		return _mockery.mock(type, name);
50	}
51
52	protected void checking(Expectations behavior) {
53		_mockery.checking(behavior);
54	}
55
56}
Full Screen
copy
1package test.com.tinyrpc.transport;
2
3import com.tinyrpc.common.exception.RpcException;
4import com.tinyrpc.registry.Constants;
5import com.tinyrpc.registry.Discovery;
6import com.tinyrpc.registry.ServiceListener;
7import com.tinyrpc.registry.URL;
8import com.tinyrpc.remoting.exchange.Request;
9import com.tinyrpc.transport.client.InvokeConfig;
10import com.tinyrpc.transport.cluster.Cluster;
11import com.tinyrpc.transport.cluster.ClusterFactory;
12import org.hamcrest.core.IsAnything;
13import org.jmock.Expectations;
14import org.jmock.Mockery;
15import org.jmock.integration.junit4.JUnit4Mockery;
16import org.jmock.lib.legacy.ClassImposteriser;
17import org.junit.Before;
18import org.junit.Test;
19import test.com.tinyrpc.mock.HelloService;
20
21public class ClusterTest {
22
23    private Mockery mockery;
24
25    private Discovery discovery;
26
27    @Before
28    public void init(){
29        mockery = new JUnit4Mockery();
30        mockery.setImposteriser(ClassImposteriser.INSTANCE);
31
32        discovery = mockery.mock(Discovery.class);
33
34
35    }
36
37    @Test
38    public void testFailfast(){
39        Expectations expectations = new Expectations();
40
41
42        expectations.allowing(discovery).subscribe(expectations.with(new IsAnything<URL>()), expectations.with(new IsAnything<ServiceListener>()));
43
44        mockery.checking(expectations);
45
46
47        InvokeConfig<HelloService> invokeConfig = new InvokeConfig<>();
48        invokeConfig.setInterCls(HelloService.class);
49        invokeConfig.setClusterType(Cluster.Type.failfast);
50
51        Cluster cluster = ClusterFactory.INSTANCE.newCluster(invokeConfig, discovery);
52
53        Request request = new Request();
54        request.setInterfaceName(HelloService.class.getName());
55        try {
56            cluster.send(request);
57        } catch (InterruptedException e) {
58            e.printStackTrace();
59        } catch (RpcException e) {
60            e.printStackTrace();
61        }
62    }
63
64
65}
66
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)