1 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 RESULT = "result"; 17 18 MethodFactory methodFactory; 19 Object invokedObject; 20 Class invokedObjectClass; 21 Invocation invocation; 22 ReturnStub returnStub; 23 24 public void setUp() { 25 methodFactory = new MethodFactory(); 26 27 invokedObject = "INVOKED-OBJECT"; 28 invokedObjectClass = Void .class; 29 30 returnStub = new ReturnStub(RESULT); 31 } 32 33 public void testReturnsValuePassedToConstructor() throws Throwable { 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 buffer = new StringBuffer (); 42 43 returnStub.describeTo(buffer); 44 45 String 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 { 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 { 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 { 85 invocation = new Invocation(invokedObject, methodFactory.newMethodReturning(String .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 { 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 )null), ex.getMessage()); 104 return; 105 } 106 fail("should have failed"); 107 } 108 } 109 | Popular Tags |