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