How to use method of org.easymock.tests2.EasyMockAnnotationsTest class

Best Easymock code snippet using org.easymock.tests2.EasyMockAnnotationsTest.

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2001-2021 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 org.easymock.tests2;
17
18import org.easymock.EasyMockSupport;
19import org.easymock.Mock;
20import org.easymock.MockType;
21import org.easymock.TestSubject;
22import org.easymock.internal.MocksControl;
23import org.easymock.tests.IMethods;
24import org.easymock.tests.IVarArgs;
25import org.junit.Before;
26import org.junit.Test;
27
28import static org.easymock.EasyMock.*;
29import static org.junit.Assert.*;
30
31/**
32 * Tests annotation-driven mocking, requiring to be executed with either
33 * EasyMockRunner or EasyMockRule.
34 *
35 * @author Henri Tremblay
36 * @author Alistair Todd
37 */
38public abstract class EasyMockAnnotationsTest extends EasyMockSupport {
39
40    @Mock
41    private IMethods standardMock;
42
43    @Mock(type = MockType.NICE)
44    private IMethods typedMock;
45
46    @Mock(name = "name1")
47    private IMethods namedMock;
48
49    @Mock(name = "name2", type = MockType.NICE)
50    private IMethods namedAndTypedMock;
51
52    @Before
53    public void setup() {
54        assertNotNull(standardMock);
55        assertNotNull(typedMock);
56        assertNotNull(namedMock);
57        assertNotNull(namedAndTypedMock);
58    }
59
60    @Test
61    public void shouldCreateMocksUsingTestClassWhenExtendsEasyMockSupport() {
62        expect(standardMock.oneArg(true)).andReturn("1");
63        expect(namedMock.oneArg(true)).andReturn("2");
64        replayAll(); // Relies on this test class having been used for createMock calls.
65        assertNull(typedMock.oneArg("0"));
66        assertNull(namedAndTypedMock.oneArg("0"));
67        assertEquals("1", standardMock.oneArg(true));
68        assertEquals("2", namedMock.oneArg(true));
69        verifyAll();
70        assertEquals("EasyMock for interface org.easymock.tests.IMethods", standardMock.toString());
71        assertEquals("name1", namedMock.toString());
72        assertEquals("EasyMock for interface org.easymock.tests.IMethods", typedMock.toString());
73        assertEquals("name2", namedAndTypedMock.toString());
74    }
75
76    private static class ToInject {
77        protected IMethods m1;
78
79        protected IMethods m2;
80
81        protected IVarArgs v;
82
83        protected String a;
84
85        protected final IVarArgs f = null;
86
87        protected static IVarArgs s;
88    }
89
90    private static class ToInjectMocksTest {
91        @Mock
92        protected IMethods m;
93
94        @Mock
95        protected IVarArgs v;
96
97        @TestSubject
98        protected ToInject toInject = new ToInject();
99    }
100
101    @Test
102    public void shouldInjectMocksWhereTypeCompatible() {
103        ToInjectMocksTest test = new ToInjectMocksTest();
104        EasyMockSupport.injectMocks(test);
105        assertSame(test.m, test.toInject.m1);
106        assertSame(test.m, test.toInject.m2);
107        assertSame(test.v, test.toInject.v);
108        assertNull(test.toInject.a);
109        assertNull(test.toInject.f);
110        assertNull(ToInject.s);
111    }
112
113    private static class ToInjectDuplicateMocksTest {
114        @Mock(name = "a")
115        protected IMethods m;
116
117        @Mock(name = "b")
118        protected IMethods v;
119
120        @TestSubject
121        protected ToInject toInject = new ToInject();
122    }
123
124    @Test
125    public void shouldErrorWhenDuplicateAssignmentPossible() {
126
127        try {
128            EasyMockSupport.injectMocks(new ToInjectDuplicateMocksTest());
129        } catch (AssertionError e) {
130            assertEquals(
131                    "At least two mocks can be assigned to 'protected org.easymock.tests.IMethods org.easymock.tests2.EasyMockAnnotationsTest$ToInject.m1': a and b",
132                    e.getMessage());
133            return;
134        }
135        fail("Expected an exception for at least two mocks can be assigned");
136    }
137
138    private static class ToInjectQualifiedMocksTest {
139        @Mock(name = "a", fieldName = "m1")
140        protected IMethods a;
141
142        @Mock(name = "b", fieldName = "m2")
143        protected IMethods b;
144
145        @TestSubject
146        protected ToInject toInject = new ToInject();
147    }
148
149    @Test
150    public void shouldInjectQualifiedMocksToNamedFields() {
151        ToInjectQualifiedMocksTest test = new ToInjectQualifiedMocksTest();
152        EasyMockSupport.injectMocks(test);
153        assertSame(test.a, test.toInject.m1);
154        assertSame(test.b, test.toInject.m2);
155        assertNull(test.toInject.v);
156    }
157
158    private static class ToInjectExtension extends ToInject {
159        // Expecting assignments in superclass
160    }
161
162    private static class ToInjectQualifiedMocksToSuperClassTest {
163
164        @Mock(name = "a", fieldName = "m1")
165        protected IMethods a;
166
167        @Mock(name = "b", fieldName = "m2")
168        protected IMethods b;
169
170        @TestSubject
171        protected ToInjectExtension toInject = new ToInjectExtension();
172    }
173
174    @Test
175    public void shouldInjectQualifiedMocksToTestSubjectSuperClass() {
176        ToInjectQualifiedMocksToSuperClassTest test = new ToInjectQualifiedMocksToSuperClassTest();
177        EasyMockSupport.injectMocks(test);
178        assertSame(test.a, test.toInject.m1);
179        assertSame(test.b, test.toInject.m2);
180        assertNull(test.toInject.v);
181    }
182
183    private static class ToInjectQualifiedMocksMultipleTestSubjectsTest {
184        @Mock(name = "a", fieldName = "m1")
185        protected IMethods a;
186
187        @Mock(name = "b", fieldName = "m2")
188        protected IMethods b;
189
190        @TestSubject
191        protected ToInject toInject = new ToInject();
192
193        @TestSubject
194        protected ToInject toInject2 = new ToInject();
195    }
196
197    @Test
198    public void shouldInjectQualifiedMocksToAllMatchingTestSubjects() {
199        ToInjectQualifiedMocksMultipleTestSubjectsTest test = new ToInjectQualifiedMocksMultipleTestSubjectsTest();
200        EasyMockSupport.injectMocks(test);
201        assertSame(test.a, test.toInject.m1);
202        assertSame(test.b, test.toInject.m2);
203        assertNull(test.toInject.v);
204        assertSame(test.a, test.toInject2.m1);
205        assertSame(test.b, test.toInject2.m2);
206        assertNull(test.toInject2.v);
207    }
208
209    private static class ToInjectUnsatisfiedQualifierTest {
210        @Mock(name = "a", fieldName = "m1")
211        protected IMethods a;
212
213        @Mock(name = "b", fieldName = "m2")
214        protected IMethods b;
215
216        @Mock(fieldName = "unmatched")
217        protected IVarArgs v;
218
219        @TestSubject
220        protected ToInject toInject = new ToInject();
221    }
222
223    @Test
224    public void shouldErrorWhenUnsatisfiedQualifier() {
225        try {
226            EasyMockSupport.injectMocks(new ToInjectUnsatisfiedQualifierTest());
227        } catch (AssertionError e) {
228            assertEquals("Unsatisfied qualifier: 'unmatched'",
229                    e.getMessage());
230            return;
231        }
232        fail("Expected an exception for unsatisfied fieldName qualifier");
233    }
234
235    private static class ToInjectTypeIncompatibleQualifierTest {
236        @Mock(name = "a", fieldName = "m1")
237        protected IMethods a;
238
239        @Mock(fieldName = "m2")
240        protected IVarArgs v;
241
242        @TestSubject
243        protected ToInject toInject = new ToInject();
244    }
245
246    @Test
247    public void shouldErrorForUnmatchedQualifierWhenTypeIncompatibleQualifier() {
248        try {
249            EasyMockSupport.injectMocks(new ToInjectTypeIncompatibleQualifierTest());
250        } catch (AssertionError e) {
251            assertEquals("Unsatisfied qualifier: 'm2'",
252                    e.getMessage());
253            return;
254        }
255        fail("Expected an exception for unsatisfied fieldName qualifier");
256    }
257
258    private static class ToInjectUnassignableField extends ToInject {
259        final IMethods finalField = null;
260
261        static IMethods staticField;
262    }
263
264    private static class ToInjectUnassignableFinalFieldQualifierTest {
265        @Mock(name = "a", fieldName = "finalField")
266        protected IMethods a;
267
268        @TestSubject
269        protected ToInjectUnassignableField toInject = new ToInjectUnassignableField();
270    }
271
272    @Test
273    public void shouldErrorForUnmatchedQualifierWhenUnassignableFinalField() {
274        try {
275            EasyMockSupport.injectMocks(new ToInjectUnassignableFinalFieldQualifierTest());
276        } catch (AssertionError e) {
277            assertEquals("Unsatisfied qualifier: 'finalField'",
278                    e.getMessage());
279            return;
280        }
281        fail("Expected an exception for unsatisfied fieldName qualifier");
282    }
283
284    private static class ToInjectUnassignableStaticFieldQualifierTest {
285        @Mock(name = "a", fieldName = "staticField")
286        protected IMethods a;
287
288        @TestSubject
289        protected ToInjectUnassignableField toInject = new ToInjectUnassignableField();
290    }
291
292    @Test
293    public void shouldErrorForUnmatchedQualifierWhenUnassignableStaticField() {
294        try {
295            EasyMockSupport.injectMocks(new ToInjectUnassignableStaticFieldQualifierTest());
296        } catch (AssertionError e) {
297            assertEquals("Unsatisfied qualifier: 'staticField'",
298                    e.getMessage());
299            return;
300        }
301        fail("Expected an exception for unsatisfied fieldName qualifier");
302    }
303
304    private static class ToInjectDuplicateQualifierTest {
305        @Mock(name = "a", fieldName = "m1")
306        protected IMethods a;
307
308        @Mock(name = "b", fieldName = "m1")
309        protected IMethods b;
310
311        @TestSubject
312        protected ToInject toInject = new ToInject();
313    }
314
315    @Test
316    public void shouldErrorWhenDuplicateQualifiers() {
317        try {
318            EasyMockSupport.injectMocks(new ToInjectDuplicateQualifierTest());
319            fail("Expected an exception for duplicate fieldName qualifier");
320        } catch (AssertionError e) {
321            assertEquals("At least two mocks have fieldName qualifier 'm1'",
322                    e.getMessage());
323        }
324    }
325
326    private static class ToInjectOneTarget {
327        protected IMethods m1;
328    }
329
330    private static class ToInjectQualifiedAndUnqualifiedTest {
331        @Mock(name = "a")
332        protected IMethods a;
333
334        @Mock(name = "b", fieldName = "m1")
335        protected IMethods b;
336
337        @TestSubject
338        protected ToInjectOneTarget toInjectOneTarget = new ToInjectOneTarget();
339    }
340
341    @Test
342    public void shouldNotAssignUnqualifiedMockWhereQualifiedMockAssigned() {
343        ToInjectQualifiedAndUnqualifiedTest test = new ToInjectQualifiedAndUnqualifiedTest();
344        EasyMockSupport.injectMocks(test);
345        assertSame(test.b, test.toInjectOneTarget.m1);
346    }
347
348    private static class TypeDefinedTwiceTest {
349        @Mock(value=MockType.STRICT, type=MockType.STRICT)
350        protected IMethods a;
351    }
352
353    @Test
354    public void shouldNotDefineValueAndTypeAtTheSameTime() {
355        TypeDefinedTwiceTest test = new TypeDefinedTwiceTest();
356        try {
357            EasyMockSupport.injectMocks(test);
358            fail("Should not accept the redefinition");
359        } catch(AssertionError e) {
360            assertEquals("@Mock.value() and @Mock.type() are aliases, you can't specify both at the same time", e.getMessage());
361        }
362    }
363
364    private static class TypeDefinedUsingValue {
365        @Mock(MockType.STRICT)
366        private IMethods standardMock;
367
368        @TestSubject
369        protected ToInjectOneTarget toInjectOneTarget = new ToInjectOneTarget();
370    }
371
372    @Test
373    public void canUseValueToDefineType() {
374        TypeDefinedUsingValue test = new TypeDefinedUsingValue();
375        EasyMockSupport.injectMocks(test);
376        assertSame(test.standardMock, test.toInjectOneTarget.m1);
377        assertEquals(MocksControl.getControl(test.standardMock).getType(), MockType.STRICT);
378    }
379}
380
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
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)