Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
134   310   30   9,57
8   222   0,22   14
14     2,14  
1    
 
  UsageTest       Line # 29 134 0% 30 15 90,4% 0.90384614
 
  (26)
 
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 org.easymock.tests.IMethods;
23    import org.junit.Before;
24    import org.junit.Test;
25   
26    /**
27    * @author OFFIS, Tammo Freese
28    */
 
29    public class UsageTest {
30   
31    IMethods mock;
32   
 
33  26 toggle @Before
34    public void setup() {
35  26 mock = createMock(IMethods.class);
36    }
37   
 
38  2 toggle @Test
39    public void exactCallCountByLastCall() {
40  2 expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2");
41  2 replay(mock);
42   
43  2 assertEquals("Test", mock.oneArg(false));
44  2 assertEquals("Test2", mock.oneArg(false));
45   
46  2 boolean failed = false;
47  2 try {
48  2 mock.oneArg(false);
49    } catch (final AssertionError expected) {
50  2 failed = true;
51    }
52  2 if (!failed)
53  0 fail("expected AssertionError");
54    }
55   
 
56  2 toggle @Test
57    public void openCallCountByLastCall() {
58  2 expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").atLeastOnce();
59   
60  2 replay(mock);
61   
62  2 assertEquals("Test", mock.oneArg(false));
63  2 assertEquals("Test2", mock.oneArg(false));
64  2 assertEquals("Test2", mock.oneArg(false));
65    }
66   
 
67  2 toggle @Test
68    public void exactCallCountByLastThrowable() {
69  2 expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").andThrow(
70    new IndexOutOfBoundsException());
71   
72  2 replay(mock);
73   
74  2 assertEquals("Test", mock.oneArg(false));
75  2 assertEquals("Test2", mock.oneArg(false));
76   
77  2 try {
78  2 mock.oneArg(false);
79  0 fail();
80    } catch (final IndexOutOfBoundsException expected) {
81    }
82   
83  2 boolean failed = true;
84  2 try {
85  2 mock.oneArg(false);
86  0 failed = false;
87    } catch (final AssertionError expected) {
88    }
89  2 if (!failed)
90  0 fail("expected AssertionError");
91    }
92   
 
93  2 toggle @Test
94    public void openCallCountByLastThrowable() {
95  2 expect(mock.oneArg(false)).andReturn("Test").andReturn("Test2").andThrow(
96    new IndexOutOfBoundsException()).atLeastOnce();
97   
98  2 replay(mock);
99   
100  2 assertEquals("Test", mock.oneArg(false));
101  2 assertEquals("Test2", mock.oneArg(false));
102   
103  2 try {
104  2 mock.oneArg(false);
105    } catch (final IndexOutOfBoundsException expected) {
106    }
107  2 try {
108  2 mock.oneArg(false);
109    } catch (final IndexOutOfBoundsException expected) {
110    }
111    }
112   
 
113  2 toggle @Test
114    public void moreThanOneArgument() {
115  2 expect(mock.threeArgumentMethod(1, "2", "3")).andReturn("Test").times(2);
116   
117  2 replay(mock);
118   
119  2 assertEquals("Test", mock.threeArgumentMethod(1, "2", "3"));
120   
121  2 boolean failed = true;
122  2 try {
123  2 verify(mock);
124  0 failed = false;
125    } catch (final AssertionError expected) {
126  2 assertEquals("\n Expectation failure on verify:"
127    + "\n threeArgumentMethod(1, \"2\", \"3\"): expected: 2, actual: 1", expected
128    .getMessage());
129    }
130  2 if (!failed) {
131  0 fail("exception expected");
132    }
133    }
134   
 
135  2 toggle @Test
136    public void wrongArguments() {
137  2 mock.simpleMethodWithArgument("3");
138  2 replay(mock);
139   
140  2 try {
141  2 mock.simpleMethodWithArgument("5");
142  0 fail();
143    } catch (final AssertionError expected) {
144  2 assertEquals("\n Unexpected method call simpleMethodWithArgument(\"5\"):"
145    + "\n simpleMethodWithArgument(\"3\"): expected: 1, actual: 0", expected.getMessage());
146    }
147   
148    }
149   
 
150  2 toggle @Test
151    public void summarizeSameObjectArguments() {
152  2 mock.simpleMethodWithArgument("3");
153  2 mock.simpleMethodWithArgument("3");
154  2 replay(mock);
155   
156  2 try {
157  2 mock.simpleMethodWithArgument("5");
158  0 fail();
159    } catch (final AssertionError expected) {
160  2 assertEquals("\n Unexpected method call simpleMethodWithArgument(\"5\"):"
161    + "\n simpleMethodWithArgument(\"3\"): expected: 2, actual: 0", expected.getMessage());
162    }
163   
164    }
165   
 
