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