147d431f63a66505a645f282416659a9758a91f1cBrett Chabot/* 247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Copyright 2001-2009 OFFIS, Tammo Freese 347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Licensed under the Apache License, Version 2.0 (the "License"); 547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * you may not use this file except in compliance with the License. 647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * You may obtain a copy of the License at 747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * http://www.apache.org/licenses/LICENSE-2.0 947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 1047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Unless required by applicable law or agreed to in writing, software 1147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * distributed under the License is distributed on an "AS IS" BASIS, 1247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * See the License for the specific language governing permissions and 1447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * limitations under the License. 1547d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 1647d431f63a66505a645f282416659a9758a91f1cBrett Chabotpackage org.easymock; 1747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 1847d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport static org.easymock.EasyMock.*; 1947d431f63a66505a645f282416659a9758a91f1cBrett Chabot 2047d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.io.Serializable; 2147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 2247d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport org.easymock.internal.*; 2347d431f63a66505a645f282416659a9758a91f1cBrett Chabot 2447d431f63a66505a645f282416659a9758a91f1cBrett Chabot/** 2547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * A <code>MockControl</code> object controls the behavior of its associated 2647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * mock object. For more information, see the EasyMock documentation. 2747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 2847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param <T> type of the mock controlled 2947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 3047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @deprecated Since EasyMock 2.0, static methods on <code>EasyMock</code> are 3147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * used to create and control mock objects. 3247d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 3347d431f63a66505a645f282416659a9758a91f1cBrett Chabot@Deprecated 3447d431f63a66505a645f282416659a9758a91f1cBrett Chabotpublic class MockControl<T> implements Serializable { 3547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 3647d431f63a66505a645f282416659a9758a91f1cBrett Chabot private static final long serialVersionUID = 8741244302173698092L; 3747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 3847d431f63a66505a645f282416659a9758a91f1cBrett Chabot private final T mock; 3947d431f63a66505a645f282416659a9758a91f1cBrett Chabot 4047d431f63a66505a645f282416659a9758a91f1cBrett Chabot private final MocksControl ctrl; 4147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 4247d431f63a66505a645f282416659a9758a91f1cBrett Chabot protected MockControl(MocksControl ctrl, Class<T> toMock) { 4347d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.ctrl = ctrl; 4447d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.mock = ctrl.createMock(toMock); 4547d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 4647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 4747d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 4847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Creates a mock control object for the specified interface. The 4947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * <code>MockControl</code> and its associated mock object will not check 5047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the order of expected method calls. An unexpected method call on the mock 5147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * object will lead to an <code>AssertionError</code>. 5247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 5347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param <T> type of the mock controlled 5447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param toMock 5547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the class of the interface to mock. 5647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @return the mock control. 5747d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 5847d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static <T> MockControl<T> createControl(Class<T> toMock) { 5947d431f63a66505a645f282416659a9758a91f1cBrett Chabot return new MockControl<T>((MocksControl) EasyMock.createControl(), 6047d431f63a66505a645f282416659a9758a91f1cBrett Chabot toMock); 6147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 6247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 6347d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 6447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Creates a mock control object for the specified interface. The 6547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * <code>MockControl</code> and its associated mock object will check the 6647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * order of expected method calls. An unexpected method call on the mock 6747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * object will lead to an <code>AssertionError</code>. 6847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 6947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param <T> type of the mock controlled 7047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param toMock 7147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the class of the interface to mock. 7247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @return the mock control. 7347d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 7447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static <T> MockControl<T> createStrictControl(Class<T> toMock) { 7547d431f63a66505a645f282416659a9758a91f1cBrett Chabot return new MockControl<T>( 7647d431f63a66505a645f282416659a9758a91f1cBrett Chabot (MocksControl) EasyMock.createStrictControl(), toMock); 7747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 7847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 7947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 8047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Creates a mock control object for the specified interface. The 8147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * <code>MockControl</code> and its associated mock object will not check 8247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the order of expected method calls. An unexpected method call on the mock 8347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * object will return an empty value (0, null, false). 8447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 8547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param <T> type of the mock controlled 8647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param toMock 8747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the class of the interface to mock. 8847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @return the mock control. 8947d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 9047d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static <T> MockControl<T> createNiceControl(Class<T> toMock) { 9147d431f63a66505a645f282416659a9758a91f1cBrett Chabot return new MockControl<T>((MocksControl) EasyMock.createNiceControl(), 9247d431f63a66505a645f282416659a9758a91f1cBrett Chabot toMock); 9347d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 9447d431f63a66505a645f282416659a9758a91f1cBrett Chabot 9547d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 9647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Returns the mock object. 9747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 9847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @return the mock object of this control 9947d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 10047d431f63a66505a645f282416659a9758a91f1cBrett Chabot public T getMock() { 10147d431f63a66505a645f282416659a9758a91f1cBrett Chabot return mock; 10247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 10347d431f63a66505a645f282416659a9758a91f1cBrett Chabot 10447d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 10547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Resets the mock control and the mock object to the state directly after 10647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * creation. 10747d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 10847d431f63a66505a645f282416659a9758a91f1cBrett Chabot public final void reset() { 10947d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.reset(); 11047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 11147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 11247d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 11347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Switches the mock object from record state to replay state. For more 11447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * information, see the EasyMock documentation. 11547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 11647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 11747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object already is in replay state. 11847d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 11947d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void replay() { 12047d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.replay(); 12147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 12247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 12347d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 12447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Verifies that all expectations have been met. For more information, see 12547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the EasyMock documentation. 12647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 12747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 12847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in record state. 12947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws AssertionError 13047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if any expectation has not been met. 13147d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 13247d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void verify() { 13347d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.verify(); 13447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 13547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 13647d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 13747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call once, and 13847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * will react by returning silently. 13947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 14047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 14147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 14247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before, or if the last method 14347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock was no void method. 14447d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 14547d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setVoidCallable() { 14647d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 14747d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting void callable") 14847d431f63a66505a645f282416659a9758a91f1cBrett Chabot .once(); 14947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 15047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 15147d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 15247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call once, and 15347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * will react by throwing the provided Throwable. 15447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 15547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable 15647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the Throwable to throw. 15747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 15847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state or if no method was 15947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. 16047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalArgumentException 16147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the last method called on the mock cannot throw the 16247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * provided Throwable. 16347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception NullPointerException 16447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if throwable is null. 16547d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 16647d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setThrowable(Throwable throwable) { 16747d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 16847d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting Throwable") 16947d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andThrow(throwable).once(); 17047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 17147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 17247d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 17347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call once, and 17447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * will react by returning the provided return value. 17547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 17647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value 17747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the return value. 17847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 17947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 18047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. or if the last method 18147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock does not return <code>boolean</code>. 18247d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 18347d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setReturnValue(Object value) { 18447d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 18547d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting return value") 18647d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andReturn(value).once(); 18747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 18847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 18947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 19047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call a fixed 19147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * number of times, and will react by returning silently. 19247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 19347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param times 19447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the number of times that the call is expected. 19547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 19647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 19747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before, or if the last method 19847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock was no void method. 19947d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 20047d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setVoidCallable(int times) { 20147d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 20247d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting void callable") 20347d431f63a66505a645f282416659a9758a91f1cBrett Chabot .times(times); 20447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 20547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 20647d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 20747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call a fixed 20847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * number of times, and will react by throwing the provided Throwable. 20947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 21047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable 21147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the Throwable to throw. 21247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param times 21347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the number of times that the call is expected. 21447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 21547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state or if no method was 21647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. 21747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalArgumentException 21847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the last method called on the mock cannot throw the 21947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * provided Throwable. 22047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception NullPointerException 22147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if throwable is null. 22247d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 22347d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setThrowable(Throwable throwable, int times) { 22447d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 22547d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting Throwable") 22647d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andThrow(throwable).times(times); 22747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 22847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 22947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 23047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call a fixed 23147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * number of times, and will react by returning the provided return value. 23247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 23347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value 23447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the return value. 23547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param times 23647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the number of times that the call is expected. 23747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 23847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 23947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. or if the last method 24047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock does not return <code>boolean</code>. 24147d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 24247d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setReturnValue(Object value, int times) { 24347d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 24447d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting return value") 24547d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andReturn(value).times(times); 24647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 24747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 24847d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 24947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call a fixed 25047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * number of times, and will react by returning the provided return value. 25147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 25247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value 25347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the return value. 25447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param range 25547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the number of times that the call is expected. 25647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 25747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 25847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. or if the last method 25947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock does not return <code>boolean</code>. 26047d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 26147d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setReturnValue(Object value, Range range) { 26247d431f63a66505a645f282416659a9758a91f1cBrett Chabot IExpectationSetters<Object> setter = expectLastCall( 26347d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting return value") 26447d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andReturn(value); 26547d431f63a66505a645f282416659a9758a91f1cBrett Chabot callWithConvertedRange(setter, range); 26647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 26747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 26847d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 26947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will by default allow the last method 27047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * specified by a method call. 27147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 27247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 27347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 27447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before, or if the last method 27547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock was no void method. 27647d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 27747d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setDefaultVoidCallable() { 27847d431f63a66505a645f282416659a9758a91f1cBrett Chabot ((MocksControl) expectLastCall("method call on the mock needed before setting default void callable")) 27947d431f63a66505a645f282416659a9758a91f1cBrett Chabot .setLegacyDefaultVoidCallable(); 28047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 28147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 28247d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 28347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will by default allow the last method 28447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * specified by a method call, and will react by throwing the provided 28547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Throwable. 28647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 28747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable 28847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * throwable the throwable to be thrown 28947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalArgumentException 29047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the last method called on the mock cannot throw the 29147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * provided Throwable. 29247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception NullPointerException 29347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if throwable is null. 29447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 29547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, or if no method was 29647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. 29747d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 29847d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setDefaultThrowable(Throwable throwable) { 29947d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.setLegacyDefaultThrowable(throwable); 30047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 30147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 30247d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 30347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will by default allow the last method 30447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * specified by a method call, and will react by returning the provided 30547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * return value. 30647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 30747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value 30847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the return value. 30947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 31047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 31147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. or if the last method 31247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock does not return <code>boolean</code>. 31347d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 31447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setDefaultReturnValue(Object value) { 31547d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.setLegacyDefaultReturnValue(value); 31647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 31747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 31847d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 31947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Sets the ArgumentsMatcher for the last method called on the mock object. 32047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * The matcher must be set before any behavior for the method is defined. 32147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 32247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param matcher the matcher for the last method called 32347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 32447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if called in replay state, or if no method was called on the 32547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * mock object before. 32647d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 32747d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setMatcher(ArgumentsMatcher matcher) { 32847d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.setLegacyMatcher(matcher); 32947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 33047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 33147d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 33247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call between 33347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * <code>minCount</code> and <code>maxCount</code> times, and will react 33447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * by returning silently. 33547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 33647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param minCount 33747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the minimum number of times that the call is expected. 33847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param maxCount 33947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the maximum number of times that the call is expected. 34047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 34147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 34247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before, or if the last method 34347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock was no void method. 34447d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 34547d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setVoidCallable(int minCount, int maxCount) { 34647d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 34747d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting void callable") 34847d431f63a66505a645f282416659a9758a91f1cBrett Chabot .times(minCount, maxCount); 34947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 35047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 35147d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setVoidCallable(Range range) { 35247d431f63a66505a645f282416659a9758a91f1cBrett Chabot IExpectationSetters<Object> setter = expectLastCall("method call on the mock needed before setting void callable"); 35347d431f63a66505a645f282416659a9758a91f1cBrett Chabot callWithConvertedRange(setter, range); 35447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 35547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 35647d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 35747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call between 35847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * <code>minCount</code> and <code>maxCount</code> times, and will react 35947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * by throwing the provided Throwable. 36047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 36147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable 36247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the Throwable to throw. 36347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param minCount 36447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the minimum number of times that the call is expected. 36547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param maxCount 36647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the maximum number of times that the call is expected. 36747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalStateException 36847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state or if no method was 36947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. 37047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception IllegalArgumentException 37147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the last method called on the mock cannot throw the 37247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * provided Throwable. 37347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @exception NullPointerException 37447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if throwable is null. 37547d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 37647d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setThrowable(Throwable throwable, int minCount, int maxCount) { 37747d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 37847d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting Throwable") 37947d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andThrow(throwable).times(minCount, maxCount); 38047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 38147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 38247d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setThrowable(Throwable throwable, Range range) { 38347d431f63a66505a645f282416659a9758a91f1cBrett Chabot IExpectationSetters<Object> setter = expectLastCall( 38447d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting Throwable") 38547d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andThrow(throwable); 38647d431f63a66505a645f282416659a9758a91f1cBrett Chabot callWithConvertedRange(setter, range); 38747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 38847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 38947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 39047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Records that the mock object will expect the last method call between 39147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * <code>minCount</code> and <code>maxCount</code> times, and will react 39247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * by returning the provided return value. 39347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 39447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value 39547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the return value. 39647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param minCount 39747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the minimum number of times that the call is expected. 39847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param maxCount 39947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * the maximum number of times that the call is expected. 40047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 40147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if the mock object is in replay state, if no method was 40247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock object before. or if the last method 40347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * called on the mock does not return <code>boolean</code>. 40447d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 40547d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setReturnValue(Object value, int minCount, int maxCount) { 40647d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 40747d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting return value") 40847d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andReturn(value).times(minCount, maxCount); 40947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 41047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 41147d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 41247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Exactly one call. 41347d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 41447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static final Range ONE = MocksControl.ONCE; 41547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 41647d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 41747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * One or more calls. 41847d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 41947d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static final Range ONE_OR_MORE = MocksControl.AT_LEAST_ONCE; 42047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 42147d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 42247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Zero or more calls. 42347d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 42447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static final Range ZERO_OR_MORE = MocksControl.ZERO_OR_MORE; 42547d431f63a66505a645f282416659a9758a91f1cBrett Chabot 42647d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 42747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Matches if each expected argument is equal to the corresponding actual 42847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * argument. 42947d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 43047d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static final ArgumentsMatcher EQUALS_MATCHER = new EqualsMatcher(); 43147d431f63a66505a645f282416659a9758a91f1cBrett Chabot 43247d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 43347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Matches always. 43447d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 43547d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static final ArgumentsMatcher ALWAYS_MATCHER = new AlwaysMatcher(); 43647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 43747d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 43847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Matches if each expected argument is equal to the corresponding actual 43947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * argument for non-array arguments; array arguments are compared with the 44047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * appropriate <code>java.util.Arrays.equals()</code> -method. 44147d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 44247d431f63a66505a645f282416659a9758a91f1cBrett Chabot public static final ArgumentsMatcher ARRAY_MATCHER = new ArrayMatcher(); 44347d431f63a66505a645f282416659a9758a91f1cBrett Chabot 44447d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 44547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Sets the default ArgumentsMatcher for all methods of the mock object. The 44647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * matcher must be set before any behavior is defined on the mock object. 44747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 44847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param matcher the default matcher for this control 44947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @throws IllegalStateException 45047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * if called in replay state, or if any behavior is already 45147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * defined on the mock object. 45247d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 45347d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void setDefaultMatcher(ArgumentsMatcher matcher) { 45447d431f63a66505a645f282416659a9758a91f1cBrett Chabot ctrl.setLegacyDefaultMatcher(matcher); 45547d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 45647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 45747d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 45847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setReturnValue(Object)}. For explanation, see 45947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * "Convenience Methods for Return Values" in the EasyMock documentation. 46047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 46147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @parammocked method return type 46247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param returned value type 46347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 46447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 46547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value value returned by the mock 46647d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 46747d431f63a66505a645f282416659a9758a91f1cBrett Chabot public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value) { 46847d431f63a66505a645f282416659a9758a91f1cBrett Chabot EasyMock.expectLastCall().andReturn(value).once(); 46947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 47047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 47147d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndReturn(int ignored, int value) { 47247d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.expectAndReturn((Object) ignored, (Object) value); 47347d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 47447d431f63a66505a645f282416659a9758a91f1cBrett Chabot 47547d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 47647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setReturnValue(Object, Range)}. For 47747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Return Values" in the EasyMock 47847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 47947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 48047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param mocked method return type 48147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param returned value type 48247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 48347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 48447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value value returned by the mock 48547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param range range of number of calls 48647d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 48747d431f63a66505a645f282416659a9758a91f1cBrett Chabot public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value, 48847d431f63a66505a645f282416659a9758a91f1cBrett Chabot Range range) { 48947d431f63a66505a645f282416659a9758a91f1cBrett Chabot IExpectationSetters<Object> expectAndReturn = EasyMock.expectLastCall() 49047d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andReturn(value); 49147d431f63a66505a645f282416659a9758a91f1cBrett Chabot callWithConvertedRange(expectAndReturn, range); 49247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 49347d431f63a66505a645f282416659a9758a91f1cBrett Chabot 49447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndReturn(int ignored, int value, Range range) { 49547d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.expectAndReturn((Object) ignored, (Object) value, range); 49647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 49747d431f63a66505a645f282416659a9758a91f1cBrett Chabot 49847d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 49947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setReturnValue(Object, int)}. For 50047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Return Values" in the EasyMock 50147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 50247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 50347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param mocked method return type 50447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param returned value type 50547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 50647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 50747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value value returned by the mock 50847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param count number of times the call is expected 50947d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 51047d431f63a66505a645f282416659a9758a91f1cBrett Chabot public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value, 51147d431f63a66505a645f282416659a9758a91f1cBrett Chabot int count) { 51247d431f63a66505a645f282416659a9758a91f1cBrett Chabot EasyMock.expectLastCall().andReturn(value).times(count); 51347d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 51447d431f63a66505a645f282416659a9758a91f1cBrett Chabot 51547d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndReturn(int ignored, int value, int count) { 51647d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.expectAndReturn((Object) ignored, (Object) value, count); 51747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 51847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 51947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 52047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setReturnValue(Object, int, int)}. For 52147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Return Values" in the EasyMock 52247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 52347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 52447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param mocked method return type 52547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param returned value type 52647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 52747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 52847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value value returned by the mock 52947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param min minimum number of times the call is expected 53047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param max maximum number of times the call is expected 53147d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 53247d431f63a66505a645f282416659a9758a91f1cBrett Chabot public <V1, V2 extends V1> void expectAndReturn(V1 ignored, V2 value, 53347d431f63a66505a645f282416659a9758a91f1cBrett Chabot int min, int max) { 53447d431f63a66505a645f282416659a9758a91f1cBrett Chabot EasyMock.expectLastCall().andReturn(value).times(min, max); 53547d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 53647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 53747d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndReturn(int ignored, int value, int min, int max) { 53847d431f63a66505a645f282416659a9758a91f1cBrett Chabot this.expectAndReturn((Object) ignored, (Object) value, min, max); 53947d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 54047d431f63a66505a645f282416659a9758a91f1cBrett Chabot 54147d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 54247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setThrowable(Throwable)}. For explanation, 54347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * see "Convenience Methods for Throwables" in the EasyMock documentation. 54447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 54547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 54647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 54747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable to be thrown on the call 54847d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 54947d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndThrow(Object ignored, Throwable throwable) { 55047d431f63a66505a645f282416659a9758a91f1cBrett Chabot EasyMock.expect(ignored).andThrow(throwable).once(); 55147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 55247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 55347d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 55447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setThrowable(Throwable, Range)}. For 55547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Throwables" in the EasyMock 55647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 55747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 55847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 55947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 56047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable to be thrown on the call 56147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param range range of number of calls 56247d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 56347d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndThrow(Object ignored, Throwable throwable, Range range) { 56447d431f63a66505a645f282416659a9758a91f1cBrett Chabot IExpectationSetters<Object> setter = EasyMock.expect(ignored).andThrow( 56547d431f63a66505a645f282416659a9758a91f1cBrett Chabot throwable); 56647d431f63a66505a645f282416659a9758a91f1cBrett Chabot callWithConvertedRange(setter, range); 56747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 56847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 56947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 57047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setThrowable(Throwable, int)}. For 57147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Throwables" in the EasyMock 57247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 57347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 57447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 57547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 57647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable to be thrown on the call 57747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param count number of times the call is expected 57847d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 57947d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndThrow(Object ignored, Throwable throwable, int count) { 58047d431f63a66505a645f282416659a9758a91f1cBrett Chabot expect(ignored).andThrow(throwable).times(count); 58147d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 58247d431f63a66505a645f282416659a9758a91f1cBrett Chabot 58347d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 58447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setThrowable(Throwable, int, int)}. For 58547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Throwables" in the EasyMock 58647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 58747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 58847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 58947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 59047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable to be thrown on the call 59147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param min minimum number of times the call is expected 59247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param max maximum number of times the call is expected 59347d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 59447d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndThrow(Object ignored, Throwable throwable, int min, 59547d431f63a66505a645f282416659a9758a91f1cBrett Chabot int max) { 59647d431f63a66505a645f282416659a9758a91f1cBrett Chabot expect(ignored).andThrow(throwable).times(min, max); 59747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 59847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 59947d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 60047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setDefaultReturnValue(Object)}. For 60147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Return Values" in the EasyMock 60247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 60347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 60447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param mocked method return type 60547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param returned value type 60647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 60747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 60847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param value value returned by the mock 60947d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 61047d431f63a66505a645f282416659a9758a91f1cBrett Chabot public <V1, V2 extends V1> void expectAndDefaultReturn(V1 ignored, V2 value) { 61147d431f63a66505a645f282416659a9758a91f1cBrett Chabot EasyMock.expectLastCall().andStubReturn(value); 61247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 61347d431f63a66505a645f282416659a9758a91f1cBrett Chabot 61447d431f63a66505a645f282416659a9758a91f1cBrett Chabot /** 61547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Same as {@link MockControl#setDefaultThrowable(Throwable)}. For 61647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * explanation, see "Convenience Methods for Throwables" in the EasyMock 61747d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation. 61847d431f63a66505a645f282416659a9758a91f1cBrett Chabot * 61947d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param ignored 62047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * an ignored value. 62147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * @param throwable to be thrown on the call 62247d431f63a66505a645f282416659a9758a91f1cBrett Chabot */ 62347d431f63a66505a645f282416659a9758a91f1cBrett Chabot public void expectAndDefaultThrow(Object ignored, Throwable throwable) { 62447d431f63a66505a645f282416659a9758a91f1cBrett Chabot expectLastCall( 62547d431f63a66505a645f282416659a9758a91f1cBrett Chabot "method call on the mock needed before setting default Throwable") 62647d431f63a66505a645f282416659a9758a91f1cBrett Chabot .andStubThrow(throwable); 62747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 62847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 62947d431f63a66505a645f282416659a9758a91f1cBrett Chabot private IExpectationSetters<Object> expectLastCall(String failureMessage) { 63047d431f63a66505a645f282416659a9758a91f1cBrett Chabot try { 63147d431f63a66505a645f282416659a9758a91f1cBrett Chabot return EasyMock.expectLastCall(); 63247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } catch (IllegalStateException e) { 63347d431f63a66505a645f282416659a9758a91f1cBrett Chabot throw new IllegalStateException(failureMessage); 63447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 63547d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 63647d431f63a66505a645f282416659a9758a91f1cBrett Chabot 63747d431f63a66505a645f282416659a9758a91f1cBrett Chabot private void callWithConvertedRange(IExpectationSetters<Object> setter, Range range) { 63847d431f63a66505a645f282416659a9758a91f1cBrett Chabot if (range == ONE) { 63947d431f63a66505a645f282416659a9758a91f1cBrett Chabot setter.once(); 64047d431f63a66505a645f282416659a9758a91f1cBrett Chabot } else if (range == ONE_OR_MORE) { 64147d431f63a66505a645f282416659a9758a91f1cBrett Chabot setter.atLeastOnce(); 64247d431f63a66505a645f282416659a9758a91f1cBrett Chabot } else if (range == ZERO_OR_MORE) { 64347d431f63a66505a645f282416659a9758a91f1cBrett Chabot setter.anyTimes(); 64447d431f63a66505a645f282416659a9758a91f1cBrett Chabot } else { 64547d431f63a66505a645f282416659a9758a91f1cBrett Chabot throw new IllegalArgumentException("Unexpected Range"); 64647d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 64747d431f63a66505a645f282416659a9758a91f1cBrett Chabot } 64847d431f63a66505a645f282416659a9758a91f1cBrett Chabot 64947d431f63a66505a645f282416659a9758a91f1cBrett Chabot}