KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2001-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 org.easymock.MockControl;
8 import org.easymock.classextension.MockClassControl;
9
10 import junit.framework.TestCase;
11
12 public class ConstructorTest extends TestCase {
13
14     public static class FooClass {
15         public void foo() {
16             // Since it's always mocked, it should never be called
17
fail();
18         }
19     }
20
21     public static class EmptyConstructorClass extends FooClass {
22     }
23
24     public static class ConstructorCallingPublicMethodClass extends FooClass {
25
26         public ConstructorCallingPublicMethodClass() {
27             foo();
28         }
29     }
30
31     private void testConstructor(Class JavaDoc mockedClass) {
32         MockControl ctrl = MockClassControl.createControl(mockedClass);
33         FooClass mock = (FooClass) ctrl.getMock();
34         assertTrue(mockedClass.isAssignableFrom(mock.getClass()));
35         mock.foo();
36         ctrl.setVoidCallable();
37         ctrl.replay();
38         mock.foo();
39         ctrl.verify();
40     }
41
42     /**
43      * Test if a class with an empty constructor is mocked correctly.
44      */

45     public void testEmptyConstructor() {
46         testConstructor(EmptyConstructorClass.class);
47     }
48
49     /**
50      * Test that a constructor calling a mocked method (in this case a public
51      * one) is mocked correctly. The expected behavior is that the mocked method
52      * won't be called and just be ignored
53      */

54     public void testConstructorCallingPublicMethod() {
55         testConstructor(ConstructorCallingPublicMethodClass.class);
56     }
57 }
58
Popular Tags