Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
29   101   7   4,14
0   60   0,24   7
7     1  
1    
 
  CallbackAndArgumentsTest       Line # 30 29 0% 7 0 100% 1.0
 
  (6)
 
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.IAnswer;
23    import org.easymock.tests.IMethods;
24    import org.junit.Before;
25    import org.junit.Test;
26   
27    /**
28    * @author OFFIS, Tammo Freese
29    */
 
30    public class CallbackAndArgumentsTest {
31   
32    private IMethods mock;
33   
 
34  6 toggle @Before
35    public void setUp() {
36  6 mock = createStrictMock(IMethods.class);
37    }
38   
 
39  2 toggle @Test
40    public void callbackGetsArguments() {
41   
42  2 final StringBuilder buffer = new StringBuilder();
43   
44  2 mock.simpleMethodWithArgument((String) notNull());
45  2 expectLastCall().andAnswer(new IAnswer<Object>() {
 
46  4 toggle public Object answer() {
47  4 buffer.append((String) getCurrentArguments()[0]);
48  4 return null;
49    }
50    }).times(2);
51   
52  2 replay(mock);
53   
54  2 mock.simpleMethodWithArgument("1");
55  2 mock.simpleMethodWithArgument("2");
56   
57  2 verify(mock);
58   
59  2 assertEquals("12", buffer.toString());
60    }
61   
 
62  2 toggle @Test(expected = IllegalStateException.class)
63    public void currentArgumentsFailsOutsideCallbacks() {
64  2 getCurrentArguments();
65    }
66   
 
67  2 toggle @Test
68    public void callbackGetsArgumentsEvenIfAMockCallsAnother() {
69   
70  2 final StringBuilder buffer = new StringBuilder();
71   
72  2 final IMethods mock2 = createStrictMock(IMethods.class);
73  2 mock2.simpleMethod();
74  2 expectLastCall().andAnswer(new IAnswer<Object>() {
 
75  4 toggle public Object answer() {
76    // empty, only needed to force deletion of arguments
77  4 return null;
78    }
79    }).times(2);
80   
81  2 mock.simpleMethodWithArgument((String) notNull());
82  2 expectLastCall().andAnswer(new IAnswer<Object>() {
 
83  4 toggle public Object answer() {
84  4 mock2.simpleMethod();
85  4 buffer.append((String) getCurrentArguments()[0]);
86  4 return null;
87    }
88    }).times(2);
89   
90  2 replay(mock);
91  2 replay(mock2);
92   
93  2 mock.simpleMethodWithArgument("1");
94  2 mock.simpleMethodWithArgument("2");
95   
96  2 verify(mock);
97  2 verify(mock2);
98   
99  2 assertEquals("12", buffer.toString());
100    }
101    }