1 5 package org.easymock.classextension.samples; 6 7 import junit.framework.TestCase; 8 9 import org.easymock.MockControl; 10 import org.easymock.classextension.MockClassControl; 11 12 15 public class BasicClassMockTest extends TestCase { 16 17 20 public static class Document { 21 22 private Printer printer; 23 24 private String content; 25 26 public Document(Printer printer) { 27 this.printer = printer; 28 } 29 30 public String getContent() { 31 return content; 32 } 33 34 public void setContent(String content) { 35 this.content = content; 36 } 37 38 public void print() { 39 printer.print(content); 40 } 41 } 42 43 47 public static abstract class Printer { 48 public abstract void print(String toPrint); 49 } 50 51 private MockControl ctrl; 52 53 private Printer printer; 54 55 private Document document; 56 57 protected void setUp() throws Exception { 58 super.setUp(); 59 ctrl = MockClassControl.createControl(Printer.class); 60 printer = (Printer) ctrl.getMock(); 61 document = new Document(printer); 62 } 63 64 protected void tearDown() throws Exception { 65 super.tearDown(); 66 ctrl = null; 67 printer = null; 68 document = null; 69 } 70 71 public void testPrintContent() { 72 printer.print("Hello world"); 73 ctrl.setVoidCallable(); 74 ctrl.replay(); 75 76 document.setContent("Hello world"); 77 document.print(); 78 79 ctrl.verify(); } 81 82 public void testPrintEmptyContent() { 83 printer.print(""); 84 ctrl.setVoidCallable(); 85 ctrl.replay(); 86 87 document.setContent(""); 88 document.print(); 89 90 ctrl.verify(); } 92 } 93 | Popular Tags |