1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic;
3
4 import junit.framework.AssertionFailedError;
5 import junit.framework.TestCase;
6 import org.jmock.dynamic.support.MockInvocationDispatcher;
7 import org.jmock.dynamic.support.MockInvokable;
8 import org.jmock.expectation.AssertMo;
9
10
11 public class CoreMockTest extends TestCase {
12 private static final String MOCK_NAME = "Test coreMock";
13
14 private DummyInterface proxy;
15 private CoreMock coreMock;
16 private MockInvocationDispatcher mockDispatcher = new MockInvocationDispatcher();
17 private MockInvokable mockInvokable = new MockInvokable();
18
19 public CoreMockTest(String name) {
20 super(name);
21 }
22
23 public void setUp() {
24 coreMock = new CoreMock(DummyInterface.class, MOCK_NAME, mockDispatcher);
25
26 try {
27 proxy = (DummyInterface) coreMock.proxy();
28 } catch (ClassCastException ex) {
29 fail("proxy is not of expected interface type");
30 }
31 }
32
33 public void testMockAnnotatesAssertionFailedError()
34 throws Throwable {
35 final String originalMessage = "original message";
36
37 Object arg = new AssertionFailedError(originalMessage);
38 mockDispatcher.dispatchResult = arg;
39
40 try {
41 proxy.noArgVoidMethod();
42 } catch (AssertionFailedError err) {
43 AssertMo.assertIncludes("should contain original message", originalMessage, err.getMessage());
44 AssertMo.assertIncludes("should contain coreMock name", MOCK_NAME, err.getMessage());
45 }
46 }
47
48 public void testProxyReturnsConfiguredResult() throws Throwable {
49 final String RESULT = "configured result";
50
51 mockDispatcher.dispatchResult = RESULT;
52
53 assertSame("result is returned by coreMock", RESULT, proxy.oneArgMethod("arg"));
54 }
55
56 public void testExceptionsPropagatedThroughProxy() throws Throwable {
57 final Throwable throwable = new DummyThrowable();
58
59 mockDispatcher.dispatchThrowable = throwable;
60
61 try {
62 proxy.noArgVoidMethod();
63 } catch (Throwable ex) {
64 assertSame("exception is caught by coreMock", throwable, ex);
65 return;
66 }
67 fail("Should have thrown exception");
68 }
69
70 public void testMockVerifies() throws Exception {
71 mockDispatcher.verifyCalls.setExpected(1);
72
73 coreMock.verify();
74
75 // Can't use Verifier as we are verifying "verify"
76 mockDispatcher.verifyExpectations();
77 }
78
79 public void testProxyEquality() throws Exception {
80 mockDispatcher.dispatchResult = new Boolean(false);
81
82 mockDispatcher.dispatchInvocation.setExpectNothing();
83
84 assertTrue("Proxy equality is implemented directly", proxy.equals(proxy));
85 mockDispatcher.verifyExpectations();
86 }
87
88 public void testProxyInequality() throws Exception {
89 mockDispatcher.dispatchResult = new Boolean(false);
90
91 mockDispatcher.dispatchInvocation.setExpected(
92 new Invocation("equals", new Class[]{Object.class}, boolean.class,
93 new Object[]{"not a proxy"}));
94
95 assertFalse("Should handle proxy inequality by calling through", proxy.equals("not a proxy"));
96 mockDispatcher.verifyExpectations();
97 }
98
99 public void testProxyEqualityWithNull() throws Exception {
100 mockDispatcher.dispatchResult = new Boolean(true);
101 mockDispatcher.dispatchInvocation.setExpected(
102 new Invocation("equals", new Class[]{Object.class}, boolean.class,
103 new Object[]{null}));
104
105 assertTrue("Proxy should handle null equality", proxy.equals(null));
106 mockDispatcher.verifyExpectations();
107 }
108
109 public void testCallingGetMockNameOnProxyReturnsNameOfUnderlyingMock() {
110 mockDispatcher.dispatchInvocation.setExpectNothing();
111
112 assertEquals("proxy.getMockName() returns name of underlying mock", MOCK_NAME, proxy.getMockName());
113
114 mockDispatcher.verifyExpectations();
115 }
116
117 public void testGeneratesMockNameFromInterfaceNameIfNoNameSpecified() throws Exception {
118 assertEquals("mockString", CoreMock.mockNameFromClass(String.class));
119 }
120
121 public void testResultOfToStringContainsName() {
122 AssertMo.assertIncludes("result of toString() should include name", MOCK_NAME, coreMock.toString());
123 }
124
125 public void testProxyToString() throws Exception {
126 assertEquals("Should get a coreMock name without touching the underlying coreMock", MOCK_NAME, DynamicUtil.proxyToString(proxy));
127 coreMock.verify(); // should not fail on a proxyToString call
128 }
129
130 public void testAddAnInvokable() {
131 mockDispatcher.addInvokable.setExpected(mockInvokable);
132
133 coreMock.add(mockInvokable);
134
135 mockDispatcher.verifyExpectations();
136 }
137
138
139 public void testReset() {
140 mockDispatcher.clearCalls.setExpected(1);
141
142 coreMock.reset();
143
144 mockDispatcher.verifyExpectations();
145 }
146
147 public void testVerifyFailuresIncludeMockName() {
148 mockDispatcher.verifyFailure = new AssertionFailedError("verify failure");
149
150 mockDispatcher.verifyCalls.setExpected(1);
151
152 try {
153 coreMock.verify();
154 } catch (AssertionFailedError expected) {
155 AssertMo.assertIncludes("Should include mock name", MOCK_NAME, expected.getMessage());
156 mockDispatcher.verifyExpectations();
157 return;
158 }
159 fail("Should have thrown exception");
160 }
161 }
This page was automatically generated by Maven