How to use method of org.jmock.test.unit.internal.MockObjectTestCase class

Best Jmock-library code snippet using org.jmock.test.unit.internal.MockObjectTestCase.

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.internal;
2
3import org.jmock.Mockery;
4import org.jmock.Sequence;
5import org.jmock.States;
6import org.jmock.api.Imposteriser;
7import org.jmock.api.MockObjectNamingScheme;
8import org.jmock.auto.internal.Mockomatic;
9import org.jmock.internal.ExpectationBuilder;
10import org.jmock.lib.AssertionErrorTranslator;
11
12/**
13 * A {@link junit.framework.TestCase} that supports testing with mock objects.
14 * It wraps a {@link org.jmock.Mockery} and automatically asserts that
15 * all expectations have been met at the end of the test before 
16 * {@link junit.framework.TestCase#tearDown()} is called.
17 * 
18 * @author npryce
19 */
20public abstract class MockObjectTestCase extends VerifyingTestCase {
21    private final Mockery context = new Mockery();
22    
23    public MockObjectTestCase() {
24        super();
25        initialise();
26    }
27    
28    public MockObjectTestCase(String name) {
29        super(name);
30        initialise();
31    }
32    
33    private void initialise() {
34        context.setExpectationErrorTranslator(AssertionErrorTranslator.INSTANCE);
35        
36        addVerifier(new Runnable() {
37            public void run() { 
38                context.assertIsSatisfied(); 
39            }
40        });
41        
42        Mockomatic mockomatic = new Mockomatic(context);
43        mockomatic.fillIn(this);
44    }
45
46    public Mockery context() {
47        return context;
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        context.setDefaultResultForType(type, result);
62    }
63    
64    /**
65     * Changes the imposteriser used to adapt mock objects to the mocked type.
66     * 
67     * The default imposteriser allows a test to mock interfaces but not
68     * classes, so you'll have to plug a different imposteriser into the
69     * Mockery if you want to mock classes.
70     */
71    public void setImposteriser(Imposteriser imposteriser) {
72        context.setImposteriser(imposteriser);
73    }
74    
75    /**
76     * Changes the naming scheme used to generate names for mock objects that 
77     * have not been explicitly named in the test.
78     * 
79     * The default naming scheme names mock objects by lower-casing the first
80     * letter of the class name, so a mock object of type BananaSplit will be
81     * called "bananaSplit" if it is not explicitly named in the test.
82     */
83    public void setNamingScheme(MockObjectNamingScheme namingScheme) {
84        context.setNamingScheme(namingScheme);
85    }
86    
87    /**
88     * Specify expectations upon the mock objects in the test.
89     * 
90     */
91    public void checking(ExpectationBuilder expectations) {
92        context.checking(expectations);
93    }
94    
95    /**
96     * Create a mock object of type T with an explicit name.
97     * 
98     * @param typeToMock
99     *  The type to be mocked
100     * @param name
101     *  The name of the new mock object that is used to identify the mock object
102     *  in error messages
103     * @return
104     *  A new mock object of type
105     */
106    public <T> T mock(Class<T> typeToMock, String name) {
107        return context.mock(typeToMock, name);
108    }
109
110    /**
111     * Create a mock object of type T with a name derived from its type.
112     * 
113     * @param typeToMock
114     *  The type to be mocked
115     * @return
116     *  A new mock object of type
117     */
118    public <T> T mock(Class<T> typeToMock) {
119        return context.mock(typeToMock);
120    }
121
122    /** 
123     * Returns a new sequence that is used to constrain the order in which 
124     * expectations can occur.
125     * 
126     * @param name
127     *     The name of the sequence.
128     * @return
129     *     A new sequence with the given name.
130     */
131    public Sequence sequence(String name) {
132        return context.sequence(name);
133    }
134    
135    /** 
136     * Returns a new state machine that is used to constrain the order in which 
137     * expectations can occur.
138     * 
139     * @param name
140     *     The name of the state machine.
141     * @return
142     *     A new state machine with the given name.
143     */
144    public States states(String name) {
145        return context.states(name);
146    }
147}
148
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)