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

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

Run Easymock automation tests on LambdaTest cloud grid

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

copy
1/**
2 * Copyright 2001-2013 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 static org.easymock.EasyMock.*;
19import static org.junit.Assert.*;
20
21import org.junit.Test;
22
23/**
24 * @author Henri Tremblay
25 */
26public class DelegateToTest {
27
28    public static interface IMyInterface {
29        int getInt(int k);
30    }
31
32    @Test
33    public void testDelegate() {
34        final IMyInterface mock = createMock(IMyInterface.class);
35        final IMyInterface delegateTo = new IMyInterface() {
36            private int i = 0;
37
38            public int getInt(final int k) {
39                return i += k;
40            }
41        };
42
43        expect(mock.getInt(10)).andDelegateTo(delegateTo);
44        expect(mock.getInt(5)).andDelegateTo(delegateTo).andDelegateTo(delegateTo).times(2);
45
46        replay(mock);
47
48        assertEquals(10, mock.getInt(10));
49        assertEquals(15, mock.getInt(5));
50        assertEquals(20, mock.getInt(5));
51        assertEquals(25, mock.getInt(5));
52
53        verify(mock);
54    }
55
56    @Test
57    public void testStubDelegate() {
58        final IMyInterface mock = createMock(IMyInterface.class);
59        final IMyInterface delegateTo = new IMyInterface() {
60            private int i = 0;
61
62            public int getInt(final int k) {
63                return ++i;
64            }
65        };
66        expect(mock.getInt(5)).andReturn(3).andStubDelegateTo(delegateTo);
67        expect(mock.getInt(20)).andStubDelegateTo(delegateTo);
68
69        replay(mock);
70
71        assertEquals(3, mock.getInt(5));
72        assertEquals(1, mock.getInt(5));
73        assertEquals(2, mock.getInt(5));
74        assertEquals(3, mock.getInt(20));
75        assertEquals(4, mock.getInt(20));
76
77        verify(mock);
78    }
79
80    @Test
81    public void testReturnException() {
82        final IMyInterface m = createMock(IMyInterface.class);
83        final IMyInterface delegateTo = new IMyInterface() {
84            public int getInt(final int k) {
85                throw new ArithmeticException("Not good!");
86            }
87        };
88        expect(m.getInt(5)).andDelegateTo(delegateTo);
89
90        replay(m);
91
92        try {
93            m.getInt(5);
94            fail();
95        } catch (final ArithmeticException e) {
96            assertEquals("Not good!", e.getMessage());
97        }
98
99        verify(m);
100    }
101
102    @Test
103    public void testWrongClass() {
104        final IMyInterface m = createMock(IMyInterface.class);
105        expect(m.getInt(0)).andDelegateTo("allo");
106        replay(m);
107        try {
108            m.getInt(0);
109            fail("Should throw an exception");
110        } catch (final IllegalArgumentException e) {
111            assertEquals(
112                    "Delegation to object [allo] is not implementing the mocked method [public abstract int org.easymock.tests2.DelegateToTest$IMyInterface.getInt(int)]",
113                    e.getMessage());
114        }
115    }
116
117    @Test
118    public void nullDelegationNotAllowed() {
119        final IMyInterface mock = createMock(IMyInterface.class);
120        try {
121            expect(mock.getInt(1)).andDelegateTo(null);
122            fail();
123        } catch (final NullPointerException expected) {
124            assertEquals("delegated to object must not be null", expected.getMessage());
125        }
126    }
127
128    @Test
129    public void nullStubDelegationNotAllowed() {
130        final IMyInterface mock = createMock(IMyInterface.class);
131        try {
132            expect(mock.getInt(1)).andStubDelegateTo(null);
133            fail();
134        } catch (final NullPointerException expected) {
135            assertEquals("delegated to object must not be null", expected.getMessage());
136        }
137    }
138}
139
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)