Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
25   100   9   3,57
0   64   0,36   3,5
7     1,29  
2    
 
  PartialMockingTest       Line # 31 23 0% 7 5 82,1% 0.8214286
  PartialMockingTest.A       Line # 33 2 0% 2 2 50% 0.5
 
  (10)
 
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.Constructor;
23    import java.util.ArrayList;
24   
25    import org.easymock.ConstructorArgs;
26    import org.junit.Test;
27   
28    /**
29    * @author Henri Tremblay
30    */
 
31    public class PartialMockingTest {
32   
 
33    public static abstract class A {
34   
35    public String s;
36   
37    public int i;
38   
 
39  2 toggle protected A(final String s) {
40  2 this.s = s;
41    }
42   
 
43  0 toggle private A(final int i) {
44  0 this.i = i;
45    }
46   
47    protected abstract int foo();
48    }
49   
 
50  2 toggle @SuppressWarnings("unchecked")
51    @Test
52    public void testPartialMock_PublicConstructor() throws Exception {
53  2 final ArrayList<String> list = createMockBuilder(ArrayList.class).withConstructor(3).createMock();
54  2 list.add("test"); // shouldn't crash since constructor was called
55    }
56   
 
57  2 toggle @Test
58    public void testPartialMock_ProtectedConstructor() throws Exception {
59  2 final A a = createMockBuilder(A.class).withConstructor("test").createMock();
60  2 assertEquals("test", a.s); // make sure constructor was called
61   
62    // Check that abstract method is mocked by default
63  2 expect(a.foo()).andReturn(3);
64  2 replay(a);
65  2 assertEquals(3, a.foo());
66  2 verify(a);
67    }
68   
 
69  2 toggle @Test(expected = RuntimeException.class)
70    public void testPartialMock_ConstructorNotFound() throws Exception {
71  2 final Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE);
72  2 final ConstructorArgs constructorArgs = new ConstructorArgs(cstr, 2.0);
73  0 try {
74  0 createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs(2.0).createMock();
75    } catch (final RuntimeException e) {
76  0 assertEquals("Failed to find constructor for param types", e.getMessage());
77  0 throw e;
78    }
79    }
80   
 
81  2 toggle @Test(expected = IllegalArgumentException.class)
82    public void testPartialMock_InvalidParams() throws Exception {
83  2 final Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE);
84  2 final ConstructorArgs constructorArgs = new ConstructorArgs(cstr, "test");
85  0 createMockBuilder(ArrayList.class).withConstructor(Integer.TYPE).withArgs("test");
86    }
87   
 
88  2 toggle @Test(expected = RuntimeException.class)
89    public void testPartialMock_ExceptionInConstructor() throws Exception {
90  2 final Constructor<?> cstr = ArrayList.class.getConstructor(Integer.TYPE);
91  2 final ConstructorArgs constructorArgs = new ConstructorArgs(cstr, -5);
92  2 try {
93  2 createMockBuilder(ArrayList.class).withConstructor(-5).createMock();
94    } catch (final RuntimeException e) {
95  2 assertEquals("Failed to instantiate mock calling constructor: Exception in constructor", e
96    .getMessage());
97  2 throw e;
98    }
99    }
100    }