Clover Coverage Report - EasyMock 2.4
Coverage timestamp: mer. juil. 2 2008 02:17:38 CEST
113   239   26   11,3
16   199   0,23   10
10     2,6  
1    
 
 
  UsageStrictMockTest       Line # 15 113 26 88,5% 0.8848921
 
  (9)
 
1    /*
2    * Copyright (c) 2001-2008 OFFIS, Tammo Freese.
3    * This program is made available under the terms of the MIT License.
4    */
5    package org.easymock.tests;
6   
7    import static org.junit.Assert.*;
8   
9    import org.easymock.MockControl;
10    import org.easymock.internal.ReplayState;
11    import org.junit.Before;
12    import org.junit.Test;
13   
14    @SuppressWarnings("deprecation")
 
15    public class UsageStrictMockTest {
16    private MockControl<IMethods> control;
17   
18    private IMethods mock;
19   
 
20  9 toggle @Before
21    public void setup() {
22  9 control = MockControl.createStrictControl(IMethods.class);
23  9 mock = control.getMock();
24   
25  9 mock.simpleMethodWithArgument("1");
26  9 mock.simpleMethodWithArgument("2");
27   
28  9 control.replay();
29    }
30   
 
31  1 toggle @Test
32    public void verify() {
33  1 control.reset();
34  1 control.replay();
35  1 control.verify();
36    }
37   
 
38  1 toggle @Test
39    public void orderedCallsSucces() {
40  1 mock.simpleMethodWithArgument("1");
41  1 mock.simpleMethodWithArgument("2");
42   
43  1 control.verify();
44    }
45   
 
46  1 toggle @Test
47    public void unorderedCallsFailure() {
48  1 boolean failed = false;
49  1 try {
50  1 mock.simpleMethodWithArgument("2");
51    } catch (AssertionError expected) {
52  1 failed = true;
53    }
54  1 if (!failed) {
55  0 fail("unordered calls accepted");
56    }
57    }
58   
 
59  1 toggle @Test
60    public void tooManyCallsFailure() {
61  1 mock.simpleMethodWithArgument("1");
62  1 mock.simpleMethodWithArgument("2");
63   
64  1 boolean failed = false;
65  1 try {
66  1 mock.simpleMethodWithArgument("2");
67    } catch (AssertionError expected) {
68  1 failed = true;
69    }
70  1 if (!failed) {
71  0 fail("too many calls accepted");
72    }
73    }
74   
 
75  1 toggle @Test
76    public void tooFewCallsFailure() {
77  1 mock.simpleMethodWithArgument("1");
78  1 boolean failed = false;
79  1 try {
80  1 control.verify();
81    } catch (AssertionError expected) {
82  1 failed = true;
83  1 assertTrue("stack trace must be filled in", Util.getStackTrace(
84    expected).indexOf(ReplayState.class.getName()) == -1);
85    }
86  1 if (!failed) {
87  0 fail("too few calls accepted");
88    }
89    }
90   
 
91  1 toggle @Test
92    public void differentMethods() {
93   
94  1 control.reset();
95   
96  1 mock.booleanReturningMethod(0);
97  1 control.setReturnValue(true);
98  1 mock.simpleMethod();
99  1 mock.booleanReturningMethod(1);
100  1 control.setReturnValue(false, 2, 3);
101  1 mock.simpleMethod();
102  1 control.setVoidCallable(MockControl.ONE_OR_MORE);
103   
104  1 control.replay();
105  1 assertEquals(true, mock.booleanReturningMethod(0));
106  1 mock.simpleMethod();
107   
108  1 boolean failed = false;
109  1 try {
110  1 control.verify();
111    } catch (AssertionError expected) {
112  1 failed = true;
113  1 assertEquals(
114    "\n Expectation failure on verify:"
115    + "\n simpleMethod(): expected: 1, actual: 1"
116    + "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 0"
117    + "\n simpleMethod(): expected: at least 1, actual: 0",
118    expected.getMessage());
119    }
120  1 if (!failed) {
121  0 fail("too few calls accepted");
122    }
123   
124  1 assertEquals(false, mock.booleanReturningMethod(1));
125   
126  1 failed = false;
127  1 try {
128  1 mock.simpleMethod();
129    } catch (AssertionError expected) {
130  1 failed = true;
131  1 assertEquals(
132    "\n Unexpected method call simpleMethod():"
133    + "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 1",
134    expected.getMessage());
135    }
136  1 if (!failed) {
137  0 fail("wrong call accepted");
138    }
139    }
140   
 
141  1 toggle @Test
142    public void range() {
143   
144  1 control.reset();
145   
146  1 mock.booleanReturningMethod(0);
147  1 control.setReturnValue(true);
148  1 mock.simpleMethod();
149  1 mock.booleanReturningMethod(1);
150  1 control.setReturnValue(false, 2, 3);
151  1 mock.simpleMethod();
152  1 control.setVoidCallable(MockControl.ONE_OR_MORE);
153  1 mock.booleanReturningMethod(1);
154  1 control.setReturnValue(false);
155   
156  1 control.replay();
157   
158  1 mock.booleanReturningMethod(0);
159  1 mock.simpleMethod();
160   
161  1 mock.booleanReturningMethod(1);
162  1 mock.booleanReturningMethod(1);
163  1 mock.booleanReturningMethod(1);
164   
165  1 boolean failed = false;
166   
167  1 try {
168  1 mock.booleanReturningMethod(1);
169    } catch (AssertionError expected) {
170  1 failed = true;
171  1 assertEquals(
172    "\n Unexpected method call booleanReturningMethod(1):"
173    + "\n booleanReturningMethod(1): expected: between 2 and 3, actual: 3 (+1)"
174    + "\n simpleMethod(): expected: at least 1, actual: 0",
175    expected.getMessage());
176    }
177  1 if (!failed) {
178  0 fail("too many calls accepted");
179    }
180    }
181   
 
182  1 toggle @Test
183    public void defaultBehavior() {
184  1 control.reset();
185   
186  1 mock.booleanReturningMethod(1);
187  1 control.setReturnValue(true);
188  1 control.setReturnValue(false);
189  1 control.setReturnValue(true);
190  1 control.setDefaultReturnValue(true);
191   
192  1 control.replay();
193   
194  1 assertEquals(true, mock.booleanReturningMethod(2));
195  1 assertEquals(true, mock.booleanReturningMethod(3));
196  1 assertEquals(true, mock.booleanReturningMethod(1));
197  1 assertEquals(false, mock.booleanReturningMethod(1));
198  1 assertEquals(true, mock.booleanReturningMethod(3));
199   
200  1 boolean failed = false;
201  1 try {
202  1 control.verify();
203    } catch (AssertionError expected) {
204  1 failed = true;
205  1 assertEquals(
206    "\n Expectation failure on verify:"
207    + "\n booleanReturningMethod(1): expected: 3, actual: 2",
208    expected.getMessage());
209    }
210  1 if (!failed) {
211  0 fail("too few calls accepted");
212    }
213    }
214   
 
215  1 toggle @Test
216    public void unexpectedCallWithArray() {
217  1 control.reset();
218  1 control.setDefaultMatcher(MockControl.ARRAY_MATCHER);
219  1 mock.arrayMethod(new String[] { "Test", "Test 2" });
220  1 control.replay();
221  1 boolean failed = false;
222  1 String[] strings = new String[] { "Test" };
223  1 try {
224  1 mock.arrayMethod(strings);
225    } catch (AssertionError expected) {
226  1 failed = true;
227  1 assertEquals(
228    "\n Unexpected method call arrayMethod("
229    + strings.toString()
230    + "):"
231    + "\n arrayMethod([\"Test\", \"Test 2\"]): expected: 1, actual: 0",
232    expected.getMessage());
233    }
234  1 if (!failed) {
235  0 fail("exception expected");
236    }
237   
238    }
239    }