KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > classextension > samples > BasicClassMockTest


1 /*
2  * Copyright (c) 2003-2004 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

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 /**
13  * Example of how to use <code>MockClassControl</code>
14  */

15 public class BasicClassMockTest extends TestCase {
16
17     /**
18      * Our nice class that is allowed to print
19      */

20     public static class Document {
21
22         private Printer printer;
23
24         private String JavaDoc content;
25
26         public Document(Printer printer) {
27             this.printer = printer;
28         }
29
30         public String JavaDoc getContent() {
31             return content;
32         }
33
34         public void setContent(String JavaDoc content) {
35             this.content = content;
36         }
37
38         public void print() {
39             printer.print(content);
40         }
41     }
42
43     /**
44      * The terrible 3rd party class that is not an interface but that we realy
45      * want to mock.
46      */

47     public static abstract class Printer {
48         public abstract void print(String JavaDoc toPrint);
49     }
50
51     private MockControl ctrl;
52
53     private Printer printer;
54
55     private Document document;
56
57     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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(); // make sure Printer.print was called
80
}
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(); // make sure Printer.print was called
91
}
92 }
93
Popular Tags