001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.easymock.util;
017:
018: import junit.framework.TestCase;
019: import static org.easymock.classextension.EasyMock.*;
020: import static org.easymock.internal.MocksControl.MockType.DEFAULT;
021: import static org.unitils.easymock.EasyMockUnitils.refEq;
022: import static org.unitils.reflectionassert.ReflectionComparatorMode.IGNORE_DEFAULTS;
023:
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.List;
027:
028: /**
029: * A test for {@link LenientMocksControl}.
030: *
031: * @author Tim Ducheyne
032: * @author Filip Neven
033: */
034: public class LenientMocksControlTest extends TestCase {
035:
036: /* Class under test, with mock type LENIENT and ignore defaults */
037: private LenientMocksControl lenientMocksControl;
038:
039: /**
040: * Initializes the test fixture.
041: */
042: protected void setUp() throws Exception {
043: super .setUp();
044:
045: lenientMocksControl = new LenientMocksControl(DEFAULT,
046: IGNORE_DEFAULTS);
047: }
048:
049: /**
050: * Test for a mocked method call that is invoked with the expected arguments.
051: */
052: public void testLenientMocksControl_equals() {
053:
054: MockedClass mock = lenientMocksControl
055: .createMock(MockedClass.class);
056: expect(
057: mock.someBehavior(true, 999, "Test",
058: new ArrayList<Object>())).andReturn("Result");
059: replay(mock);
060:
061: String result = mock.someBehavior(true, 999, "Test",
062: new ArrayList<Object>());
063:
064: assertEquals("Result", result);
065: verify(mock);
066: }
067:
068: /**
069: * Test for a mocked method call that has no arguments.
070: */
071: public void testLenientMocksControl_equalsNoArguments() {
072:
073: MockedClass mock = lenientMocksControl
074: .createMock(MockedClass.class);
075: mock.someBehavior();
076: replay(mock);
077:
078: mock.someBehavior();
079:
080: verify(mock);
081: }
082:
083: /**
084: * Test for a invoking a mocked method call more than once.
085: */
086: public void testLenientMocksControl_equalsDoubleInvocation() {
087:
088: MockedClass mock = lenientMocksControl
089: .createMock(MockedClass.class);
090: expect(
091: mock.someBehavior(true, 111, "Test1", Arrays
092: .asList("1"))).andReturn("Result1");
093: expect(
094: mock.someBehavior(false, 222, "Test2", Arrays
095: .asList("2"))).andReturn("Result2");
096: replay(mock);
097:
098: String result1 = mock.someBehavior(true, 111, "Test1", Arrays
099: .asList("1"));
100: String result2 = mock.someBehavior(false, 222, "Test2", Arrays
101: .asList("2"));
102:
103: verify(mock);
104: assertEquals("Result1", result1);
105: assertEquals("Result2", result2);
106: }
107:
108: /**
109: * Test for ignoring a default value for an argument.
110: */
111: public void testLenientMocksControl_equalsIgnoreDefaults() {
112:
113: MockedClass mock = lenientMocksControl
114: .createMock(MockedClass.class);
115: expect(mock.someBehavior(false, 0, null, null)).andReturn(
116: "Result");
117: replay(mock);
118:
119: String result = mock.someBehavior(true, 999, "Test",
120: new ArrayList<Object>());
121:
122: assertEquals("Result", result);
123: verify(mock);
124: }
125:
126: /**
127: * Test for a mocked method that is expected but was never called.
128: */
129: public void testLenientMocksControl_notEqualsNotCalled() {
130:
131: MockedClass mock = lenientMocksControl
132: .createMock(MockedClass.class);
133: expect(
134: mock.someBehavior(true, 999, "XXXX",
135: new ArrayList<Object>())).andReturn("Result");
136: replay(mock);
137:
138: try {
139: verify(mock);
140: fail();
141: } catch (AssertionError e) {
142: //expected
143: }
144: }
145:
146: /**
147: * Test for a mocked method call that is invoked with the different arguments.
148: */
149: public void testLenientMocksControl_notEqualsDifferentArguments() {
150:
151: MockedClass mock = lenientMocksControl
152: .createMock(MockedClass.class);
153: expect(
154: mock.someBehavior(true, 999, "XXXX",
155: new ArrayList<Object>())).andReturn("Result");
156: replay(mock);
157:
158: try {
159: mock.someBehavior(true, 999, "Test",
160: new ArrayList<Object>());
161: fail();
162: } catch (AssertionError e) {
163: //expected
164: }
165: }
166:
167: /**
168: * Test for using argument matchers (refEq and EasyMocks eq).
169: */
170: public void testLenientMocksControl_mixingArgumentMatchers() {
171:
172: MockedClass mock = lenientMocksControl
173: .createMock(MockedClass.class);
174: expect(
175: mock.someBehavior(eq(true), refEq(999), eq("Test"),
176: refEq(new ArrayList<Object>()))).andReturn(
177: "Result");
178: replay(mock);
179:
180: String result = mock.someBehavior(true, 999, "Test",
181: new ArrayList<Object>());
182:
183: assertEquals("Result", result);
184: verify(mock);
185: }
186:
187: /**
188: * Test for a invoking a mocked method call with a enum argument.
189: */
190: public void testLenientMocksControl_equalsEnumArgument() {
191:
192: MockedClass mock = lenientMocksControl
193: .createMock(MockedClass.class);
194: expect(mock.someBehavior(MockedClass.TestEnum.TEST1))
195: .andStubReturn("Result1");
196: expect(mock.someBehavior(MockedClass.TestEnum.TEST2))
197: .andStubReturn("Result2");
198: replay(mock);
199:
200: String result1 = mock.someBehavior(MockedClass.TestEnum.TEST1);
201: String result2 = mock.someBehavior(MockedClass.TestEnum.TEST2);
202:
203: verify(mock);
204: assertEquals("Result1", result1);
205: assertEquals("Result2", result2);
206: }
207:
208: /**
209: * The test class that is going to be mocked.
210: */
211: private static class MockedClass {
212:
213: public enum TestEnum {
214: TEST1, TEST2
215: }
216:
217: public void someBehavior() {
218: }
219:
220: public String someBehavior(boolean b, int i, Object object,
221: List<?> list) {
222: return null;
223: }
224:
225: public String someBehavior(TestEnum testEnum) {
226: return null;
227: }
228: }
229:
230: }
|