KickJava   Java API By Example, From Geeks To Geeks.

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


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.TestCase;
10
11 import org.easymock.MockControl;
12
13 public class RecordStateInvalidThrowableTest extends TestCase {
14
15     MockControl<IMethods> control;
16
17     IMethods mock;
18
19     private class CheckedException extends Exception JavaDoc {
20     }
21
22     protected void setUp() {
23         control = MockControl.createControl(IMethods.class);
24         mock = control.getMock();
25     }
26
27     public void testThrowNull() {
28         mock.throwsNothing(false);
29         try {
30             control.setThrowable(null);
31             fail("NullPointerException expected");
32         } catch (NullPointerException JavaDoc expected) {
33             assertEquals("null cannot be thrown", expected.getMessage());
34         }
35
36     }
37
38     public void testThrowCheckedExceptionWhereNoCheckedExceptionIsThrown() {
39         mock.throwsNothing(false);
40         try {
41             control.setThrowable(new CheckedException());
42             fail("IllegalArgumentException expected");
43         } catch (IllegalArgumentException JavaDoc expected) {
44             assertEquals("last method called on mock cannot throw "
45                     + CheckedException.class.getName(), expected.getMessage());
46         }
47     }
48
49     public void testThrowWrongCheckedException() throws IOException JavaDoc {
50         mock.throwsIOException(0);
51         try {
52             control.setThrowable(new CheckedException());
53             fail("IllegalArgumentException expected");
54         } catch (IllegalArgumentException JavaDoc expected) {
55             assertEquals("last method called on mock cannot throw "
56                     + CheckedException.class.getName(), expected.getMessage());
57         }
58     }
59
60     public void testThrowAfterThrowable() throws IOException JavaDoc {
61         mock.throwsIOException(0);
62         control.setThrowable(new IOException JavaDoc(), MockControl.ONE_OR_MORE);
63         try {
64             control.setThrowable(new IOException JavaDoc());
65             fail("IllegalStateException expected");
66         } catch (IllegalStateException JavaDoc expected) {
67             assertEquals(
68                     "last method called on mock already has a non-fixed count set.",
69                     expected.getMessage());
70         }
71     }
72 }
73
Popular Tags