1 5 package org.easymock.classextension.tests; 6 7 import java.lang.reflect.Field ; 8 import java.lang.reflect.Method ; 9 import java.util.List ; 10 11 import junit.framework.TestCase; 12 13 import org.easymock.MockControl; 14 import org.easymock.classextension.MockClassControl; 15 import org.easymock.internal.IBehavior; 16 import org.easymock.internal.NiceBehavior; 17 import org.easymock.internal.OrderedBehavior; 18 import org.easymock.internal.UnorderedBehavior; 19 20 24 public class MockingTest extends TestCase { 25 26 public static class ClassToMock { 27 public int foo() { 28 return 10; 29 } 30 31 public int method() { 32 return 20; 33 } 34 } 35 36 39 protected void setUp() throws Exception { 40 super.setUp(); 41 } 42 43 46 protected void tearDown() throws Exception { 47 super.tearDown(); 48 } 49 50 53 public void testTwoMocks() { 54 MockControl transition1Control = 55 MockClassControl.createControl(ClassToMock.class); 56 ClassToMock transition1 = (ClassToMock) transition1Control.getMock(); 57 MockControl transition2Control = 58 MockClassControl.createControl(ClassToMock.class); 59 ClassToMock transition2 = (ClassToMock) transition2Control.getMock(); 60 61 assertNotSame(transition2.getClass(), transition1.getClass()); 63 64 transition2.foo(); 65 transition1.foo(); 66 } 67 68 public void testInterfaceMocking() { 69 MockControl ctrl = MockClassControl.createControl(List .class); 70 checkInterfaceMock(ctrl, UnorderedBehavior.class); 71 } 72 73 public void testNiceInterfaceMocking() { 74 MockControl ctrl = MockClassControl.createNiceControl(List .class); 75 checkInterfaceMock(ctrl, NiceBehavior.class); 76 } 77 78 public void testStrictInterfaceMocking() { 79 MockControl ctrl = MockClassControl.createStrictControl(List .class); 80 checkInterfaceMock(ctrl, OrderedBehavior.class); 81 } 82 83 public void testClassMocking() { 84 MockControl ctrl = MockClassControl.createControl(ClassToMock.class); 85 checkClassMocking(ctrl, UnorderedBehavior.class); 86 } 87 88 public void testStrictClassMocking() { 89 MockControl ctrl = MockClassControl 90 .createStrictControl(ClassToMock.class); 91 checkClassMocking(ctrl, OrderedBehavior.class); 92 } 93 94 public void testNiceClassMocking() { 95 MockControl ctrl = MockClassControl 96 .createNiceControl(ClassToMock.class); 97 checkClassMocking(ctrl, NiceBehavior.class); 98 } 99 100 private void checkInterfaceMock(MockControl ctrl, Class behavior) { 101 assertTrue(ctrl.getMock() instanceof List ); 102 checkBehavior(ctrl, behavior); 103 } 104 105 public void testPartialClassMocking() { 106 MockControl ctrl = MockClassControl.createControl(ClassToMock.class, 107 getMethod()); 108 checkPartialClassMocking(ctrl, UnorderedBehavior.class); 109 } 110 111 public void testStrictPartialClassMocking() { 112 MockControl ctrl = MockClassControl.createStrictControl( 113 ClassToMock.class, getMethod()); 114 checkPartialClassMocking(ctrl, OrderedBehavior.class); 115 } 116 117 public void testNicePartialClassMocking() { 118 MockControl ctrl = MockClassControl.createNiceControl( 119 ClassToMock.class, getMethod()); 120 checkPartialClassMocking(ctrl, NiceBehavior.class); 121 } 122 123 public void testDeprecatedClassMocking() { 124 MockControl ctrl = MockClassControl.createControl(ClassToMock.class, 125 null, null); 126 checkClassMocking(ctrl, UnorderedBehavior.class); 127 } 128 129 public void testDeprecatedStrictClassMocking() { 130 MockControl ctrl = MockClassControl.createStrictControl( 131 ClassToMock.class, null, null); 132 checkClassMocking(ctrl, OrderedBehavior.class); 133 } 134 135 public void testDeprecatedNiceClassMocking() { 136 MockControl ctrl = MockClassControl.createNiceControl( 137 ClassToMock.class, null, null); 138 checkClassMocking(ctrl, NiceBehavior.class); 139 } 140 141 public void testDeprecatedPartialClassMocking() { 142 MockControl ctrl = MockClassControl.createControl(ClassToMock.class, 143 null, null, getMethod()); 144 checkPartialClassMocking(ctrl, UnorderedBehavior.class); 145 } 146 147 public void testDeprecatedStrictPartialClassMocking() { 148 MockControl ctrl = MockClassControl.createStrictControl( 149 ClassToMock.class, null, null, getMethod()); 150 checkPartialClassMocking(ctrl, OrderedBehavior.class); 151 } 152 153 public void testDeprecatedNicePartialClassMocking() { 154 MockControl ctrl = MockClassControl.createNiceControl( 155 ClassToMock.class, null, null, getMethod()); 156 checkPartialClassMocking(ctrl, NiceBehavior.class); 157 } 158 159 private void checkPartialClassMocking(MockControl ctrl, Class behavior) { 160 checkClassMocking(ctrl, behavior); 161 ClassToMock mock = (ClassToMock) ctrl.getMock(); 162 assertEquals(10, mock.foo()); 163 ctrl.expectAndReturn(mock.method(), 30); 164 ctrl.replay(); 165 assertEquals(10, mock.foo()); 166 assertEquals(30, mock.method()); 167 ctrl.verify(); 168 } 169 170 private void checkClassMocking(MockControl ctrl, Class behavior) { 171 assertTrue(ctrl instanceof MockClassControl); 172 checkBehavior(ctrl, behavior); 173 assertTrue(ctrl.getMock() instanceof ClassToMock); 174 } 175 176 private void checkBehavior(MockControl ctrl, Class behavior) { 177 assertEquals(behavior, extractBehavior(ctrl).getClass()); 178 } 179 180 private IBehavior extractBehavior(MockControl ctrl) { 181 try { 182 Field field = MockControl.class.getDeclaredField("behavior"); 183 field.setAccessible(true); 184 IBehavior behavior = (IBehavior) field.get(ctrl); 185 field.setAccessible(false); 186 return behavior; 187 } catch (Exception e) { 188 throw new RuntimeException ("Can't find behavior field"); 189 } 190 } 191 192 private Method [] getMethod() { 193 try { 194 return new Method [] { ClassToMock.class.getDeclaredMethod("method", 195 null) }; 196 } catch (NoSuchMethodException e) { 197 throw new RuntimeException (e.getMessage()); 198 } 199 } 200 } 201 | Popular Tags |