166  2 toggle @Test
167    public void argumentsOrdered() {
168  2 mock.simpleMethodWithArgument("4");
169  2 mock.simpleMethodWithArgument("3");
170  2 mock.simpleMethodWithArgument("2");
171  2 mock.simpleMethodWithArgument("0");
172  2 mock.simpleMethodWithArgument("1");
173  2 replay(mock);
174   
175  2 try {
176  2 mock.simpleMethodWithArgument("5");
177  0 fail("exception expected");
178    } catch (final AssertionError expected) {
179  2 assertEquals("\n Unexpected method call simpleMethodWithArgument(\"5\"):"
180    + "\n simpleMethodWithArgument(\"4\"): expected: 1, actual: 0"
181    + "\n simpleMethodWithArgument(\"3\"): expected: 1, actual: 0"
182    + "\n simpleMethodWithArgument(\"2\"): expected: 1, actual: 0"
183    + "\n simpleMethodWithArgument(\"0\"): expected: 1, actual: 0"
184    + "\n simpleMethodWithArgument(\"1\"): expected: 1, actual: 0", expected.getMessage());
185    }
186   
187    }
188   
 
189  2 toggle @Test
190    public void mixingOrderedAndUnordered() {
191  2 mock.simpleMethodWithArgument("2");
192  2 mock.simpleMethodWithArgument("1");
193  2 checkOrder(mock, true);
194  2 mock.simpleMethodWithArgument("3");
195  2 mock.simpleMethodWithArgument("4");
196  2 checkOrder(mock, false);
197  2 mock.simpleMethodWithArgument("6");
198  2 mock.simpleMethodWithArgument("7");
199  2 mock.simpleMethodWithArgument("5");
200   
201  2 replay(mock);
202   
203  2 mock.simpleMethodWithArgument("1");
204  2 mock.simpleMethodWithArgument("2");
205   
206  2 boolean failed = false;
207  2 try {
208  2 mock.simpleMethodWithArgument("4");
209    } catch (final AssertionError e) {
210  2 failed = true;
211    }
212  2 if (!failed) {
213  0 fail();
214    }
215   
216  2 mock.simpleMethodWithArgument("3");
217  2 mock.simpleMethodWithArgument("4");
218  2 mock.simpleMethodWithArgument("5");
219  2 mock.simpleMethodWithArgument("6");
220  2 mock.simpleMethodWithArgument("7");
221   
222  2 verify(mock);
223   
224    }
225   
 
226  2 toggle @Test
227    public void resumeIfFailure() {
228  2 final IMethods mock = createMock(IMethods.class);
229  2 expect(mock.oneArg(true)).andReturn("foo").anyTimes();
230  2 replay(mock);
231   
232  2 mock.oneArg(true);
233   
234  2 try {
235  2 mock.simpleMethod();
236    } catch (final AssertionError error) {
237    }
238   
239  2 mock.oneArg(true);
240   
241  2 verify(mock);
242    }
243   
 
244  2 toggle @Test
245    public void defaultResetToNice() {
246  2 final IMethods mock = createMock(IMethods.class);
247   
248  2 expect(mock.oneArg(true)).andReturn("foo");
249  2 replay(mock);
250   
251  2 resetToNice(mock);
252   
253  2 replay(mock);
254   
255  2 assertNull(mock.oneArg(true));
256   
257  2 verify(mock);
258    }
259   
 
260  2 toggle @Test
261    public void strictResetToDefault() {
262  2 final IMethods mock = createStrictMock(IMethods.class);
263   
264  2 expect(mock.oneArg(true)).andReturn("foo");
265  2 expect(mock.oneArg(false)).andReturn("foo");
266   
267  2 replay(mock);
268   
269  2 resetToDefault(mock);
270   
271  2 expect(mock.oneArg(false)).andReturn("foo");
272  2 expect(mock.oneArg(true)).andReturn("foo");
273   
274  2 replay(mock);
275   
276  2 assertEquals("foo", mock.oneArg(false));
277  2 assertEquals("foo", mock.oneArg(true));
278   
279  2 verify(mock);
280    }
281   
 
282  2 toggle @Test
283    public void niceToStrict() {
284  2 final IMethods mock = createNiceMock(IMethods.class);
285   
286  2 expect(mock.oneArg(false)).andReturn("foo");
287   
288  2 replay(mock);
289   
290  2 assertNull(mock.oneArg(true));
291   
292  2 resetToStrict(mock);
293   
294  2 expect(mock.oneArg(false)).andReturn("foo");
295  2 expect(mock.oneArg(true)).andReturn("foo");
296   
297  2 replay(mock);
298   
299  2 try {
300  2 mock.oneArg(true);
301  0 fail();
302    } catch (final AssertionError e) {
303    }
304   
305  2 assertEquals("foo", mock.oneArg(false));
306  2 assertEquals("foo", mock.oneArg(true));
307   
308  2 verify(mock);
309    }
310    }