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.ThrowStub; 9 import org.jmock.expectation.AssertMo; 10 import test.jmock.core.DummyThrowable; 11 import test.jmock.core.testsupport.MethodFactory; 12 13 14 public class ThrowStubTest 15 extends TestCase 16 { 17 static final Throwable THROWABLE = new DummyThrowable(); 18 19 static final Class [] EXCEPTION_TYPES = {DummyThrowable.class}; 20 21 MethodFactory methodFactory; 22 Invocation invocation; 23 ThrowStub throwStub; 24 25 public void setUp() { 26 methodFactory = new MethodFactory(); 27 invocation = new Invocation("INVOKED-OBJECT", 28 methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, EXCEPTION_TYPES), 29 null); 30 throwStub = new ThrowStub(THROWABLE); 31 } 32 33 public void testThrowsThrowableObjectPassedToConstructorWhenInvoked() { 34 try { 35 throwStub.invoke(invocation); 36 } 37 catch (Throwable t) { 38 assertSame("Should be the same throwable", THROWABLE, t); 39 } 40 } 41 42 public void testIncludesDetailsOfThrowableInDescription() { 43 StringBuffer buffer = new StringBuffer (); 44 45 throwStub.describeTo(buffer); 46 47 String description = buffer.toString(); 48 49 assertTrue("contains class of thrown object in description", 50 description.indexOf(THROWABLE.toString()) >= 0); 51 assertTrue("contains 'throws' in description", 52 description.indexOf("throws") >= 0); 53 } 54 55 public static class ExpectedExceptionType1 extends Exception 56 { 57 } 58 59 public static class ExpectedExceptionType2 extends Exception 60 { 61 } 62 63 public void testThrowsAssertionFailedErrorIfTriesToThrowIncompatibleCheckedException() 64 throws Throwable { 65 Class [] expectedExceptionTypes = {ExpectedExceptionType1.class, ExpectedExceptionType2.class}; 66 Invocation incompatibleInvocation = new Invocation("INVOKED-OBJECT", 67 methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, expectedExceptionTypes), 68 null); 69 70 try { 71 throwStub.invoke(incompatibleInvocation); 72 } 73 catch (AssertionFailedError ex) { 74 String message = ex.getMessage(); 75 76 for (int i = 0; i < expectedExceptionTypes.length; i++) { 77 AssertMo.assertIncludes("should include name of expected exception types", 78 expectedExceptionTypes[i].getName(), message); 79 } 80 AssertMo.assertIncludes("should include name of thrown exception type", 81 THROWABLE.getClass().getName(), message); 82 return; 83 } 84 fail("should have failed"); 85 } 86 87 public void testGivesInformativeErrorMessageIfAttemptToThrowCheckedExceptionFromMethodWithNoExceptions() 88 throws Throwable { 89 Invocation incompatibleInvocation = new Invocation("INVOKED-OBJECT", 90 methodFactory.newMethod("methodName", MethodFactory.NO_ARGUMENTS, void.class, MethodFactory.NO_EXCEPTIONS), 91 null); 92 93 try { 94 throwStub.invoke(incompatibleInvocation); 95 } 96 catch (AssertionFailedError ex) { 97 String message = ex.getMessage(); 98 99 AssertMo.assertIncludes("should include name of thrown exception type", 100 THROWABLE.getClass().getName(), message); 101 AssertMo.assertIncludes("should describe that the method doesn't allow any exceptions", 102 "no exceptions", message); 103 return; 104 } 105 fail("should have failed"); 106 } 107 108 public void testDoesNotCheckTypeCompatiblityOfUncheckedExceptions() throws Throwable { 109 throwStub = new ThrowStub(new RuntimeException ()); 110 111 try { 112 throwStub.invoke(invocation); 113 } 114 catch (RuntimeException ex) { 115 return; 116 } 117 fail("should have thrown a RuntimeException"); 118 } 119 120 public void testDoesNotCheckTypeCompatiblityOfErrors() throws Throwable { 121 throwStub = new ThrowStub(new Error ()); 122 123 try { 124 throwStub.invoke(invocation); 125 } 126 catch (AssertionFailedError err) { 127 throw err; 128 } 129 catch (Error ex) { 130 return; 131 } 132 fail("should have thrown an Error"); 133 } 134 135 public void testSetsStackTraceWhenExceptionIsThrown() { 136 try { 137 throwStub.invoke(invocation); 138 } 139 catch (Throwable t) { 140 StackTraceElement [] stackTrace = t.getStackTrace(); 141 142 assertEquals("thrown from ThrowStub object", 143 throwStub.getClass().getName(), stackTrace[0].getClassName()); 144 } 145 } 146 } 147 | Popular Tags |