1 5 package org.easymock.classextension.samples; 6 7 import java.lang.reflect.Method ; 8 9 import org.easymock.MockControl; 10 import org.easymock.classextension.MockClassControl; 11 12 import junit.framework.TestCase; 13 14 17 public class PartialClassMockTest extends TestCase { 18 19 public static class Rect { 20 21 private int x; 22 23 private int y; 24 25 public int getX() { 26 return x; 27 } 28 29 public void setX(int x) { 30 this.x = x; 31 } 32 33 public int getY() { 34 return y; 35 } 36 37 public void setY(int y) { 38 this.y = y; 39 } 40 41 public int getArea() { 42 return getX() * getY(); 43 } 44 } 45 46 private MockControl ctrl; 47 48 private Rect rect; 49 50 protected void setUp() throws Exception { 51 super.setUp(); 52 ctrl = MockClassControl.createControl(Rect.class, new Method [] { 53 Rect.class.getMethod("getX", null), 54 Rect.class.getMethod("getY", null) }); 55 rect = (Rect) ctrl.getMock(); 56 } 57 58 protected void tearDown() throws Exception { 59 super.tearDown(); 60 ctrl = null; 61 rect = null; 62 } 63 64 public void testGetArea() { 65 ctrl.expectAndReturn(rect.getX(), 4); 66 ctrl.expectAndReturn(rect.getY(), 5); 67 ctrl.replay(); 68 assertEquals(20, rect.getArea()); 69 ctrl.verify(); 70 } 71 } 72 | Popular Tags |