Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
34   149   19   1,89
0   99   0,56   9
18     1,06  
2    
 
  MockingTest       Line # 36 32 0% 17 1 97,9% 0.9791667
  MockingTest.ClassToMock       Line # 38 2 0% 2 2 50% 0.5
 
  (20)
 
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 java.lang.reflect.Method;
23    import java.util.List;
24   
25    import org.easymock.internal.ClassExtensionHelper;
26    import org.easymock.internal.MocksControl;
27    import org.easymock.internal.MocksControl.MockType;
28    import org.junit.Test;
29   
30    /**
31    * Test all kind of mocking making sure the partial mocking and interface works
32    * and that to correct behavior is given.
33    *
34    * @author Henri Tremblay
35    */
 
36    public class MockingTest {
37   
 
38    public static class ClassToMock {
 
39  12 toggle public int foo() {
40  12 return 10;
41    }
42   
 
43  0 toggle public int method() {
44  0 return 20;
45    }
46    }
47   
48    /**
49    * Make sure one mock is not interacting with another
50    */
 
51  2 toggle @Test
52    public void testTwoMocks() {
53  2 final ClassToMock transition1 = createMock(ClassToMock.class);
54  2 final ClassToMock transition2 = createMock(ClassToMock.class);
55   
56    // Should have two different callbacks
57  2 assertNotSame(ClassExtensionHelper.getInterceptor(transition2), ClassExtensionHelper
58    .getInterceptor(transition1));
59   
60  2 transition2.foo();
61  2 transition1.foo();
62    }
63   
 
64  2 toggle @Test
65    public void testInterfaceMocking() {
66  2 checkInterfaceMock(createMock(List.class), MockType.DEFAULT);
67    }
68   
 
69  2 toggle @Test
70    public void testNiceInterfaceMocking() {
71  2 checkInterfaceMock(createNiceMock(List.class), MockType.NICE);
72    }
73   
 
74  2 toggle @Test
75    public void testStrictInterfaceMocking() {
76  2 checkInterfaceMock(createStrictMock(List.class), MockType.STRICT);
77    }
78   
 
79  2 toggle @Test
80    public void testClassMocking() {
81  2 checkClassMocking(createMock(ClassToMock.class), MockType.DEFAULT);
82    }
83   
 
84  2 toggle @Test
85    public void testStrictClassMocking() {
86  2 checkClassMocking(createStrictMock(ClassToMock.class), MockType.STRICT);
87    }
88   
 
89  2 toggle @Test
90    public void testNiceClassMocking() {
91  2 checkClassMocking(createNiceMock(ClassToMock.class), MockType.NICE);
92    }
93   
 
94  6 toggle private void checkInterfaceMock(final Object mock, final MockType behavior) {
95  6 checkBehavior(mock, behavior);
96    }
97   
 
98  2 toggle @SuppressWarnings("deprecation")
99    @Test
100    public void testPartialClassMocking() {
101  2 final ClassToMock mock = createMock(ClassToMock.class, getMethod());
102  2 checkPartialClassMocking(mock, MockType.DEFAULT);
103    }
104   
 
105  2 toggle @SuppressWarnings("deprecation")
106    @Test
107    public void testStrictPartialClassMocking() {
108  2 final ClassToMock mock = createStrictMock(ClassToMock.class, getMethod());
109  2 checkPartialClassMocking(mock, MockType.STRICT);
110    }
111   
 
112  2 toggle @SuppressWarnings("deprecation")
113    @Test
114    public void testNicePartialClassMocking() {
115  2 final ClassToMock mock = createNiceMock(ClassToMock.class, getMethod());
116  2 checkPartialClassMocking(mock, MockType.NICE);
117    }
118   
 
119  6 toggle private void checkPartialClassMocking(final ClassToMock mock, final MockType behavior) {
120  6 checkClassMocking(mock, behavior);
121  6 assertEquals(10, mock.foo());
122  6 expect(mock.method()).andReturn(30);
123  6 replay(mock);
124  6 assertEquals(10, mock.foo());
125  6 assertEquals(30, mock.method());
126  6 verify(mock);
127    }
128   
 
129  12 toggle private void checkClassMocking(final Object mock, final MockType behavior) {
130  12 checkBehavior(mock, behavior);
131    }
132   
 
133  18 toggle private void checkBehavior(final Object mock, final MockType behavior) {
134  18 assertEquals(behavior, extractBehavior(mock));
135    }
136   
 
137  18 toggle private MockType extractBehavior(final Object mock) {
138  18 final MocksControl ctrl = ClassExtensionHelper.getControl(mock);
139  18 return ctrl.getType();
140    }
141   
 
142  6 toggle private Method[] getMethod() {
143  6 try {
144  6 return new Method[] { ClassToMock.class.getDeclaredMethod("method", (Class[]) null) };
145    } catch (final NoSuchMethodException e) {
146  0 throw new RuntimeException(e.getMessage());
147    }
148    }
149    }