KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
8
9 import junit.framework.AssertionFailedError;
10 import junit.framework.TestCase;
11
12 import org.easymock.MockControl;
13 import org.easymock.internal.ReplayState;
14
15 public class UsageVerifyTest extends TestCase {
16     private MockControl<IMethods> control;
17
18     private IMethods mock;
19
20     protected void setUp() {
21         control = MockControl.createControl(IMethods.class);
22         mock = control.getMock();
23     }
24
25     public void testTwoReturns() {
26         mock.throwsNothing(true);
27         control.setReturnValue("Test");
28         control.setReturnValue("Test2");
29
30         control.replay();
31
32         assertEquals("Test", mock.throwsNothing(true));
33
34         boolean failed = true;
35
36         try {
37             control.verify();
38             failed = false;
39         } catch (AssertionFailedError expected) {
40             assertEquals("\n Expectation failure on verify:"
41                     + "\n throwsNothing(true): expected: 2, actual: 1",
42                     expected.getMessage());
43             assertTrue("stack trace must be filled in", Util.getStackTrace(
44                     expected).indexOf(ReplayState.class.getName()) == -1);
45         }
46
47         if (!failed)
48             fail("AssertionFailedError expected");
49
50         assertEquals("Test2", mock.throwsNothing(true));
51
52         control.verify();
53
54         try {
55             mock.throwsNothing(true);
56             fail("AssertionFailedError expected");
57         } catch (AssertionFailedError expected) {
58             assertEquals("\n Unexpected method call throwsNothing(true):"
59                     + "\n throwsNothing(true): expected: 2, actual: 3",
60                     expected.getMessage());
61         }
62     }
63
64     public void testAtLeastTwoReturns() {
65         mock.throwsNothing(true);
66         control.setReturnValue("Test");
67         control.setReturnValue("Test2", MockControl.ONE_OR_MORE);
68
69         control.replay();
70
71         assertEquals("Test", mock.throwsNothing(true));
72
73         try {
74             control.verify();
75             fail("AssertionFailedError expected");
76         } catch (AssertionFailedError expected) {
77
78             assertEquals(
79                     "\n Expectation failure on verify:"
80                             + "\n throwsNothing(true): expected: at least 2, actual: 1",
81                     expected.getMessage());
82         }
83
84         assertEquals("Test2", mock.throwsNothing(true));
85         assertEquals("Test2", mock.throwsNothing(true));
86
87         control.verify();
88     }
89
90     public void testTwoThrows() throws IOException JavaDoc {
91         mock.throwsIOException(0);
92         control.setThrowable(new IOException JavaDoc());
93         control.setThrowable(new IOException JavaDoc());
94         mock.throwsIOException(1);
95         control.setThrowable(new IOException JavaDoc());
96
97         control.replay();
98
99         try {
100             mock.throwsIOException(0);
101             fail("IOException expected");
102         } catch (IOException JavaDoc expected) {
103         }
104
105         try {
106             control.verify();
107             fail("AssertionFailedError expected");
108         } catch (AssertionFailedError expected) {
109             assertEquals("\n Expectation failure on verify:"
110                     + "\n throwsIOException(0): expected: 2, actual: 1"
111                     + "\n throwsIOException(1): expected: 1, actual: 0",
112                     expected.getMessage());
113         }
114
115         try {
116             mock.throwsIOException(0);
117             fail("IOException expected");
118         } catch (IOException JavaDoc expected) {
119         }
120
121         try {
122             control.verify();
123             fail("AssertionFailedError expected");
124         } catch (AssertionFailedError expected) {
125             assertEquals("\n Expectation failure on verify:"
126                     + "\n throwsIOException(1): expected: 1, actual: 0",
127                     expected.getMessage());
128         }
129
130         try {
131             mock.throwsIOException(1);
132             fail("IOException expected");
133         } catch (IOException JavaDoc expected) {
134         }
135
136         control.verify();
137
138         try {
139             mock.throwsIOException(0);
140             fail("AssertionFailedError expected");
141         } catch (AssertionFailedError expected) {
142             assertEquals("\n Unexpected method call throwsIOException(0):"
143                     + "\n throwsIOException(0): expected: 2, actual: 3",
144                     expected.getMessage());
145         }
146     }
147 }
148
Popular Tags