1 3 package atest.jmock; 4 5 import junit.framework.AssertionFailedError; 6 import org.jmock.Mock; 7 import org.jmock.MockObjectTestCase; 8 import org.jmock.core.DynamicMockError; 9 10 11 public class ErrorMessagesAcceptanceTest extends MockObjectTestCase 12 { 13 private static final String MOCK_NAME = "MOCK_NAME"; 14 15 public void testUnexpectedCallAlsoShowsExpectedCalls() { 16 String arg1 = "arg1"; 17 String callID = "callID"; 18 Object arg2 = newDummy("arg2"); 19 Mock mock = mock(Types.WithTwoMethods.class, MOCK_NAME); 20 unregisterToVerify(mock); 22 mock.expects(once()).method("twoArgsReturnsInt").with(ANYTHING, ANYTHING) 23 .will(returnValue(1)).id(callID); 24 mock.expects(once()).method("twoArgsReturnsInt").with(eq(arg1), same(arg2)) 25 .after(callID) 26 .will(returnValue(1)); 27 28 try { 29 ((Types.WithTwoMethods)mock.proxy()).noArgsReturnsNothing(); 30 } 31 catch (DynamicMockError error) { 32 String errorMessage = error.getMessage(); 33 34 String causeOfError = "no match found"; 35 String expectedMethod1 = 36 "expected once: twoArgsReturnsInt( ANYTHING, ANYTHING ), returns <1> [" + callID + "]"; 37 String expectedMethod2 = 38 "expected once: twoArgsReturnsInt( eq(<" + arg1 + ">), same(<" + arg2 + ">) ), after " + callID + " (not invoked), returns <1>"; 39 40 assertStringContains("should contain mock name", 41 errorMessage, MOCK_NAME); 42 43 assertStringContains("should report cause of error", 44 errorMessage, causeOfError); 45 46 assertSubstringOrder("mock name should appear before cause of error", 47 errorMessage, MOCK_NAME, causeOfError); 48 49 assertStringContains("should report invokedMethod that caused error", 50 errorMessage, 51 "noArgsReturnsNothing()"); 52 53 assertStringContains("should report acceptable methods (#1)", 54 errorMessage, expectedMethod1); 55 56 assertStringContains("should report acceptable methods (#2)", 57 errorMessage, expectedMethod2); 58 59 assertSubstringOrder("should report acceptable methods in search order", 60 errorMessage, expectedMethod1, expectedMethod2); 61 62 return; 63 } 64 65 fail("expected DynamicMockError"); 66 } 67 68 public void testShowsNoExpectationsStringWhenNoExpectationsSet() { 69 Mock mock = mock(Types.WithTwoMethods.class); 70 try { 71 ((Types.WithTwoMethods)mock.proxy()).twoArgsReturnsInt("arg1", "arg2"); 72 } 73 catch (DynamicMockError error) { 74 String errorMessage = error.getMessage(); 75 76 assertStringContains("should report no expectations have been set", 77 errorMessage, "No expectations set"); 78 return; 79 } 80 81 fail("expected DynamicMockError"); 82 } 83 84 public void testShowsExpectationThatDoesNotVerify() { 85 String arg1 = "arg1"; 86 Object arg2 = new Object (); 87 String expectedMethod = 88 "expected once: twoArgsReturnsInt( eq(<" + arg1 + ">), same(<" + arg2 + ">) ), returns <1>"; 89 90 Mock mock = mock(Types.WithTwoMethods.class, MOCK_NAME); 91 unregisterToVerify(mock); 93 mock.expects(once()).method("twoArgsReturnsInt").with(eq(arg1), same(arg2)) 94 .will(returnValue(1)); 95 96 try { 97 mock.verify(); 98 } 99 catch (AssertionFailedError ex) { 100 String message = ex.getMessage(); 101 102 assertStringContains("error message should contain expected invokedMethod", 103 message, expectedMethod); 104 assertStringContains("error message should describe error", 105 message, "was not invoked"); 106 107 return; 108 } 109 110 fail("expected AssertionFailedError"); 111 } 112 113 public static void assertStringContains( String message, String string, String substring ) { 114 assertTrue(message + ": expected \"" + string + "\" to contain \"" + substring + "\"", 115 string.indexOf(substring) >= 0); 116 } 117 118 public static void assertSubstringOrder( String message, String string, 119 String earlierSubstring, String laterSubstring ) { 120 assertStringContains(message, string, earlierSubstring); 121 assertStringContains(message, string, laterSubstring); 122 123 int earlierIndex = string.indexOf(earlierSubstring); 124 int laterIndex = string.indexOf(laterSubstring); 125 126 assertTrue(message + ": expected \"" + earlierSubstring + "\" " + 127 "to appear before \"" + laterSubstring + "\" in \"" + string + "\"", 128 earlierIndex < laterIndex); 129 130 } 131 } 132 | Popular Tags |