KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > stub > ReturnStubTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.core.stub;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.stub.ReturnStub;
9 import org.jmock.expectation.AssertMo;
10 import test.jmock.core.testsupport.MethodFactory;
11
12
13 public class ReturnStubTest
14         extends TestCase
15 {
16     static final String JavaDoc RESULT = "result";
17
18     MethodFactory methodFactory;
19     Object JavaDoc invokedObject;
20     Class JavaDoc invokedObjectClass;
21     Invocation invocation;
22     ReturnStub returnStub;
23
24     public void setUp() {
25         methodFactory = new MethodFactory();
26
27         invokedObject = "INVOKED-OBJECT";
28         invokedObjectClass = Void JavaDoc.class;
29
30         returnStub = new ReturnStub(RESULT);
31     }
32
33     public void testReturnsValuePassedToConstructor() throws Throwable JavaDoc {
34         invocation = new Invocation(invokedObject, methodFactory.newMethodReturning(RESULT.getClass()), null);
35
36         assertSame("Should be the same result object",
37                    RESULT, returnStub.invoke(invocation));
38     }
39
40     public void testIncludesValueInDescription() {
41         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
42
43         returnStub.describeTo(buffer);
44
45         String JavaDoc description = buffer.toString();
46
47         assertTrue("contains result in description",
48                    description.indexOf(RESULT.toString()) >= 0);
49         assertTrue("contains 'returns' in description",
50                    description.indexOf("returns") >= 0);
51     }
52
53     public void testThrowsAssertionFailedErrorIfTriesToReturnValueOfIncompatibleType()
54             throws Throwable JavaDoc {
55         invocation = new Invocation(invokedObject, methodFactory.newMethodReturning(int.class), null);
56
57         try {
58             returnStub.invoke(invocation);
59         }
60         catch (AssertionFailedError ex) {
61             AssertMo.assertIncludes("expected return type", invocation.invokedMethod.getReturnType().toString(), ex.getMessage());
62             AssertMo.assertIncludes("returned value type", RESULT.getClass().toString(), ex.getMessage());
63             return;
64         }
65         fail("should have failed");
66     }
67
68     public void testThrowsAssertionFailedErrorIfTriesToReturnValueFromVoidMethod()
69             throws Throwable JavaDoc {
70         invocation = new Invocation(invokedObject, methodFactory.newMethodReturning(void.class), null);
71
72         try {
73             returnStub.invoke(invocation);
74         }
75         catch (AssertionFailedError ex) {
76             AssertMo.assertIncludes("should describe error",
77                                     "tried to return a value from a void method", ex.getMessage());
78             return;
79         }
80         fail("should have failed");
81     }
82
83     public void testCanReturnNullReference()
84             throws Throwable JavaDoc {
85         invocation = new Invocation(invokedObject, methodFactory.newMethodReturning(String JavaDoc.class), null);
86
87         returnStub = new ReturnStub(null);
88
89         assertNull("should return null", returnStub.invoke(invocation));
90     }
91
92     public void testThrowsAssertionFailedErrorIfTriesToReturnNullFromMethodWithPrimitiveReturnType()
93             throws Throwable JavaDoc {
94         invocation = new Invocation(invokedObject, methodFactory.newMethodReturning(int.class), null);
95
96         returnStub = new ReturnStub(null);
97
98         try {
99             returnStub.invoke(invocation);
100         }
101         catch (AssertionFailedError ex) {
102             AssertMo.assertIncludes("expected return type", invocation.invokedMethod.getReturnType().toString(), ex.getMessage());
103             AssertMo.assertIncludes("null", String.valueOf((Object JavaDoc)null), ex.getMessage());
104             return;
105         }
106         fail("should have failed");
107     }
108 }
109
Popular Tags