KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > classextension > tests > MockClassControlTest


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.tests;
6
7 import junit.framework.TestCase;
8
9 import org.easymock.MockControl;
10 import org.easymock.classextension.MockClassControl;
11
12 /**
13  * Tests of MockClassControl basic functionnalities
14  */

15 public class MockClassControlTest extends TestCase {
16
17     /**
18      * Class that will be mocked. The methods defined in it are there just to
19      * make sure they are correctly overloaded by the mock.
20      */

21     public static class ClassToMock {
22
23     }
24
25     /**
26      * Same as ClassToMock except that the methods with a standard behavior
27      * provided by the mock are overloaded. We expect the standard behavior to
28      * still be called.
29      */

30     public static class ClassToMockWithOverload {
31
32         public boolean equals(Object JavaDoc o) {
33             return false;
34         }
35
36         public int hashCode() {
37             return -1;
38         }
39
40         public String JavaDoc toString() {
41             return "super";
42         }
43     }
44
45     private MockControl ctrl;
46
47     private Object JavaDoc mock;
48
49     public void setUp() throws Exception JavaDoc {
50         super.setUp();
51     }
52
53     public void tearDown() throws Exception JavaDoc {
54         super.tearDown();
55         ctrl = null;
56         mock = null;
57     }
58
59     private void initMock(Class JavaDoc toMock) {
60         ctrl = MockClassControl.createControl(toMock);
61         mock = ctrl.getMock();
62     }
63
64     public void testEquals() {
65         testEquals(ClassToMock.class);
66     }
67
68     public void testEquals_WithOverload() {
69         testEquals(ClassToMockWithOverload.class);
70     }
71
72     /**
73      * Make sure that a mock is equals to itself
74      */

75     public void testEquals(Class JavaDoc toMock) {
76         initMock(toMock);
77         assertEquals(mock, mock);
78         ctrl.replay();
79         assertEquals(mock, mock);
80     }
81
82     public void testHashCode() {
83         testHashCode(ClassToMock.class);
84     }
85
86     public void testHashCode_WithOverload() {
87         testHashCode(ClassToMockWithOverload.class);
88     }
89
90     /**
91      * Make sure the hashCode doesn't need to be recorded and that it stays the
92      * same after the replay
93      */

94     private void testHashCode(Class JavaDoc toMock) {
95         initMock(toMock);
96         int code = mock.hashCode();
97         ctrl.replay();
98         assertEquals(code, mock.hashCode());
99     }
100
101     public void testToString() {
102         testToString(ClassToMock.class);
103     }
104
105     public void testToString_WithOverload() {
106         testToString(ClassToMockWithOverload.class);
107     }
108
109     /**
110      * Check that the toString is the EasyMock one giving the mocked class
111      */

112     private void testToString(Class JavaDoc toMock) {
113         initMock(toMock);
114         String JavaDoc expectedValue = "EasyMock for " + toMock.toString();
115         assertEquals(expectedValue, mock.toString());
116         ctrl.replay();
117         assertEquals(expectedValue, mock.toString());
118     }
119 }
120
Popular Tags