How to use method of org.jmock.auto.internal.Mockomatic class

Best Jmock-library code snippet using org.jmock.auto.internal.Mockomatic.

Run Jmock-library automation tests on LambdaTest cloud grid

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

copy
1package org.jmock.test.unit.auto.internal;
2
3import static org.hamcrest.Matchers.equalTo;
4import static org.hamcrest.Matchers.notNullValue;
5import static org.hamcrest.Matchers.startsWith;
6import static org.junit.Assert.assertThat;
7import junit.framework.TestCase;
8
9import org.jmock.Mockery;
10import org.jmock.Sequence;
11import org.jmock.States;
12import org.jmock.auto.Auto;
13import org.jmock.auto.Mock;
14import org.jmock.auto.internal.Mockomatic;
15import org.jmock.test.acceptance.MockedType;
16
17public class MockomaticTests extends TestCase {
18    Mockery mockery = new Mockery();
19    Mockomatic mockomatic = new Mockomatic(mockery);
20    
21    public static class ObjectWithPublicAndPrivateFields {
22        public @Mock MockedType publicMock;
23        private @Mock MockedType privateMock;
24        
25        public MockedType privateMock() { return privateMock; }  
26    }
27    
28    public void testCreatesMockObjectsNamedAfterTheField() {        
29        ObjectWithPublicAndPrivateFields example = new ObjectWithPublicAndPrivateFields();
30        
31        mockomatic.fillIn(example);
32        
33        assertThat("created public mock", 
34                   example.publicMock, notNullValue());
35        assertThat("named public mock after field", 
36                   example.publicMock.toString(), equalTo("publicMock"));
37
38        assertThat("created private mock", 
39                   example.privateMock(), notNullValue());
40        assertThat("named private mock after field", 
41                   example.privateMock().toString(), equalTo("privateMock"));
42    }
43    
44    public static class BaseClass {
45        public @Mock MockedType mockInBaseClass;
46    }
47    public static class DerivedClass extends BaseClass {
48        public @Mock MockedType mockInDerivedClass;
49    }
50    
51    public void testCreatesMockObjectsInAllClassesInInheritanceHierarchy() {
52        DerivedClass example = new DerivedClass();
53        mockomatic.fillIn(example);
54        
55        assertThat("created mock in base class", example.mockInBaseClass, notNullValue());
56        assertThat("created mock in derived class", example.mockInDerivedClass, notNullValue());
57    }
58    
59    public static class WantsStates {
60        public @Auto States stateMachine;
61    }
62    
63    public void testCreatesStateMachinesNamedAfterTheField() {
64        WantsStates example = new WantsStates();
65        mockomatic.fillIn(example);
66        
67        assertThat("created state machine", 
68                   example.stateMachine, notNullValue());
69        assertThat("named state machine after field", 
70                   example.stateMachine.toString(), startsWith("stateMachine "));
71    }
72    
73    public static class WantsSequence {
74        public @Auto Sequence aSequence;
75    }
76    
77    public void testCreatesSequencesNamedAfterTheField() {
78        WantsSequence example = new WantsSequence();
79        mockomatic.fillIn(example);
80        
81        assertThat("created sequence", 
82                   example.aSequence, notNullValue());
83        assertThat("named sequence after field", 
84                   example.aSequence.toString(), equalTo("aSequence"));
85    }
86}
87
Full Screen
copy
1/**
2Copyright (c) 2000-2007, jMock.org
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8Redistributions of source code must retain the above copyright notice, this list of
9conditions and the following disclaimer. Redistributions in binary form must reproduce
10the above copyright notice, this list of conditions and the following disclaimer in
11the documentation and/or other materials provided with the distribution.
12
13Neither the name of jMock nor the names of its contributors may be used to endorse
14or promote products derived from this software without specific prior written
15permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
25WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26DAMAGE.
27 */
28package org.jmock.integration.junit4;
29
30import static org.junit.Assert.fail;
31
32import java.lang.reflect.Field;
33import java.util.List;
34
35import org.jmock.auto.internal.AllDeclaredFields;
36import org.jmock.auto.internal.Mockomatic;
37import org.junit.rules.MethodRule;
38import org.junit.runners.model.FrameworkMethod;
39import org.junit.runners.model.Statement;
40
41/**
42 * A <code>JUnitRuleMockery</code> is a JUnit Rule that manages JMock
43 * expectations and allowances, and asserts that expectations have been met
44 * after each test has finished. To use it, add a field to the test class (note
45 * that you don't have to specify <code>@RunWith(JMock.class)</code> any more).
46 * For example,
47 * 
48 * <pre>
49 * public class ATestWithSatisfiedExpectations {
50 *     &#064;Rule
51 *     public final JUnitRuleMockery context = new JUnitRuleMockery();
52 *     private final Runnable runnable = context.mock(Runnable.class);
53 * 
54 *     &#064;Test
55 *     public void doesSatisfyExpectations() {
56 *         context.checking(new Expectations() {
57 *             {
58 *                 oneOf(runnable).run();
59 *             }
60 *         });
61 * 
62 *         runnable.run();
63 *     }
64 * }
65 * </pre>
66 * 
67 * Note that the Rule field must be declared public and as a
68 * <code>JUnitRuleMockery</code> (not a <code>Mockery</code>) for JUnit to
69 * recognise it, as it's checked statically.
70 * 
71 * @author smgf
72 */
73public class JUnitRuleMockery extends JUnit4Mockery implements MethodRule {
74    private final Mockomatic mockomatic = new Mockomatic(this);
75
76    @Override
77    public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
78        return new Statement() {
79            @Override
80            public void evaluate() throws Throwable {
81                prepare(target);
82                base.evaluate();
83                assertIsSatisfied();
84            }
85
86            private void prepare(final Object target) {
87                final List<Field> allFields = AllDeclaredFields.in(target.getClass());
88                assertOnlyOneJMockContextIn(allFields);
89                fillInAutoMocks(target, allFields);
90            }
91
92            private void assertOnlyOneJMockContextIn(final List<Field> allFields) {
93                Field contextField = null;
94                for (final Field field : allFields) {
95                    if (JUnitRuleMockery.class.isAssignableFrom(field.getType())) {
96                        if (null != contextField) {
97                            fail("Test class should only have one JUnitRuleMockery field, found " + contextField.getName() + " and " + field.getName());
98                        }
99                        contextField = field;
100                    }
101                }
102            }
103
104            private void fillInAutoMocks(final Object target, final List<Field> allFields) {
105                mockomatic.fillIn(target, allFields);
106            }
107        };
108    }
109}
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)