|
| 1 | +/** |
| 2 | + * Copyright (c) 2014, FinancialForce.com, inc |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * - Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * - Neither the name of the FinancialForce.com, inc nor the names of its contributors |
| 14 | + * may be used to endorse or promote products derived from this software without |
| 15 | + * specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 20 | + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 22 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | +public with sharing class fflib_ApexMocks |
| 27 | +{ |
| 28 | +public static final Integer NEVER = 0; |
| 29 | + |
| 30 | +private final fflib_MethodCountRecorder methodCountRecorder; |
| 31 | +private final fflib_MethodReturnValueRecorder methodReturnValueRecorder; |
| 32 | + |
| 33 | +public Boolean Verifying |
| 34 | +{ |
| 35 | +get |
| 36 | +{ |
| 37 | +return methodCountRecorder.Verifying; |
| 38 | +} |
| 39 | + |
| 40 | +private set; |
| 41 | +} |
| 42 | + |
| 43 | +public Boolean Stubbing |
| 44 | +{ |
| 45 | +get |
| 46 | +{ |
| 47 | +return methodReturnValueRecorder.Stubbing; |
| 48 | +} |
| 49 | + |
| 50 | +private set; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Construct an ApexMocks instance. |
| 55 | + */ |
| 56 | +public fflib_ApexMocks() |
| 57 | +{ |
| 58 | +methodCountRecorder = new fflib_MethodCountRecorder(); |
| 59 | +methodReturnValueRecorder = new fflib_MethodReturnValueRecorder(); |
| 60 | + |
| 61 | +methodCountRecorder.Verifying = false; |
| 62 | +methodReturnValueRecorder.Stubbing = false; |
| 63 | +} |
| 64 | + |
| 65 | +public static String extractTypeName(Object mockInstance) |
| 66 | +{ |
| 67 | +return String.valueOf(mockInstance).split(':').get(0); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Verify a method was called on a mock object. |
| 72 | + * @param mockInstance The mock object instance. |
| 73 | + * @return The mock object instance. |
| 74 | + */ |
| 75 | +public Object verify(Object mockInstance) |
| 76 | +{ |
| 77 | +return verify(mockInstance, 1); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Verify a method was called on a mock object. |
| 82 | + * @param mockInstance The mock object instance. |
| 83 | + * @param times The number of times you expect the method to have been called. |
| 84 | + * @return The mock object instance. |
| 85 | + */ |
| 86 | +public Object verify(Object mockInstance, Integer times) |
| 87 | +{ |
| 88 | +methodCountRecorder.Verifying = true; |
| 89 | +methodCountRecorder.VerifyCount = times; |
| 90 | +return mockInstance; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Verfiy a method was called on a mock object. |
| 95 | + * @param mockInstance The mock object instance. |
| 96 | + * @param methodName The method you expect to have been called. |
| 97 | + * @param methodArg The argument you expect to have been passed to the method being verified. |
| 98 | + */ |
| 99 | +public void verifyMethodCall(Object mockInstance, String methodName, Object methodArg) |
| 100 | +{ |
| 101 | +methodCountRecorder.verifyMethodCall(mockInstance, methodName, methodArg); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Tell ApexMocks framework you are about to start stubbing using when() calls. |
| 106 | + */ |
| 107 | +public void startStubbing() |
| 108 | +{ |
| 109 | +methodReturnValueRecorder.Stubbing = true; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Tell ApexMocks framework you are about to stop stubbing using when() calls. |
| 114 | + */ |
| 115 | +public void stopStubbing() |
| 116 | +{ |
| 117 | +methodReturnValueRecorder.Stubbing = false; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Setup when stubbing for a mock object instance. |
| 122 | + * @param This is the return value from the method called on the mockInstance, and is ignored here since we are about to setup |
| 123 | + * the stubbed return value using thenReturn() (see MethodReturnValue class below). |
| 124 | + */ |
| 125 | +public fflib_MethodReturnValue when(Object ignoredRetVal) |
| 126 | +{ |
| 127 | +return methodReturnValueRecorder.MethodReturnValue; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Record a method was called on a mock object. |
| 132 | + * @param mockInstance The mock object instance. |
| 133 | + * @param methodName The method to be recorded. |
| 134 | + * @param methodArg The method argument to be recorded. |
| 135 | + */ |
| 136 | +public void recordMethod(Object mockInstance, String methodName, Object methodArg) |
| 137 | +{ |
| 138 | +methodCountRecorder.recordMethod(mockInstance, methodName, methodArg); |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Prepare a stubbed method return value. |
| 143 | + * @param mockInstance The mock object instance. |
| 144 | + * @param methodName The method for which to prepare a return value. |
| 145 | + * @param methodArg The method argument for which to prepare a return value. |
| 146 | + */ |
| 147 | +public void prepareMethodReturnValue(Object mockInstance, String methodName, Object methodArg) |
| 148 | +{ |
| 149 | +methodReturnValueRecorder.prepareMethodReturnValue(mockInstance, methodName, methodArg); |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Get the method return value for the given method call. |
| 154 | + * @param mockInstance The mock object instance. |
| 155 | + * @param methodName The method for which to prepare a return value. |
| 156 | + * @param methodArg The method argument for which to prepare a return value. |
| 157 | + * @return The MethodReturnValue instance. |
| 158 | + */ |
| 159 | +public fflib_MethodReturnValue getMethodReturnValue(Object mockInstance, String methodName, Object methodArg) |
| 160 | +{ |
| 161 | +return methodReturnValueRecorder.getMethodReturnValue(mockInstance, methodName, methodArg); |
| 162 | +} |
| 163 | +} |
0 commit comments