Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
50   139   13   5,56
0   98   0,26   4,5
9     1,44  
2    
 
  DelegateToTest       Line # 27 50 0% 13 4 93,2% 0.9322034
  DelegateToTest.IMyInterface       Line # 29 0 - 0 0 - -1.0
 
  (12)
 
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.junit.Test;
23   
24    /**
25    * @author Henri Tremblay
26    */
 
27    public class DelegateToTest {
28   
 
29    public static interface IMyInterface {
30    int getInt(int k);
31    }
32   
 
33  2 toggle @Test
34    public void testDelegate() {
35  2 final IMyInterface mock = createMock(IMyInterface.class);
36  2 final IMyInterface delegateTo = new IMyInterface() {
37    private int i = 0;
38   
 
39  8 toggle public int getInt(final int k) {
40  8 return i += k;
41    }
42    };
43   
44  2 expect(mock.getInt(10)).andDelegateTo(delegateTo);
45  2 expect(mock.getInt(5)).andDelegateTo(delegateTo).andDelegateTo(delegateTo).times(2);
46   
47  2 replay(mock);
48   
49  2 assertEquals(10, mock.getInt(10));
50  2 assertEquals(15, mock.getInt(5));
51  2 assertEquals(20, mock.getInt(5));
52  2 assertEquals(25, mock.getInt(5));
53   
54  2 verify(mock);
55    }
56   
 
57  2 toggle @Test
58    public void testStubDelegate() {
59  2 final IMyInterface mock = createMock(IMyInterface.class);
60  2 final IMyInterface delegateTo = new IMyInterface() {
61    private int i = 0;
62   
 
63  8 toggle public int getInt(final int k) {
64  8 return ++i;
65    }
66    };
67  2 expect(mock.getInt(5)).andReturn(3).andStubDelegateTo(delegateTo);
68  2 expect(mock.getInt(20)).andStubDelegateTo(delegateTo);
69   
70  2 replay(mock);
71   
72  2 assertEquals(3, mock.getInt(5));
73  2 assertEquals(1, mock.getInt(5));
74  2 assertEquals(2, mock.getInt(5));
75  2 assertEquals(3, mock.getInt(20));
76  2 assertEquals(4, mock.getInt(20));
77   
78  2 verify(mock);
79    }
80   
 
81  2 toggle @Test
82    public void testReturnException() {
83  2 final IMyInterface m = createMock(IMyInterface.class);
84  2 final IMyInterface delegateTo = new IMyInterface() {
 
85  2 toggle public int getInt(final int k) {
86  2 throw new ArithmeticException("Not good!");
87    }
88    };
89  2 expect(m.getInt(5)).andDelegateTo(delegateTo);
90   
91  2 replay(m);
92   
93  2 try {
94  2 m.getInt(5);
95  0 fail();
96    } catch (final ArithmeticException e) {
97  2 assertEquals("Not good!", e.getMessage());
98    }
99   
100  2 verify(m);
101    }
102   
 
103  2 toggle @Test
104    public void testWrongClass() {
105  2 final IMyInterface m = createMock(IMyInterface.class);
106  2 expect(m.getInt(0)).andDelegateTo("allo");
107  2 replay(m);
108  2 try {
109  2 m.getInt(0);
110  0 fail("Should throw an exception");
111    } catch (final IllegalArgumentException e) {
112  2 assertEquals(
113    "Delegation to object [allo] is not implementing the mocked method [public abstract int org.easymock.tests2.DelegateToTest$IMyInterface.getInt(int)]",
114    e.getMessage());
115    }
116    }
117   
 
118  2 toggle @Test
119    public void nullDelegationNotAllowed() {
120  2 final IMyInterface mock = createMock(IMyInterface.class);
121  2 try {
122  2 expect(mock.getInt(1)).andDelegateTo(null);
123  0 fail();
124    } catch (final NullPointerException expected) {
125  2 assertEquals("delegated to object must not be null", expected.getMessage());
126    }
127    }
128   
 
129  2 toggle @Test
130    public void nullStubDelegationNotAllowed() {
131  2 final IMyInterface mock = createMock(IMyInterface.class);
132  2 try {
133  2 expect(mock.getInt(1)).andStubDelegateTo(null);
134  0 fail();
135    } catch (final NullPointerException expected) {
136  2 assertEquals("delegated to object must not be null", expected.getMessage());
137    }
138    }
139    }