Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
67   186   17   5,15
0   132   0,25   6,5
13     1,31  
2    
 
  MocksControlTest       Line # 33 62 0% 13 4 94,4% 0.943662
  MocksControlTest.A       Line # 35 5 0% 4 5 44,4% 0.44444445
 
  (24)
 
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 java.lang.reflect.Method;
23    import java.util.ArrayList;
24    import java.util.List;
25   
26    import org.easymock.ConstructorArgs;
27    import org.easymock.IMocksControl;
28    import org.junit.Test;
29   
30    /**
31    * @author Henri Tremblay
32    */
 
33    public class MocksControlTest {
34   
 
35    public static class A {
36    int i = 1;
37   
 
38  18 toggle public A(final int i) {
39  18 this.i = i;
40    }
41   
 
42  8 toggle public int foo() {
43  8 return bar();
44    }
45   
 
46  0 toggle public int bar() {
47  0 return i;
48    }
49   
 
50  0 toggle public boolean add(final int i) {
51  0 this.i += i;
52  0 return true;
53    }
54    }
55   
 
56  2 toggle @Test
57    public void testMocksControl_Interface() {
58  2 final IMocksControl ctrl = createControl();
59  2 final List<?> list = ctrl.createMock(List.class);
60  2 testList(ctrl, list);
61    }
62   
 
63  2 toggle @Test
64    public void testMocksControl_Class() {
65  2 final IMocksControl ctrl = createControl();
66  2 final ArrayList<?> list = ctrl.createMock(ArrayList.class);
67  2 testList(ctrl, list);
68    }
69   
 
70  2 toggle @Test
71    public void testMocksControl_Class_WithName() {
72  2 final IMocksControl ctrl = createControl();
73  2 final ArrayList<?> list = ctrl.createMock("myMock", ArrayList.class);
74  2 testList(ctrl, list);
75    }
76   
 
77  2 toggle @SuppressWarnings("deprecation")
78    @Test
79    public void testMocksControl_PartialMock_NoConstructorCalled() throws Exception {
80  2 final IMocksControl ctrl = createControl();
81  2 final A a = ctrl.createMock(A.class, A.class.getMethod("bar", new Class[0]), A.class.getMethod(
82    "toString", new Class[0]));
83   
84  2 assertEquals("No constructor called so should not be initialized", 0, a.i);
85  2 expect(a.bar()).andReturn(5);
86  2 replay(a);
87  2 assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
88  2 verify(a);
89   
90  2 assertEquals("EasyMock for class org.easymock.tests2.MocksControlTest$A", a.toString());
91    }
92   
 
93  2 toggle @SuppressWarnings("deprecation")
94    @Test
95    public void testMocksControl_NamedPartialMock_NoConstructorCalled() throws Exception {
96  2 final IMocksControl ctrl = createControl();
97  2 final A a = ctrl.createMock("myMock", A.class, A.class.getMethod("bar", new Class[0]), A.class
98    .getMethod("toString", new Class[0]));
99   
100  2 assertEquals("No constructor called so should not be initialized", 0, a.i);
101  2 expect(a.bar()).andReturn(5);
102  2 replay(a);
103  2 assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
104  2 verify(a);
105   
106  2 assertEquals("myMock", a.toString());
107    }
108   
 
109  2 toggle @SuppressWarnings("deprecation")
110    @Test
111    public void testMocksControl_PartialMock_ConstructorCalled() throws Exception {
112  2 final IMocksControl ctrl = createControl();
113   
114  2 final ConstructorArgs args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 6);
115   
116  2 final A a = ctrl.createMock(A.class, args, A.class.getMethod("bar", new Class[0]), A.class.getMethod(
117    "toString", new Class[0]));
118   
119  2 assertEquals("Constructor called so should be initialized", 6, a.i);
120  2 expect(a.bar()).andReturn(5);
121  2 replay(a);
122  2 assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
123  2 verify(a);
124   
125  2 assertEquals("EasyMock for class org.easymock.tests2.MocksControlTest$A", a.toString());
126    }
127   
 
128  2 toggle @SuppressWarnings("deprecation")
129    @Test
130    public void testMocksControl_NamedPartialMock_ConstructorCalled() throws Exception {
131  2 final IMocksControl ctrl = createControl();
132   
133  2 final ConstructorArgs args = new ConstructorArgs(A.class.getConstructor(Integer.TYPE), 6);
134   
135  2 final A a = ctrl.createMock("myMock", A.class, args, A.class.getMethod("bar", new Class[0]), A.class
136    .getMethod("toString", new Class[0]));
137   
138  2 assertEquals("Constructor called so should be initialized", 6, a.i);
139  2 expect(a.bar()).andReturn(5);
140  2 replay(a);
141  2 assertEquals("foo isn't mocked so it will call bar which return 5", 5, a.foo());
142  2 verify(a);
143   
144  2 assertEquals("myMock", a.toString());
145    }
146   
 
147  2 toggle @SuppressWarnings("deprecation")
148    @Test
149    public void testInterfaceForbidden_PartialMock() throws Exception {
150  2 final ConstructorArgs args = new ConstructorArgs(ArrayList.class.getConstructor(Integer.TYPE), 6);
151  2 final Method[] methods = new Method[] { List.class.getMethod("size", new Class[0]) };
152   
153  2 final IMocksControl ctrl = createControl();
154   
155  2 try {
156  2 ctrl.createMock(List.class, methods);
157  0 fail("partial mocking on interface shouln't be allowed");
158    } catch (final IllegalArgumentException e) {
159    }
160   
161  2 try {
162  2 ctrl.createMock(List.class, args, methods);
163  0 fail("partial mocking on interface shouln't be allowed");
164    } catch (final IllegalArgumentException e) {
165    }
166   
167  2 try {
168  2 ctrl.createMock("myMock", List.class, methods);
169  0 fail("partial mocking on interface shouln't be allowed");
170    } catch (final IllegalArgumentException e) {
171    }
172   
173  2 try {
174  2 ctrl.createMock("myMock", List.class, args, methods);
175  0 fail("partial mocking on interface shouln't be allowed");
176    } catch (final IllegalArgumentException e) {
177    }
178    }
179   
 
180  6 toggle private void testList(final IMocksControl ctrl, final List<?> list) {
181  6 expect(list.size()).andReturn(3);
182  6 ctrl.replay();
183  6 assertEquals(3, list.size());
184  6 ctrl.verify();
185    }
186    }