How to use method of org.jmock.integration.junit4.JUnitRuleMockery class

Best Jmock-library code snippet using org.jmock.integration.junit4.JUnitRuleMockery.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one
3 *  or more contributor license agreements.  See the NOTICE file
4 *  distributed with this work for additional information
5 *  regarding copyright ownership.  The ASF licenses this file
6 *  to you under the Apache License, Version 2.0 (the
7 *  "License"); you may not use this file except in compliance
8 *  with the License.  You may obtain a copy of the License at
9 *
10 *        http://www.apache.org/licenses/LICENSE-2.0
11 *
12 *  Unless required by applicable law or agreed to in writing,
13 *  software distributed under the License is distributed on an
14 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 *  KIND, either express or implied.  See the License for the
16 *  specific language governing permissions and limitations
17 *  under the License.
18 */
19/**
20Copyright (c) 2000-2007, jMock.org
21All rights reserved.
22
23Redistribution and use in source and binary forms, with or without
24modification, are permitted provided that the following conditions are met:
25
26Redistributions of source code must retain the above copyright notice, this list of
27conditions and the following disclaimer. Redistributions in binary form must reproduce
28the above copyright notice, this list of conditions and the following disclaimer in
29the documentation and/or other materials provided with the distribution.
30
31Neither the name of jMock nor the names of its contributors may be used to endorse
32or promote products derived from this software without specific prior written
33permission.
34
35THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
36EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
38SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
40TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
43WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
44DAMAGE.
45 */
46package uk.co.objectconnexions.expressiveobjects.core.unittestsupport.jmock.integration.junit4;
47
48import static org.junit.Assert.fail;
49
50import java.lang.reflect.Field;
51import java.util.List;
52
53import uk.co.objectconnexions.expressiveobjects.core.unittestsupport.jmock.auto.internal.AllDeclaredFields;
54import uk.co.objectconnexions.expressiveobjects.core.unittestsupport.jmock.auto.internal.Mockomatic;
55import org.jmock.integration.junit4.JUnit4Mockery;
56import org.junit.rules.MethodRule;
57import org.junit.runners.model.FrameworkMethod;
58import org.junit.runners.model.Statement;
59
60/**
61 * A <code>JUnitRuleMockery</code> is a JUnit Rule that manages JMock
62 * expectations and allowances, and asserts that expectations have been met
63 * after each test has finished. To use it, add a field to the test class (note
64 * that you don't have to specify <code>@RunWith(JMock.class)</code> any more).
65 * For example,
66 * 
67 * <pre>
68 * public class ATestWithSatisfiedExpectations {
69 *     &#064;Rule
70 *     public final JUnitRuleMockery context = new JUnitRuleMockery();
71 *     private final Runnable runnable = context.mock(Runnable.class);
72 * 
73 *     &#064;Test
74 *     public void doesSatisfyExpectations() {
75 *         context.checking(new Expectations() {
76 *             {
77 *                 oneOf(runnable).run();
78 *             }
79 *         });
80 * 
81 *         runnable.run();
82 *     }
83 * }
84 * </pre>
85 * 
86 * Note that the Rule field must be declared public and as a
87 * <code>JUnitRuleMockery</code> (not a <code>Mockery</code>) for JUnit to
88 * recognise it, as it's checked statically.
89 * 
90 * @author smgf
91 */
92public class JUnitRuleMockery extends JUnit4Mockery implements MethodRule {
93    private final Mockomatic mockomatic = new Mockomatic(this);
94
95    @Override
96    public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
97        return new Statement() {
98            @Override
99            public void evaluate() throws Throwable {
100                prepare(target);
101                base.evaluate();
102                assertIsSatisfied();
103            }
104
105            private void prepare(final Object target) {
106                final List<Field> allFields = AllDeclaredFields.in(target.getClass());
107                assertOnlyOneJMockContextIn(allFields);
108                fillInAutoMocks(target, allFields);
109            }
110
111            private void assertOnlyOneJMockContextIn(final List<Field> allFields) {
112                Field contextField = null;
113                for (final Field field : allFields) {
114                    if (JUnitRuleMockery.class.isAssignableFrom(field.getType())) {
115                        if (null != contextField) {
116                            fail("Test class should only have one JUnitRuleMockery field, found " + contextField.getName() + " and " + field.getName());
117                        }
118                        contextField = field;
119                    }
120                }
121            }
122
123            private void fillInAutoMocks(final Object target, final List<Field> allFields) {
124                mockomatic.fillIn(target, allFields);
125            }
126        };
127    }
128}
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)