KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > tests > UsageDefaultReturnValueTest


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

5 package org.easymock.tests;
6
7 import junit.framework.TestCase;
8
9 import org.easymock.MockControl;
10
11 public class UsageDefaultReturnValueTest extends TestCase {
12     MockControl<IMethods> control;
13
14     IMethods mock;
15
16     protected void setUp() {
17         control = MockControl.createControl(IMethods.class);
18         mock = control.getMock();
19     }
20
21     public void testDefaultReturnValue() {
22         mock.threeArgumentMethod(7, "", "test");
23         control.setReturnValue("test", 1);
24
25         mock.threeArgumentMethod(8, null, "test2");
26         control.setReturnValue("test2", 1);
27
28         Object JavaDoc defaultValue = new Object JavaDoc();
29         control.setDefaultReturnValue(defaultValue);
30
31         control.replay();
32         assertEquals("test", mock.threeArgumentMethod(7, "", "test"));
33         assertEquals("test2", mock.threeArgumentMethod(8, null, "test2"));
34         assertSame(defaultValue, mock.threeArgumentMethod(7, new Object JavaDoc(),
35                 "test"));
36         assertSame(defaultValue, mock.threeArgumentMethod(7, "", "test"));
37         assertSame(defaultValue, mock.threeArgumentMethod(8, null, "test"));
38         assertSame(defaultValue, mock.threeArgumentMethod(9, null, "test"));
39
40         control.verify();
41     }
42
43     public void testDefaultVoidCallable() {
44
45         mock.twoArgumentMethod(1, 2);
46         control.setDefaultVoidCallable();
47
48         mock.twoArgumentMethod(1, 1);
49         RuntimeException JavaDoc expected = new RuntimeException JavaDoc();
50         control.setThrowable(expected);
51
52         control.replay();
53         mock.twoArgumentMethod(2, 1);
54         mock.twoArgumentMethod(1, 2);
55         mock.twoArgumentMethod(3, 7);
56
57         try {
58             mock.twoArgumentMethod(1, 1);
59             fail("RuntimeException expected");
60         } catch (RuntimeException JavaDoc actual) {
61             assertSame(expected, actual);
62         }
63
64     }
65
66     public void testDefaultThrowable() {
67         mock.twoArgumentMethod(1, 2);
68         control.setVoidCallable();
69         mock.twoArgumentMethod(1, 1);
70         control.setVoidCallable();
71
72         RuntimeException JavaDoc expected = new RuntimeException JavaDoc();
73         control.setDefaultThrowable(expected);
74
75         control.replay();
76
77         mock.twoArgumentMethod(1, 2);
78         mock.twoArgumentMethod(1, 1);
79         try {
80             mock.twoArgumentMethod(2, 1);
81             fail("RuntimeException expected");
82         } catch (RuntimeException JavaDoc actual) {
83             assertSame(expected, actual);
84         }
85     }
86
87     public void testDefaultReturnValueBoolean() {
88         mock.booleanReturningMethod(12);
89         control.setReturnValue(true);
90         control.setDefaultReturnValue(false);
91
92         control.replay();
93
94         assertFalse(mock.booleanReturningMethod(11));
95         assertTrue(mock.booleanReturningMethod(12));
96         assertFalse(mock.booleanReturningMethod(13));
97
98         control.verify();
99     }
100 }
101
Popular Tags