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
1/*
2 * Copyright 2006 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package jdave.mock;
17
18import jdave.ContainmentSupport;
19
20import org.jmock.Expectations;
21import org.jmock.Mockery;
22import org.jmock.Sequence;
23import org.jmock.States;
24import org.jmock.internal.ExpectationBuilder;
25import org.jmock.lib.legacy.ClassImposteriser;
26
27/**
28 * Note, most of these methods are copied from jmock MockObjectTestCase.
29 * We do not want to derive from junit's TestCase as MockObjectTestCase
30 * does.
31 * 
32 * @author Joni Freeman
33 */
34public class MockSupport extends ContainmentSupport {
35    private final Mockery mockery = new Mockery();
36
37    protected MockSupport() {
38        mockery.setExpectationErrorTranslator(JDaveErrorTranslator.INSTANCE); 
39        mockery.setImposteriser(ClassImposteriser.INSTANCE);
40    }
41
42    public Mockery mockery() {
43        return mockery;
44    }
45    
46    public void verifyMocks() {
47        mockery.assertIsSatisfied();
48    }
49    
50    /**
51     * Sets the result returned for the given type when no return value has been explicitly
52     * specified in the expectation.
53     *
54     * @param type
55     *    The type for which to return <var>result</var>.
56     * @param result
57     *    The value to return when a method of return type <var>type</var>
58     *    is invoked for which an explicit return value has has not been specified.
59     */
60    public void setDefaultResultForType(Class<?> type, Object result) {
61        mockery.setDefaultResultForType(type, result);
62    }
63    
64    /**
65     * Specify expectations upon the mock objects in the test.
66     */
67    public void checking(ExpectationBuilder expectations) {
68        mockery.checking(expectations);
69    }
70
71    /**
72     * Create a mock object of type T with an explicit name.
73     *
74     * @param typeToMock
75     *  The type to be mocked
76     * @param name
77     *  The name of the new mock object that is used to identify the mock object
78     *  in error messages
79     * @return
80     *  A new mock object of type
81     */
82    public <T> T mock(Class<T> typeToMock, String name) {
83        return mockery.mock(typeToMock, name);
84    }
85
86    /**
87     * Create a mock object of type T with a name derived from its type.
88     *
89     * @param typeToMock
90     *  The type to be mocked
91     * @return
92     *  A new mock object of type
93     */
94    public <T> T mock(Class<T> typeToMock) {
95        return mockery.mock(typeToMock);
96    }
97    
98    /**
99     * Create a dummy mock object of type T with a name derived from its type.
100     * All method invocations of dummy are ignored.
101     *
102     * @param typeToMock
103     *  The type to be mocked
104     * @param name
105     *  The name of the new mock object that is used to identify the mock object
106     *  in error messages
107     * @return
108     *  A new dummy mock object of type
109     */
110    public <T> T dummy(Class<T> typeToMock, String name) {
111        return ignore(mock(typeToMock, name));
112    }
113    
114    /**
115     * Create a dummy mock object of type T with a name derived from its type.
116     * All method invocations of dummy are ignored.
117     *
118     * @param typeToMock
119     *  The type to be mocked
120     * @return
121     *  A new dummy mock object of type
122     */
123    public <T> T dummy(Class<T> typeToMock) {
124        return ignore(mock(typeToMock));
125    }
126
127    private <T> T ignore(final T dummy) {
128        checking(new Expectations() {{ 
129            ignoring(dummy);
130        }});
131        return dummy;
132    }
133
134    /**
135     * Returns a new sequence that is used to constrain the order in which
136     * expectations can occur.
137     *
138     * @param name
139     *     The name of the sequence.
140     * @return
141     *     A new sequence with the given name.
142     */
143    public Sequence sequence(String name) {
144        return mockery.sequence(name);
145    }
146    
147    /**
148     * Returns a new state machine that is used to constrain the order in which
149     * expectations can occur.
150     *
151     * @param name
152     *     The name of the state machine.
153     * @return
154     *     A new state machine with the given name.
155     */
156    public States states(String name) {
157        return mockery.states(name);
158    }
159}
160
Full Screen
copy
1package name.abhijitsarkar.unittesting.jmock;
2
3import javax.servlet.RequestDispatcher;
4import javax.servlet.http.HttpServletRequest;
5import javax.servlet.http.HttpServletResponse;
6
7import name.abhijitsarkar.unittesting.jmock.RESTUtil;
8
9import org.jmock.Expectations;
10import org.jmock.Mockery;
11import org.jmock.Sequence;
12import org.jmock.integration.junit4.JMock;
13import org.jmock.integration.junit4.JUnit4Mockery;
14import org.junit.Test;
15import org.junit.runner.RunWith;
16
17@RunWith(JMock.class)
18public class RESTUtilTest {
19	private static final String TARGET = "/pages/hello.jsp";
20
21	private final Mockery context;
22	private final RequestDispatcher rd;
23	private final RESTUtil restUtil;
24	private final HttpServletRequest request;
25	private final HttpServletResponse response;
26
27	public RESTUtilTest() {
28
29		context = new JUnit4Mockery();
30
31		rd = context.mock(RequestDispatcher.class);
32		context.setDefaultResultForType(RequestDispatcher.class, rd);
33
34		request = context.mock(HttpServletRequest.class);
35		response = context.mock(HttpServletResponse.class);
36
37		restUtil = new RESTUtil();
38	}
39
40	@Test
41	public void testForward() throws Exception {
42		final String name = "Abhijit";
43
44		final Sequence fwd = context.sequence("fwd");
45
46		context.checking(new Expectations() {
47			{
48				oneOf(request).setAttribute("name", name);
49				inSequence(fwd);
50
51				oneOf(request).getRequestDispatcher(TARGET);
52				will(returnValue(rd));
53				inSequence(fwd);
54
55				oneOf(rd).forward(request, response);
56				inSequence(fwd);
57			}
58		});
59
60		restUtil.forward(request, response, TARGET, "name", name);
61	}
62}
63
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)