Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
101   272   35   4,21
0   218   0,35   24
24     1,46  
1    
 
  MockBuilderTest       Line # 35 101 0% 35 11 91,2% 0.912
 
  (46)
 
1    /**
2    * Copyright 2001-2010 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    */
16   
17    package org.easymock.tests2;
18   
19    import static org.easymock.EasyMock.*;
20    import static org.junit.Assert.*;
21   
22    import java.lang.reflect.Field;
23    import java.util.ArrayList;
24   
25    import org.easymock.ConstructorArgs;
26    import org.easymock.IMocksControl;
27    import org.easymock.internal.ClassExtensionHelper;
28    import org.easymock.internal.MockBuilder;
29    import org.junit.Before;
30    import org.junit.Test;
31   
32    /**
33    * @author Henri Tremblay
34    */
 
35    public class MockBuilderTest {
36   
37    private MockBuilder<ArrayList<String>> builder;
38   
39    private ArrayList<String> mock;
40   
 
41  46 toggle @SuppressWarnings("unchecked")
42    @Before
43    public void setUp() throws Exception {
44  46 builder = new MockBuilder(ArrayList.class);
45    }
46   
 
47  2 toggle @Test
48    public void testAddMockedMethod() throws NoSuchMethodException {
49  2 builder.addMockedMethod(ArrayList.class.getMethod("size")).addMockedMethod("contains")
50    .addMockedMethod("add", Object.class).addMockedMethods("clear", "isEmpty").addMockedMethods(
51    ArrayList.class.getMethod("get", int.class),
52    ArrayList.class.getMethod("indexOf", Object.class));
53   
54  2 mock = builder.createMock();
55   
56  2 expect(mock.size()).andReturn(3);
57  2 expect(mock.contains("test")).andReturn(true);
58  2 expect(mock.add("added")).andReturn(true);
59  2 mock.clear();
60  2 expect(mock.isEmpty()).andReturn(false);
61  2 expect(mock.get(1)).andReturn("result");
62  2 expect(mock.indexOf("t")).andReturn(2);
63   
64  2 replay(mock);
65   
66  2 assertEquals(3, mock.size());
67  2 assertEquals(true, mock.contains("test"));
68  2 assertEquals(true, mock.add("added"));
69  2 mock.clear();
70  2 assertEquals(false, mock.isEmpty());
71  2 assertEquals("result", mock.get(1));
72  2 assertEquals(2, mock.indexOf("t"));
73   
74  2 verify(mock);
75    }
76   
 
77  2 toggle @Test(expected = IllegalArgumentException.class)
78    public void testAddMethod_NotExisting() {
79  2 builder.addMockedMethod("..");
80    }
81   
 
82  2 toggle @Test(expected = IllegalArgumentException.class)
83    public void testAddMethodWithParams_NotExisting() {
84  2 builder.addMockedMethod("..", String.class);
85    }
86   
 
87  2 toggle @Test
88    public void testWithConstructorParams() {
89  2 builder.withConstructor(int.class).withArgs(-3);
90  2 try {
91  2 builder.createMock();
92  0 fail("instantiation should fail because of negative");
93    } catch (final RuntimeException e) {
94    }
95    }
96   
 
97  2 toggle @Test(expected = IllegalArgumentException.class)
98    public void testWithConstructor_WrongClass() {
99  2 builder.withConstructor(long.class);
100    }
101   
 
102  2 toggle @Test
103    public void testWithEmptyConstructor() throws Exception {
104  2 mock = builder.withConstructor().createMock();
105  2 final Field field = ArrayList.class.getDeclaredField("elementData");
106  2 field.setAccessible(true);
107  2 final int expected = ((Object[]) field.get(new ArrayList<String>())).length;
108  2 final int actual = ((Object[]) field.get(mock)).length;
109  2 assertEquals(expected, actual);
110    }
111   
 
112  2 toggle @Test
113    public void testWithEmptyConstructor_NoEmptyConstructor() throws Exception {
114  2 try {
115  2 createMockBuilder(Integer.class).withConstructor().createMock();
116  0 fail("no empty constructor should be found");
117    } catch (final IllegalArgumentException e) {
118    }
119    }
120   
 
121  2 toggle @Test
122    public void testWithConstructor() throws NoSuchMethodException {
123  2 builder.withConstructor(ArrayList.class.getConstructor(int.class)).withArgs(-3);
124  2 try {
125  2 builder.createMock();
126  0 fail("instantiation should fail because of negative");
127    } catch (final RuntimeException e) {
128    }
129    }
130   
 
131  2 toggle @Test(expected = IllegalStateException.class)
132    public void testWithConstructor_Twice() {
133  2 builder.withConstructor(int.class).withConstructor(int.class);
134    }
135   
 
136  2 toggle @Test
137    public void testWithConstructorConstructorArgs() throws NoSuchMethodException {
138  2 final ConstructorArgs args = new ConstructorArgs(ArrayList.class.getConstructor(int.class), Integer
139    .valueOf(-3));
140  2 builder.withConstructor(args);
141  2 try {
142  2 builder.createMock();
143  0 fail("instantiation should fail because of negative");
144    } catch (final RuntimeException e) {
145    }
146    }
147   
 
148  2 toggle @Test
149    public void testWithConstructorWithArgs() throws NoSuchMethodException {
150  2 builder.withConstructor(-3);
151  2 try {
152  2 builder.createMock();
153  0 fail("instantiation should fail because of negative");
154    } catch (final RuntimeException e) {
155    }
156    }
157   
 
158  2 toggle @Test(expected = IllegalArgumentException.class)
159    public void testWithConstructorWithArgs_NotExisting() throws NoSuchMethodException {
160  2 builder.withConstructor("string");
161    }
162   
 
163  2 toggle @Test
164    public void testWithArgsTwice() {
165  2 try {
166  2 builder.withConstructor(int.class).withArgs(3).withArgs(2);
167  0 fail("withArgs called twice");
168    } catch (final IllegalStateException e) {
169  2 assertEquals("Trying to define the constructor arguments more than once.", e.getMessage());
170    }
171    }
172   
 
173  2 toggle @Test
174    public void testWithArgs_WithoutConstructor() {
175  2 try {
176  2 builder.withArgs(2);
177  0 fail("withArgs without constructor");
178    } catch (final IllegalStateException e) {
179  2 assertEquals("Trying to define constructor arguments without first setting their type.", e
180    .getMessage());
181    }
182    }
183   
 
184  2 toggle @Test
185    public void testCreateMockIMocksControl() {
186  2 final IMocksControl ctrl = createControl();
187  2 mock = builder.createMock(ctrl);
188  2 assertSame(ClassExtensionHelper.getControl(mock), ctrl);
189    }
190   
 
191  2 toggle @Test
192    public void testCreateMock() {
193  2 mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock();
194  2 replay(mock);
195  2 try {
196  2 mock.size();
197  0 fail("Unexpected call");
198    } catch (final AssertionError e) {
199    }
200    }
201   
 
202  2 toggle @Test
203    public void testCreateNiceMock() {
204  2 mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock();
205  2 replay(mock);
206  2 assertEquals(0, mock.size());
207  2 verify(mock);
208    }
209   
 
210  2 toggle @Test
211    public void testCreateStrictMock() {
212  2 mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString")
213    .createStrictMock();
214  2 expect(mock.size()).andReturn(1);
215  2 mock.clear();
216  2 replay(mock);
217  2 try {
218  2 mock.clear();
219  0 fail("Unexpected call");
220    } catch (final AssertionError e) {
221    }
222    }
223   
 
224  2 toggle @Test
225    public void testCreateMockStringIMocksControl() {
226  2 final IMocksControl ctrl = createControl();
227  2 mock = builder.addMockedMethod("toString").createMock("myName", ctrl);
228  2 assertSame(ClassExtensionHelper.getControl(mock), ctrl);
229  2 assertTrue(mock.toString().contains("myName"));
230    }
231   
 
232  2 toggle @Test
233    public void testCreateMockString() {
234  2 mock = builder.addMockedMethod("size").addMockedMethod("toString").createMock("myName");
235  2 replay(mock);
236  2 try {
237  2 mock.size();
238  0 fail("Unexpected call");
239    } catch (final AssertionError e) {
240  2 assertTrue(e.getMessage().contains("myName"));
241    }
242    }
243   
 
244  2 toggle @Test
245    public void testCreateNiceMockString() {
246  2 mock = builder.addMockedMethod("size").addMockedMethod("toString").createNiceMock("myName");
247  2 replay(mock);
248  2 assertEquals(0, mock.size());
249  2 verify(mock);
250  2 assertTrue(mock.toString().contains("myName"));
251    }
252   
 
253  2 toggle @Test
254    public void testCreateStrictMockString() throws Throwable {
255  2 mock = builder.addMockedMethod("size").addMockedMethod("clear").addMockedMethod("toString")
256    .createStrictMock("myName");
257  2 expect(mock.size()).andReturn(1);
258  2 mock.clear();
259  2 replay(mock);
260  2 try {
261  2 mock.clear();
262  0 fail("Unexpected call");
263    } catch (final AssertionError e) {
264  2 assertTrue(e.getMessage().contains("myName"));
265    }
266    }
267   
 
268  2 toggle @Test(expected = IllegalStateException.class)
269    public void testCreateMock_ConstructorWithoutArgs() {
270  2 builder.withConstructor(int.class).createMock();
271    }
272    }