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
1package org.jmock.integration.junit4;
2
3import org.jmock.auto.internal.Mockomatic;
4import org.jmock.internal.AllDeclaredFields;
5import org.junit.rules.MethodRule;
6import org.junit.runners.model.FrameworkMethod;
7import org.junit.runners.model.Statement;
8
9import java.lang.reflect.Field;
10import java.util.List;
11
12import static org.junit.Assert.fail;
13
14/**
15 * A <code>JUnitRuleMockery</code> is a JUnit Rule that manages JMock expectations
16 * and allowances, and asserts that expectations have been met after each test
17 * has finished. To use it, add a field to the test class (note that you don't 
18 * have to specify <code>@RunWith(JMock.class)</code> any more). For example,  
19 * 
20 * <pre>public class ATestWithSatisfiedExpectations {
21 *  @Rule public final JUnitRuleMockery context = new JUnitRuleMockery();
22 *  private final Runnable runnable = context.mock(Runnable.class);
23 *     
24 *  @Test
25 *  public void doesSatisfyExpectations() {
26 *    context.checking(new Expectations() {{
27 *      oneOf (runnable).run();
28 *    }});
29 *          
30 *    runnable.run();
31 *  }
32 *}</pre>
33 *
34 * Note that the Rule field must be declared public and as a <code>JUnitRuleMockery</code>
35 * (not a <code>Mockery</code>) for JUnit to recognise it, as it's checked statically.
36 * 
37 * @author smgf
38 */
39
40public class JUnitRuleMockery extends JUnit4Mockery implements MethodRule {
41    private final Mockomatic mockomatic = new Mockomatic(this);
42
43    @Override
44    public Statement apply(final Statement base, FrameworkMethod method, final Object target) {
45        return new Statement() {
46            @Override
47            public void evaluate() throws Throwable {
48                prepare(target);
49                base.evaluate();
50                assertIsSatisfied();
51            }
52
53            private void prepare(final Object target) {
54                List<Field> allFields = AllDeclaredFields.in(target.getClass());
55                assertOnlyOneJMockContextIn(allFields);
56                fillInAutoMocks(target, allFields);
57            }
58
59            private void assertOnlyOneJMockContextIn(List<Field> allFields) {
60                Field contextField = null;
61                for (Field field : allFields) {
62                    if (JUnitRuleMockery.class.isAssignableFrom(field.getType())) {
63                        if (null != contextField) {
64                            fail("Test class should only have one JUnitRuleMockery field, found "
65                                  + contextField.getName() + " and " + field.getName());
66                        }
67                        contextField = field;
68                    }
69                }
70            }
71
72            private void fillInAutoMocks(final Object target, List<Field> allFields) {
73                mockomatic.fillIn(target, allFields);
74            }
75        };
76    }
77}
78
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)