How to use EasyMockNewInvocationControl class of org.powermock.api.easymock.internal.invocationcontrol package

Best Powermock code snippet using org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControl

Source:PowerMock.java Github

copy

Full Screen

...22import org.easymock.internal.MockInvocationHandler;23import org.easymock.internal.MocksControl;24import org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl;25import org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlAssertionError;26import org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControl;27import org.powermock.api.easymock.internal.mockstrategy.MockStrategy;28import org.powermock.api.easymock.internal.mockstrategy.impl.DefaultMockStrategy;29import org.powermock.api.easymock.internal.mockstrategy.impl.NiceMockStrategy;30import org.powermock.api.easymock.internal.mockstrategy.impl.StrictMockStrategy;31import org.powermock.api.support.SuppressCode;32import org.powermock.api.support.membermodification.MemberModifier;33import org.powermock.core.ClassReplicaCreator;34import org.powermock.core.DefaultFieldValueGenerator;35import org.powermock.core.MockGateway;36import org.powermock.core.MockRepository;37import org.powermock.core.classloader.MockClassLoader;38import org.powermock.core.classloader.annotations.PrepareForTest;39import org.powermock.core.classloader.annotations.PrepareOnlyThisForTest;40import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;41import org.powermock.core.spi.MethodInvocationControl;42import org.powermock.core.spi.NewInvocationControl;43import org.powermock.core.spi.support.InvocationSubstitute;44import org.powermock.reflect.Whitebox;45import org.powermock.reflect.internal.WhiteboxImpl;46import java.lang.reflect.Constructor;47import java.lang.reflect.Field;48import java.lang.reflect.InvocationTargetException;49import java.lang.reflect.Method;50import java.lang.reflect.Modifier;51import java.lang.reflect.Proxy;52import java.util.ArrayList;53import java.util.Arrays;54import java.util.HashSet;55import java.util.LinkedList;56import java.util.List;57import java.util.Set;58/**59 * PowerMock extends EasyMock functionality with several new features such as60 * mocking static and private methods, mocking new instances and more. Use61 * PowerMock instead of EasyMock where applicable.62 */63public class PowerMock extends MemberModifier {64 private static final String NICE_REPLAY_AND_VERIFY_KEY = "PowerMock.niceReplayAndVerify";65 static {66 MockGateway.MOCK_STANDARD_METHODS = false;67 MockGateway.MOCK_GET_CLASS_METHOD = false;68 }69 /**70 * Creates a mock object that supports mocking of final and native methods.71 *72 * @param <T> the type of the mock object73 * @param type the type of the mock object74 * @param methods optionally what methods to mock75 * @return the mock object.76 */77 public static synchronized <T> T createMock(Class<T> type, Method... methods) {78 return doMock(type, false, new DefaultMockStrategy(), null, methods);79 }80 /**81 * Creates a mock object that supports mocking of final and native methods.82 *83 * @param <T> the type of the mock object84 * @param type the type of the mock object85 * @return the mock object.86 */87 public static synchronized <T> T createMock(Class<T> type) {88 return doMock(type, false, new DefaultMockStrategy(), null, (Method[]) null);89 }90 /**91 * Creates a mock object that supports mocking of final and native methods92 * and invokes a specific constructor.93 *94 * @param <T> the type of the mock object95 * @param type the type of the mock object96 * @param constructorArgs The constructor arguments that will be used to invoke a97 * special constructor.98 * @param methods optionally what methods to mock99 * @return the mock object.100 */101 public static <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) {102 return doMock(type, false, new DefaultMockStrategy(), constructorArgs, methods);103 }104 /**105 * Creates a mock object that supports mocking of final and native methods106 * and invokes a specific constructor based on the supplied argument values.107 *108 * @param <T> the type of the mock object109 * @param type the type of the mock object110 * @param constructorArguments The constructor arguments that will be used to invoke a111 * certain constructor.112 * @return the mock object.113 */114 public static <T> T createMock(Class<T> type, Object... constructorArguments) {115 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);116 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);117 return doMock(type, false, new DefaultMockStrategy(), constructorArgs, (Method[]) null);118 }119 /**120 * Creates a strict mock object that supports mocking of final and native121 * methods.122 *123 * @param <T> the type of the mock object124 * @param type the type of the mock object125 * @param methods optionally what methods to mock126 * @return the mock object.127 */128 public static synchronized <T> T createStrictMock(Class<T> type, Method... methods) {129 return doMock(type, false, new StrictMockStrategy(), null, methods);130 }131 /**132 * Creates a strict mock object that supports mocking of final and native133 * methods.134 *135 * @param <T> the type of the mock object136 * @param type the type of the mock object137 * @return the mock object.138 */139 public static synchronized <T> T createStrictMock(Class<T> type) {140 return doMock(type, false, new StrictMockStrategy(), null, (Method[]) null);141 }142 /**143 * Creates a nice mock object that supports mocking of final and native144 * methods.145 *146 * @param <T> the type of the mock object147 * @param type the type of the mock object148 * @param methods optionally what methods to mock149 * @return the mock object.150 */151 public static synchronized <T> T createNiceMock(Class<T> type, Method... methods) {152 return doMock(type, false, new NiceMockStrategy(), null, methods);153 }154 /**155 * Creates a nice mock object that supports mocking of final and native156 * methods.157 *158 * @param <T> the type of the mock object159 * @param type the type of the mock object160 * @return the mock object.161 */162 public static synchronized <T> T createNiceMock(Class<T> type) {163 return doMock(type, false, new NiceMockStrategy(), null, (Method[]) null);164 }165 /**166 * Creates a strict mock object that supports mocking of final and native167 * methods and invokes a specific constructor.168 *169 * @param <T> the type of the mock object170 * @param type the type of the mock object171 * @param constructorArgs The constructor arguments that will be used to invoke a172 * special constructor.173 * @param methods optionally what methods to mock174 * @return the mock object.175 */176 public static <T> T createStrictMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) {177 return doMock(type, false, new StrictMockStrategy(), constructorArgs, methods);178 }179 /**180 * Creates a nice mock object that supports mocking of final and native181 * methods and invokes a specific constructor.182 *183 * @param <T> the type of the mock object184 * @param type the type of the mock object185 * @param constructorArgs The constructor arguments that will be used to invoke a186 * special constructor.187 * @param methods optionally what methods to mock188 * @return the mock object.189 */190 public static <T> T createNiceMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) {191 return doMock(type, false, new NiceMockStrategy(), constructorArgs, methods);192 }193 /**194 * Creates a strict mock object that supports mocking of final and native195 * methods and invokes a specific constructor based on the supplied argument196 * values.197 *198 * @param <T> the type of the mock object199 * @param type the type of the mock object200 * @param constructorArguments The constructor arguments that will be used to invoke a201 * certain constructor.202 * @return the mock object.203 */204 public static <T> T createStrictMock(Class<T> type, Object... constructorArguments) {205 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);206 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);207 return doMock(type, false, new StrictMockStrategy(), constructorArgs, (Method[]) null);208 }209 /**210 * Creates a nice mock object that supports mocking of final and native211 * methods and invokes a specific constructor based on the supplied argument212 * values.213 *214 * @param <T> the type of the mock object215 * @param type the type of the mock object216 * @param constructorArguments The constructor arguments that will be used to invoke a217 * certain constructor.218 * @return the mock object.219 */220 public static <T> T createNiceMock(Class<T> type, Object... constructorArguments) {221 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);222 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);223 return doMock(type, false, new NiceMockStrategy(), constructorArgs, (Method[]) null);224 }225 /**226 * Enable static mocking for a class.227 *228 * @param type the class to enable static mocking229 * @param methods optionally what methods to mock230 */231 public static synchronized void mockStatic(Class<?> type, Method... methods) {232 doMock(type, true, new DefaultMockStrategy(), null, methods);233 }234 /**235 * Enable static mocking for a class.236 *237 * @param type the class to enable static mocking238 */239 public static synchronized void mockStatic(Class<?> type) {240 doMock(type, true, new DefaultMockStrategy(), null, (Method[]) null);241 }242 /**243 * Enable strict static mocking for a class.244 *245 * @param type the class to enable static mocking246 * @param methods optionally what methods to mock247 */248 public static synchronized void mockStaticStrict(Class<?> type, Method... methods) {249 doMock(type, true, new StrictMockStrategy(), null, methods);250 }251 /**252 * Enable strict static mocking for a class.253 *254 * @param type the class to enable static mocking255 */256 public static synchronized void mockStaticStrict(Class<?> type) {257 doMock(type, true, new StrictMockStrategy(), null, (Method[]) null);258 }259 /**260 * Enable nice static mocking for a class.261 *262 * @param type the class to enable static mocking263 * @param methods optionally what methods to mock264 */265 public static synchronized void mockStaticNice(Class<?> type, Method... methods) {266 doMock(type, true, new NiceMockStrategy(), null, methods);267 }268 /**269 * Enable nice static mocking for a class.270 *271 * @param type the class to enable static mocking272 */273 public static synchronized void mockStaticNice(Class<?> type) {274 doMock(type, true, new NiceMockStrategy(), null, (Method[]) null);275 }276 /**277 * A utility method that may be used to specify several methods that should278 * <i>not</i> be mocked in an easy manner (by just passing in the method279 * names of the method you wish <i>not</i> to mock). Note that you cannot280 * uniquely specify a method to exclude using this method if there are281 * several methods with the same name in {@code type}. This method will282 * mock ALL methods that doesn't match the supplied name(s) regardless of283 * parameter types and signature. If this is not the case you should284 * fall-back on using the {@link #createMock(Class, Method...)} method285 * instead.286 *287 * @param <T> The type of the mock.288 * @param type The type that'll be used to create a mock instance.289 * @param methodNames The names of the methods that should be mocked. If290 * {@code null}, then this method will have the same effect291 * as just calling {@link #createMock(Class, Method...)} with the292 * second parameter as {@code new Method[0]} (i.e. all293 * methods in that class will be mocked).294 * @return A mock object of type <T>.295 */296 public static synchronized <T> T createPartialMockForAllMethodsExcept(Class<T> type, String... methodNames) {297 if (methodNames != null && methodNames.length == 0) {298 return createMock(type);299 }300 return createMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));301 }302 /**303 * A utility method that may be used to specify several methods that should304 * <i>not</i> be nicely mocked in an easy manner (by just passing in the305 * method names of the method you wish <i>not</i> to mock). Note that you306 * cannot uniquely specify a method to exclude using this method if there307 * are several methods with the same name in {@code type}. This method308 * will mock ALL methods that doesn't match the supplied name(s) regardless309 * of parameter types and signature. If this is not the case you should310 * fall-back on using the {@link #createMock(Class, Method...)} method311 * instead.312 *313 * @param <T> The type of the mock.314 * @param type The type that'll be used to create a mock instance.315 * @param methodNames The names of the methods that should be mocked. If316 * {@code null}, then this method will have the same effect317 * as just calling {@link #createMock(Class, Method...)} with the318 * second parameter as {@code new Method[0]} (i.e. all319 * methods in that class will be mocked).320 * @return A mock object of type <T>.321 */322 public static synchronized <T> T createNicePartialMockForAllMethodsExcept(Class<T> type, String... methodNames) {323 if (methodNames != null && methodNames.length == 0) {324 return createNiceMock(type);325 }326 return createNiceMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));327 }328 /**329 * A utility method that may be used to specify several methods that should330 * <i>not</i> be strictly mocked in an easy manner (by just passing in the331 * method names of the method you wish <i>not</i> to mock). Note that you332 * cannot uniquely specify a method to exclude using this method if there333 * are several methods with the same name in {@code type}. This method334 * will mock ALL methods that doesn't match the supplied name(s) regardless335 * of parameter types and signature. If this is not the case you should336 * fall-back on using the {@link #createMock(Class, Method...)} method337 * instead.338 *339 * @param <T> The type of the mock.340 * @param type The type that'll be used to create a mock instance.341 * @param methodNames The names of the methods that should be mocked. If342 * {@code null}, then this method will have the same effect343 * as just calling {@link #createMock(Class, Method...)} with the344 * second parameter as {@code new Method[0]} (i.e. all345 * methods in that class will be mocked).346 * @return A mock object of type <T>.347 */348 public static synchronized <T> T createStrictPartialMockForAllMethodsExcept(Class<T> type, String... methodNames) {349 if (methodNames != null && methodNames.length == 0) {350 return createStrictMock(type);351 }352 return createStrictMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));353 }354 /**355 * Mock all methods of a class except for a specific one. Use this method356 * only if you have several overloaded methods.357 *358 * @param <T> The type of the mock.359 * @param type The type that'll be used to create a mock instance.360 * @param methodNameToExclude The name of the method not to mock.361 * @param firstArgumentType The type of the first parameter of the method not to mock362 * @param moreTypes Optionally more parameter types that defines the method. Note363 * that this is only needed to separate overloaded methods.364 * @return A mock object of type <T>.365 */366 public static synchronized <T> T createPartialMockForAllMethodsExcept(Class<T> type, String methodNameToExclude,367 Class<?> firstArgumentType, Class<?>... moreTypes) {368 /*369 * The reason why we've split the first and "additional types" is370 * because it should not intervene with the mockAllExcept(type,371 * String...methodNames) method.372 */373 final Class<?>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);374 return createMock(type, WhiteboxImpl.getAllMethodsExcept(type, methodNameToExclude, argumentTypes));375 }376 /**377 * Mock all methods of a class except for a specific one nicely. Use this378 * method only if you have several overloaded methods.379 *380 * @param <T> The type of the mock.381 * @param type The type that'll be used to create a mock instance.382 * @param methodNameToExclude The name of the method not to mock.383 * @param firstArgumentType The type of the first parameter of the method not to mock384 * @param moreTypes Optionally more parameter types that defines the method. Note385 * that this is only needed to separate overloaded methods.386 * @return A mock object of type <T>.387 */388 public static synchronized <T> T createNicePartialMockForAllMethodsExcept(Class<T> type,389 String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) {390 /*391 * The reason why we've split the first and "additional types" is392 * because it should not intervene with the mockAllExcept(type,393 * String...methodNames) method.394 */395 final Class<?>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);396 return createNiceMock(type, WhiteboxImpl.getAllMethodsExcept(type, methodNameToExclude, argumentTypes));397 }398 /**399 * Mock all methods of a class except for a specific one strictly. Use this400 * method only if you have several overloaded methods.401 *402 * @param <T> The type of the mock.403 * @param type The type that'll be used to create a mock instance.404 * @param methodNameToExclude The name of the method not to mock.405 * @param firstArgumentType The type of the first parameter of the method not to mock406 * @param moreTypes Optionally more parameter types that defines the method. Note407 * that this is only needed to separate overloaded methods.408 * @return A mock object of type <T>.409 */410 public static synchronized <T> T createStrictPartialMockForAllMethodsExcept(Class<T> type,411 String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) {412 /*413 * The reason why we've split the first and "additional types" is414 * because it should not intervene with the mockAllExcept(type,415 * String...methodNames) method.416 */417 final Class<?>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);418 return createStrictMock(type, WhiteboxImpl.getAllMethodsExcept(type, methodNameToExclude, argumentTypes));419 }420 /**421 * Mock a single specific method. Use this to handle overloaded methods.422 *423 * @param <T> The type of the mock.424 * @param type The type that'll be used to create a mock instance.425 * @param methodNameToMock The name of the method to mock426 * @param firstArgumentType The type of the first parameter of the method to mock427 * @param additionalArgumentTypes Optionally more parameter types that defines the method. Note428 * that this is only needed to separate overloaded methods.429 * @return A mock object of type <T>.430 */431 public static synchronized <T> T createPartialMock(Class<T> type, String methodNameToMock,432 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {433 return doMockSpecific(type, new DefaultMockStrategy(), new String[]{methodNameToMock}, null,434 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));435 }436 /**437 * Strictly mock a single specific method. Use this to handle overloaded438 * methods.439 *440 * @param <T> The type of the mock.441 * @param type The type that'll be used to create a mock instance.442 * @param methodNameToMock The name of the method to mock443 * @param firstArgumentType The type of the first parameter of the method to mock444 * @param additionalArgumentTypes Optionally more parameter types that defines the method. Note445 * that this is only needed to separate overloaded methods.446 * @return A mock object of type <T>.447 */448 public static synchronized <T> T createStrictPartialMock(Class<T> type, String methodNameToMock,449 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {450 return doMockSpecific(type, new StrictMockStrategy(), new String[]{methodNameToMock}, null,451 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));452 }453 /**454 * Nicely mock a single specific method. Use this to handle overloaded455 * methods.456 *457 * @param <T> The type of the mock.458 * @param type The type that'll be used to create a mock instance.459 * @param methodNameToMock The name of the method to mock460 * @param firstArgumentType The type of the first parameter of the method to mock461 * @param additionalArgumentTypes Optionally more parameter types that defines the method. Note462 * that this is only needed to separate overloaded methods.463 * @return A mock object of type <T>.464 */465 public static synchronized <T> T createNicePartialMock(Class<T> type, String methodNameToMock,466 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {467 return doMockSpecific(type, new NiceMockStrategy(), new String[]{methodNameToMock}, null,468 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));469 }470 /**471 * Mock a single static method.472 *473 * @param clazz The class where the method is specified in.474 * @param methodNameToMock The first argument475 * @param firstArgumentType The first argument type.476 * @param additionalArgumentTypes Optional additional argument types.477 */478 public static synchronized void mockStaticPartial(Class<?> clazz, String methodNameToMock,479 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {480 doMockSpecific(clazz, new DefaultMockStrategy(), new String[]{methodNameToMock}, null,481 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));482 }483 /**484 * Mock a single static method (strict).485 *486 * @param clazz The class where the method is specified in.487 * @param methodNameToMock The first argument488 * @param firstArgumentType The first argument type.489 * @param additionalArgumentTypes Optional additional argument types.490 */491 public static synchronized void mockStaticPartialStrict(Class<?> clazz, String methodNameToMock,492 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {493 doMockSpecific(clazz, new StrictMockStrategy(), new String[]{methodNameToMock}, null,494 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));495 }496 /**497 * Mock a single static method (nice).498 *499 * @param clazz The class where the method is specified in.500 * @param methodNameToMock The first argument501 * @param firstArgumentType The first argument type.502 * @param additionalArgumentTypes Optional additional argument types.503 */504 public static synchronized void mockStaticPartialNice(Class<?> clazz, String methodNameToMock,505 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {506 doMockSpecific(clazz, new NiceMockStrategy(), new String[]{methodNameToMock}, null,507 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));508 }509 /**510 * A utility method that may be used to mock several <b>static</b> methods511 * in an easy way (by just passing in the method names of the method you512 * wish to mock). Note that you cannot uniquely specify a method to mock513 * using this method if there are several methods with the same name in514 * {@code type}. This method will mock ALL methods that match the515 * supplied name regardless of parameter types and signature. If this is the516 * case you should fall-back on using the517 * {@link #mockStatic(Class, Method...)} method instead.518 *519 * @param clazz The class that contains the static methods that should be520 * mocked.521 * @param methodNames The names of the methods that should be mocked. If522 * {@code null}, then this method will have the same effect523 * as just calling {@link #mockStatic(Class, Method...)} with the524 * second parameter as {@code new Method[0]} (i.e. all525 * methods in that class will be mocked).526 */527 public static synchronized void mockStaticPartial(Class<?> clazz, String... methodNames) {528 mockStatic(clazz, Whitebox.getMethods(clazz, methodNames));529 }530 /**531 * A utility method that may be used to mock several <b>static</b> methods532 * (strict) in an easy way (by just passing in the method names of the533 * method you wish to mock). Note that you cannot uniquely specify a method534 * to mock using this method if there are several methods with the same name535 * in {@code type}. This method will mock ALL methods that match the536 * supplied name regardless of parameter types and signature. If this is the537 * case you should fall-back on using the538 * {@link #mockStaticStrict(Class, Method...)} method instead.539 *540 * @param clazz The class that contains the static methods that should be541 * mocked.542 * @param methodNames The names of the methods that should be mocked. If543 * {@code null}, then this method will have the same effect544 * as just calling {@link #mockStatic(Class, Method...)} with the545 * second parameter as {@code new Method[0]} (i.e. all546 * methods in that class will be mocked).547 */548 public static synchronized void mockStaticPartialStrict(Class<?> clazz, String... methodNames) {549 mockStaticStrict(clazz, Whitebox.getMethods(clazz, methodNames));550 }551 /**552 * A utility method that may be used to mock several <b>static</b> methods553 * (nice) in an easy way (by just passing in the method names of the method554 * you wish to mock). Note that you cannot uniquely specify a method to mock555 * using this method if there are several methods with the same name in556 * {@code type}. This method will mock ALL methods that match the557 * supplied name regardless of parameter types and signature. If this is the558 * case you should fall-back on using the559 * {@link #mockStaticStrict(Class, Method...)} method instead.560 *561 * @param clazz The class that contains the static methods that should be562 * mocked.563 * @param methodNames The names of the methods that should be mocked. If564 * {@code null}, then this method will have the same effect565 * as just calling {@link #mockStatic(Class, Method...)} with the566 * second parameter as {@code new Method[0]} (i.e. all567 * methods in that class will be mocked).568 */569 public static synchronized void mockStaticPartialNice(Class<?> clazz, String... methodNames) {570 mockStaticNice(clazz, Whitebox.getMethods(clazz, methodNames));571 }572 static <T> T doMockSpecific(Class<T> type, MockStrategy mockStrategy, String[] methodNamesToMock,573 ConstructorArgs constructorArgs, Class<?>... argumentTypes) {574 List<Method> methods = new LinkedList<Method>();575 for (String methodName : methodNamesToMock) {576 methods.add(WhiteboxImpl.findMethodOrThrowException(type, methodName, argumentTypes));577 }578 final Method[] methodArray = methods.toArray(new Method[0]);579 if (WhiteboxImpl.areAllMethodsStatic(methodArray)) {580 if (mockStrategy instanceof DefaultMockStrategy) {581 mockStatic(type, methodArray);582 } else if (mockStrategy instanceof StrictMockStrategy) {583 mockStaticStrict(type, methodArray);584 } else {585 mockStaticNice(type, methodArray);586 }587 return null;588 }589 T mock = null;590 if (mockStrategy instanceof DefaultMockStrategy) {591 mock = createMock(type, constructorArgs, methodArray);592 } else if (mockStrategy instanceof StrictMockStrategy) {593 mock = createStrictMock(type, constructorArgs, methodArray);594 } else {595 mock = createNiceMock(type, constructorArgs, methodArray);596 }597 return mock;598 }599 /**600 * A utility method that may be used to mock several methods in an easy way601 * (by just passing in the method names of the method you wish to mock).602 * Note that you cannot uniquely specify a method to mock using this method603 * if there are several methods with the same name in {@code type}.604 * This method will mock ALL methods that match the supplied name regardless605 * of parameter types and signature. If this is the case you should606 * fall-back on using the {@link #createMock(Class, Method...)} method607 * instead.608 *609 * @param <T> The type of the mock.610 * @param type The type that'll be used to create a mock instance.611 * @param methodNames The names of the methods that should be mocked. If612 * {@code null}, then this method will have the same effect613 * as just calling {@link #createMock(Class, Method...)} with the614 * second parameter as {@code new Method[0]} (i.e. all615 * methods in that class will be mocked).616 * @return A mock object of type <T>.617 */618 public static synchronized <T> T createPartialMock(Class<T> type, String... methodNames) {619 return createMock(type, Whitebox.getMethods(type, methodNames));620 }621 /**622 * A utility method that may be used to mock several methods in an easy way623 * (by just passing in the method names of the method you wish to mock).624 * Note that you cannot uniquely specify a method to mock using this method625 * if there are several methods with the same name in {@code type}.626 * This method will mock ALL methods that match the supplied name regardless627 * of parameter types and signature. If this is the case you should628 * fall-back on using the {@link #createMock(Class, Method...)} method629 * instead.630 * <p/>631 * With this method you can specify where the class hierarchy the methods632 * are located. This is useful in, for example, situations where class A633 * extends B and both have a method called "mockMe" (A overrides B's mockMe634 * method) and you like to specify the only the "mockMe" method in B should635 * be mocked. "mockMe" in A should be left intact. In this case you should636 * do:637 * <p/>638 * <pre>639 * A tested = createPartialMock(A.class, B.class, &quot;mockMe&quot;);640 * </pre>641 *642 * @param <T> The type of the mock.643 * @param type The type that'll be used to create a mock instance.644 * @param where Where in the class hierarchy the methods resides.645 * @param methodNames The names of the methods that should be mocked. If646 * {@code null}, then this method will have the same effect647 * as just calling {@link #createMock(Class, Method...)} with the648 * second parameter as {@code new Method[0]} (i.e. all649 * methods in that class will be mocked).650 * @return A mock object of type <T>.651 */652 public static synchronized <T> T createPartialMock(Class<T> type, Class<? super T> where, String... methodNames) {653 return createMock(type, Whitebox.getMethods(where, methodNames));654 }655 /**656 * A utility method that may be used to strictly mock several methods in an657 * easy way (by just passing in the method names of the method you wish to658 * mock). Note that you cannot uniquely specify a method to mock using this659 * method if there are several methods with the same name in660 * {@code type}. This method will mock ALL methods that match the661 * supplied name regardless of parameter types and signature. If this is the662 * case you should fall-back on using the663 * {@link #createMock(Class, Method...)} method instead.664 *665 * @param <T> The type of the mock.666 * @param type The type that'll be used to create a mock instance.667 * @param methodNames The names of the methods that should be mocked. If668 * {@code null}, then this method will have the same effect669 * as just calling {@link #createMock(Class, Method...)} with the670 * second parameter as {@code new Method[0]} (i.e. all671 * methods in that class will be mocked).672 * @return A mock object of type <T>.673 */674 public static synchronized <T> T createStrictPartialMock(Class<T> type, String... methodNames) {675 return createStrictMock(type, Whitebox.getMethods(type, methodNames));676 }677 /**678 * A utility method that may be used to strictly mock several methods in an679 * easy way (by just passing in the method names of the method you wish to680 * mock). Note that you cannot uniquely specify a method to mock using this681 * method if there are several methods with the same name in682 * {@code type}. This method will mock ALL methods that match the683 * supplied name regardless of parameter types and signature. If this is the684 * case you should fall-back on using the685 * {@link #createMock(Class, Method...)} method instead.686 * <p/>687 * With this method you can specify where the class hierarchy the methods688 * are located. This is useful in, for example, situations where class A689 * extends B and both have a method called "mockMe" (A overrides B's mockMe690 * method) and you like to specify the only the "mockMe" method in B should691 * be mocked. "mockMe" in A should be left intact. In this case you should692 * do:693 * <p/>694 * <pre>695 * A tested = createPartialMockStrict(A.class, B.class, &quot;mockMe&quot;);696 * </pre>697 *698 * @param <T> The type of the mock.699 * @param type The type that'll be used to create a mock instance.700 * @param where Where in the class hierarchy the methods resides.701 * @param methodNames The names of the methods that should be mocked. If702 * {@code null}, then this method will have the same effect703 * as just calling {@link #createMock(Class, Method...)} with the704 * second parameter as {@code new Method[0]} (i.e. all705 * methods in that class will be mocked).706 * @return A mock object of type <T>.707 */708 public static synchronized <T> T createStrictPartialMock(Class<T> type, Class<? super T> where,709 String... methodNames) {710 return createStrictMock(type, Whitebox.getMethods(where, methodNames));711 }712 /**713 * A utility method that may be used to nicely mock several methods in an714 * easy way (by just passing in the method names of the method you wish to715 * mock). Note that you cannot uniquely specify a method to mock using this716 * method if there are several methods with the same name in717 * {@code type}. This method will mock ALL methods that match the718 * supplied name regardless of parameter types and signature. If this is the719 * case you should fall-back on using the720 * {@link #createMock(Class, Method...)} method instead.721 *722 * @param <T> The type of the mock.723 * @param type The type that'll be used to create a mock instance.724 * @param methodNames The names of the methods that should be mocked. If725 * {@code null}, then this method will have the same effect726 * as just calling {@link #createMock(Class, Method...)} with the727 * second parameter as {@code new Method[0]} (i.e. all728 * methods in that class will be mocked).729 * @return A mock object of type <T>.730 */731 public static synchronized <T> T createNicePartialMock(Class<T> type, String... methodNames) {732 return createNiceMock(type, Whitebox.getMethods(type, methodNames));733 }734 /**735 * A utility method that may be used to nicely mock several methods in an736 * easy way (by just passing in the method names of the method you wish to737 * mock). Note that you cannot uniquely specify a method to mock using this738 * method if there are several methods with the same name in739 * {@code type}. This method will mock ALL methods that match the740 * supplied name regardless of parameter types and signature. If this is the741 * case you should fall-back on using the742 * {@link #createMock(Class, Method...)} method instead.743 * <p/>744 * With this method you can specify where the class hierarchy the methods745 * are located. This is useful in, for example, situations where class A746 * extends B and both have a method called "mockMe" (A overrides B's mockMe747 * method) and you like to specify the only the "mockMe" method in B should748 * be mocked. "mockMe" in A should be left intact. In this case you should749 * do:750 * <p/>751 * <pre>752 * A tested = createPartialMockNice(A.class, B.class, &quot;mockMe&quot;);753 * </pre>754 *755 * @param <T> The type of the mock.756 * @param type The type that'll be used to create a mock instance.757 * @param where Where in the class hierarchy the methods resides.758 * @param methodNames The names of the methods that should be mocked. If759 * {@code null}, then this method will have the same effect760 * as just calling {@link #createMock(Class, Method...)} with the761 * second parameter as {@code new Method[0]} (i.e. all762 * methods in that class will be mocked).763 * @return A mock object of type <T>.764 */765 public static synchronized <T> T createNicePartialMock(Class<T> type, Class<? super T> where, String... methodNames) {766 return createNiceMock(type, Whitebox.getMethods(where, methodNames));767 }768 /**769 * A utility method that may be used to mock several methods in an easy way770 * (by just passing in the method names of the method you wish to mock). The771 * mock object created will support mocking of final methods and invokes the772 * default constructor (even if it's private).773 *774 * @param <T> the type of the mock object775 * @param type the type of the mock object776 * @param methodNames The names of the methods that should be mocked. If777 * {@code null}, then this method will have the same effect778 * as just calling {@link #createMock(Class, Method...)} with the779 * second parameter as {@code new Method[0]} (i.e. all780 * methods in that class will be mocked).781 * @return the mock object.782 */783 public static <T> T createPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames)784 throws Exception {785 return createMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),786 Whitebox.getMethods(type, methodNames));787 }788 /**789 * A utility method that may be used to nicely mock several methods in an790 * easy way (by just passing in the method names of the method you wish to791 * mock). The mock object created will support mocking of final methods and792 * invokes the default constructor (even if it's private).793 *794 * @param <T> the type of the mock object795 * @param type the type of the mock object796 * @param methodNames The names of the methods that should be mocked. If797 * {@code null}, then this method will have the same effect798 * as just calling {@link #createMock(Class, Method...)} with the799 * second parameter as {@code new Method[0]} (i.e. all800 * methods in that class will be mocked).801 * @return the mock object.802 */803 public static <T> T createNicePartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames)804 throws Exception {805 return createNiceMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),806 Whitebox.getMethods(type, methodNames));807 }808 /**809 * A utility method that may be used to strictly mock several methods in an810 * easy way (by just passing in the method names of the method you wish to811 * mock). The mock object created will support mocking of final methods and812 * invokes the default constructor (even if it's private).813 *814 * @param <T> the type of the mock object815 * @param type the type of the mock object816 * @param methodNames The names of the methods that should be mocked. If817 * {@code null}, then this method will have the same effect818 * as just calling {@link #createMock(Class, Method...)} with the819 * second parameter as {@code new Method[0]} (i.e. all820 * methods in that class will be mocked).821 * @return the mock object.822 */823 public static <T> T createStrictPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames)824 throws Exception {825 return createStrictMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),826 Whitebox.getMethods(type, methodNames));827 }828 /**829 * A utility method that may be used to mock several methods in an easy way830 * (by just passing in the method names of the method you wish to mock). The831 * mock object created will support mocking of final and native methods and832 * invokes a specific constructor based on the supplied argument values.833 *834 * @param <T> the type of the mock object835 * @param type the type of the mock object836 * @param methodNames The names of the methods that should be mocked. If837 * {@code null}, then this method will have the same effect838 * as just calling {@link #createMock(Class, Method...)} with the839 * second parameter as {@code new Method[0]} (i.e. all840 * methods in that class will be mocked).841 * @param constructorArguments The constructor arguments that will be used to invoke a842 * certain constructor. (optional)843 * @return the mock object.844 */845 public static <T> T createPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) {846 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);847 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);848 return doMock(type, false, new DefaultMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames));849 }850 /**851 * * A utility method that may be used to strictly mock several methods in852 * an easy way (by just passing in the method names of the method you wish853 * to mock). The mock object created will support mocking of final and854 * native methods and invokes a specific constructor based on the supplied855 * argument values.856 *857 * @param <T> the type of the mock object858 * @param type the type of the mock object859 * @param methodNames The names of the methods that should be mocked. If860 * {@code null}, then this method will have the same effect861 * as just calling {@link #createMock(Class, Method...)} with the862 * second parameter as {@code new Method[0]} (i.e. all863 * methods in that class will be mocked).864 * @param constructorArguments The constructor arguments that will be used to invoke a865 * certain constructor. (optional)866 * @return the mock object.867 */868 public static <T> T createStrictPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) {869 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);870 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);871 return doMock(type, false, new StrictMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames));872 }873 /**874 * * A utility method that may be used to nicely mock several methods in an875 * easy way (by just passing in the method names of the method you wish to876 * mock). The mock object created will support mocking of final and native877 * methods and invokes a specific constructor based on the supplied argument878 * values.879 *880 * @param <T> the type of the mock object881 * @param type the type of the mock object882 * @param methodNames The names of the methods that should be mocked. If883 * {@code null}, then this method will have the same effect884 * as just calling {@link #createMock(Class, Method...)} with the885 * second parameter as {@code new Method[0]} (i.e. all886 * methods in that class will be mocked).887 * @param constructorArguments The constructor arguments that will be used to invoke a888 * certain constructor. (optional)889 * @return the mock object.890 */891 public static <T> T createNicePartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) {892 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);893 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);894 return doMock(type, false, new NiceMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames));895 }896 /**897 * A utility method that may be used to mock several methods in an easy way898 * (by just passing in the method names of the method you wish to mock). Use899 * this to handle overloaded methods. The mock object created will support900 * mocking of final and native methods and invokes a specific constructor901 * based on the supplied argument values.902 *903 * @param <T> the type of the mock object904 * @param type the type of the mock object905 * @param methodName The names of the methods that should be mocked. If906 * {@code null}, then this method will have the same effect907 * as just calling {@link #createMock(Class, Method...)} with the908 * second parameter as {@code new Method[0]} (i.e. all909 * methods in that class will be mocked).910 * @param methodParameterTypes Parameter types that defines the method. Note that this is911 * only needed to separate overloaded methods.912 * @param constructorArguments The constructor arguments that will be used to invoke a913 * certain constructor. (optional)914 * @return the mock object.915 */916 public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,917 Object... constructorArguments) {918 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);919 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);920 return doMockSpecific(type, new DefaultMockStrategy(), new String[]{methodName}, constructorArgs,921 methodParameterTypes);922 }923 /**924 * A utility method that may be used to strictly mock several methods in an925 * easy way (by just passing in the method names of the method you wish to926 * mock). Use this to handle overloaded methods. The mock object created927 * will support mocking of final and native methods and invokes a specific928 * constructor based on the supplied argument values.929 *930 * @param <T> the type of the mock object931 * @param type the type of the mock object932 * @param methodName The names of the methods that should be mocked. If933 * {@code null}, then this method will have the same effect934 * as just calling {@link #createMock(Class, Method...)} with the935 * second parameter as {@code new Method[0]} (i.e. all936 * methods in that class will be mocked).937 * @param methodParameterTypes Parameter types that defines the method. Note that this is938 * only needed to separate overloaded methods.939 * @param constructorArguments The constructor arguments that will be used to invoke a940 * certain constructor. (optional)941 * @return the mock object.942 */943 public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,944 Object... constructorArguments) {945 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);946 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);947 return doMockSpecific(type, new StrictMockStrategy(), new String[]{methodName}, constructorArgs,948 methodParameterTypes);949 }950 /**951 * A utility method that may be used to nicely mock several methods in an952 * easy way (by just passing in the method names of the method you wish to953 * mock). Use this to handle overloaded methods. The mock object created954 * will support mocking of final and native methods and invokes a specific955 * constructor based on the supplied argument values.956 *957 * @param <T> the type of the mock object958 * @param type the type of the mock object959 * @param methodName The names of the methods that should be mocked. If960 * {@code null}, then this method will have the same effect961 * as just calling {@link #createMock(Class, Method...)} with the962 * second parameter as {@code new Method[0]} (i.e. all963 * methods in that class will be mocked).964 * @param methodParameterTypes Parameter types that defines the method. Note that this is965 * only needed to separate overloaded methods.966 * @param constructorArguments The constructor arguments that will be used to invoke a967 * certain constructor. (optional)968 * @return the mock object.969 */970 public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,971 Object... constructorArguments) {972 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);973 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);974 return doMockSpecific(type, new NiceMockStrategy(), new String[]{methodName}, constructorArgs,975 methodParameterTypes);976 }977 /**978 * A utility method that may be used to mock several methods in an easy way979 * (by just passing in the method names of the method you wish to mock). Use980 * this to handle overloaded methods <i>and</i> overloaded constructors. The981 * mock object created will support mocking of final and native methods and982 * invokes a specific constructor based on the supplied argument values.983 *984 * @param <T> the type of the mock object985 * @param type the type of the mock object986 * @param methodName The names of the methods that should be mocked. If987 * {@code null}, then this method will have the same effect988 * as just calling {@link #createMock(Class, Method...)} with the989 * second parameter as {@code new Method[0]} (i.e. all990 * methods in that class will be mocked).991 * @param methodParameterTypes Parameter types that defines the method. Note that this is992 * only needed to separate overloaded methods.993 * @param constructorArguments The constructor arguments that will be used to invoke a994 * certain constructor.995 * @param constructorParameterTypes Parameter types that defines the constructor that should be996 * invoked. Note that this is only needed to separate overloaded997 * constructors.998 * @return the mock object.999 */1000 public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1001 Object[] constructorArguments, Class<?>[] constructorParameterTypes) {1002 ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),1003 constructorArguments);1004 return doMockSpecific(type, new DefaultMockStrategy(), new String[]{methodName}, constructorArgs,1005 methodParameterTypes);1006 }1007 /**1008 * A utility method that may be used to strictly mock several methods in an1009 * easy way (by just passing in the method names of the method you wish to1010 * mock). Use this to handle overloaded methods <i>and</i> overloaded1011 * constructors. The mock object created will support mocking of final and1012 * native methods and invokes a specific constructor based on the supplied1013 * argument values.1014 *1015 * @param <T> the type of the mock object1016 * @param type the type of the mock object1017 * @param methodName The names of the methods that should be mocked. If1018 * {@code null}, then this method will have the same effect1019 * as just calling {@link #createMock(Class, Method...)} with the1020 * second parameter as {@code new Method[0]} (i.e. all1021 * methods in that class will be mocked).1022 * @param methodParameterTypes Parameter types that defines the method. Note that this is1023 * only needed to separate overloaded methods.1024 * @param constructorArguments The constructor arguments that will be used to invoke a1025 * certain constructor.1026 * @param constructorParameterTypes Parameter types that defines the constructor that should be1027 * invoked. Note that this is only needed to separate overloaded1028 * constructors.1029 * @return the mock object.1030 */1031 public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1032 Object[] constructorArguments, Class<?>[] constructorParameterTypes) {1033 ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),1034 constructorArguments);1035 return doMockSpecific(type, new StrictMockStrategy(), new String[]{methodName}, constructorArgs,1036 methodParameterTypes);1037 }1038 /**1039 * A utility method that may be used to nicely mock several methods in an1040 * easy way (by just passing in the method names of the method you wish to1041 * mock). Use this to handle overloaded methods <i>and</i> overloaded1042 * constructors. The mock object created will support mocking of final and1043 * native methods and invokes a specific constructor based on the supplied1044 * argument values.1045 *1046 * @param <T> the type of the mock object1047 * @param type the type of the mock object1048 * @param methodName The names of the methods that should be mocked. If1049 * {@code null}, then this method will have the same effect1050 * as just calling {@link #createMock(Class, Method...)} with the1051 * second parameter as {@code new Method[0]} (i.e. all1052 * methods in that class will be mocked).1053 * @param methodParameterTypes Parameter types that defines the method. Note that this is1054 * only needed to separate overloaded methods.1055 * @param constructorArguments The constructor arguments that will be used to invoke a1056 * certain constructor.1057 * @param constructorParameterTypes Parameter types that defines the constructor that should be1058 * invoked. Note that this is only needed to separate overloaded1059 * constructors.1060 * @return the mock object.1061 */1062 public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1063 Object[] constructorArguments, Class<?>[] constructorParameterTypes) {1064 ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),1065 constructorArguments);1066 return doMockSpecific(type, new NiceMockStrategy(), new String[]{methodName}, constructorArgs,1067 methodParameterTypes);1068 }1069 /**1070 * Used to specify expectations on private static methods. If possible use1071 * variant with only method name.1072 */1073 public static synchronized <T> IExpectationSetters<T> expectPrivate(Class<?> clazz, Method method,1074 Object... arguments) throws Exception {1075 return doExpectPrivate(clazz, method, arguments);1076 }1077 /**1078 * Used to specify expectations on private methods. If possible use variant1079 * with only method name.1080 */1081 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, Method method,1082 Object... arguments) throws Exception {1083 return doExpectPrivate(instance, method, arguments);1084 }1085 /**1086 * Used to specify expectations on private methods. Use this method to1087 * handle overloaded methods.1088 */1089 @SuppressWarnings("all")1090 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1091 Class<?>[] parameterTypes, Object... arguments) throws Exception {1092 if (arguments == null) {1093 arguments = new Object[0];1094 }1095 if (instance == null) {1096 throw new IllegalArgumentException("instance cannot be null.");1097 } else if (arguments.length != parameterTypes.length) {1098 throw new IllegalArgumentException(1099 "The length of the arguments must be equal to the number of parameter types.");1100 }1101 Method foundMethod = Whitebox.getMethod(instance.getClass(), methodName, parameterTypes);1102 WhiteboxImpl.throwExceptionIfMethodWasNotFound(instance.getClass(), methodName, foundMethod, parameterTypes);1103 return doExpectPrivate(instance, foundMethod, arguments);1104 }1105 /**1106 * Used to specify expectations on methods using the method name. Works on1107 * for example private or package private methods.1108 */1109 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1110 Object... arguments) throws Exception {1111 if (instance == null) {1112 throw new IllegalArgumentException("Instance or class cannot be null.");1113 }1114 return expectPrivate(instance, methodName, Whitebox.getType(instance), arguments);1115 }1116 /**1117 * Used to specify expectations on methods without specifying a method name.1118 * Works on for example private or package private methods. PowerMock tries1119 * to find a unique method to expect based on the argument parameters. If1120 * PowerMock is unable to locate a unique method you need to revert to using1121 * {@link #expectPrivate(Object, String, Object...)}.1122 */1123 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, Object... arguments)1124 throws Exception {1125 return expectPrivate(instance, null, Whitebox.getType(instance), arguments);1126 }1127 /**1128 * Used to specify expectations on methods using the method name at a1129 * specific place in the class hierarchy (specified by the1130 * {@code where} parameter). Works on for example private or package1131 * private methods.1132 * <p/>1133 * Use this for overloaded methods.1134 */1135 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1136 Class<?> where, Class<?>[] parameterTypes, Object... arguments) throws Exception {1137 if (instance == null) {1138 throw new IllegalArgumentException("Instance or class to expect cannot be null.");1139 }1140 Method[] methods = null;1141 if (methodName != null) {1142 if (parameterTypes == null) {1143 methods = Whitebox.getMethods(where, methodName);1144 } else {1145 methods = new Method[]{Whitebox.getMethod(where, methodName, parameterTypes)};1146 }1147 }1148 Method methodToExpect;1149 if (methods != null && methods.length == 1) {1150 methodToExpect = methods[0];1151 } else {1152 methodToExpect = WhiteboxImpl.findMethodOrThrowException(instance, null, methodName, arguments);1153 }1154 return doExpectPrivate(instance, methodToExpect, arguments);1155 }1156 /**1157 * Used to specify expectations on methods using the method name at a1158 * specific place in the class hierarchy (specified by the1159 * {@code where} parameter). Works on for example private or package1160 * private methods.1161 */1162 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1163 Class<?> where, Object... arguments) throws Exception {1164 return expectPrivate(instance, methodName, where, null, arguments);1165 }1166 /**1167 * This method just delegates to EasyMock class extensions1168 * {@link org.easymock.EasyMock#expectLastCall()} method.1169 *1170 * @return The expectation setter.1171 * @see org.easymock.EasyMock#expectLastCall()1172 */1173 public static synchronized IExpectationSetters<Object> expectLastCall() {1174 return org.easymock.EasyMock.expectLastCall();1175 }1176 /**1177 * Sometimes it is useful to allow replay and verify on non-mocks. For1178 * example when using partial mocking in some tests and no mocking in other1179 * test-methods, but using the same setUp and tearDown.1180 */1181 public static synchronized void niceReplayAndVerify() {1182 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, true);1183 }1184 /**1185 * Test if a object is a mock created by EasyMock or not.1186 */1187 private static boolean isEasyMocked(Object mock) {1188 return Enhancer.isEnhanced(mock.getClass()) || Proxy.isProxyClass(mock.getClass());1189 }1190 /**1191 * Replay all classes and mock objects known by PowerMock. This includes all1192 * classes that are prepared for test using the {@link PrepareForTest} or1193 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1194 * their static initializers removed by using the1195 * {@link SuppressStaticInitializationFor} annotation. It also includes all1196 * mock instances created by PowerMock such as those created or used by1197 * {@link #createMock(Class, Method...)},1198 * {@link #mockStatic(Class, Method...)},1199 * {@link #expectNew(Class, Object...)},1200 * {@link #createPartialMock(Class, String...)} etc.1201 * <p/>1202 * To make it easy to pass in additional mocks <i>not</i> created by the1203 * PowerMock API you can optionally specify them as <tt>additionalMocks</tt>1204 * . These are typically those mock objects you have created using pure1205 * EasyMock or EasyMock class extensions. No additional mocks needs to be1206 * specified if you're only using PowerMock API methods.1207 * <p/>1208 * Note that the <tt>additionalMocks</tt> are also automatically verified1209 * when invoking the {@link #verifyAll()} method.1210 *1211 * @param additionalMocks Mocks not created by the PowerMock API. These are typically1212 * those mock objects you have created using pure EasyMock or1213 * EasyMock class extensions.1214 */1215 public static synchronized void replayAll(Object... additionalMocks) {1216 MockRepository.addObjectsToAutomaticallyReplayAndVerify(additionalMocks);1217 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1218 replay(classToReplayOrVerify);1219 }1220 }1221 /**1222 * Reset all classes and mock objects known by PowerMock. This includes all1223 * classes that are prepared for test using the {@link PrepareForTest} or1224 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1225 * their static initializers removed by using the1226 * {@link SuppressStaticInitializationFor} annotation. It also includes all1227 * mock instances created by PowerMock such as those created or used by1228 * {@link #createMock(Class, Method...)},1229 * {@link #mockStatic(Class, Method...)},1230 * {@link #expectNew(Class, Object...)},1231 * {@link #createPartialMock(Class, String...)} etc.1232 * <p/>1233 * To make it easy to pass in additional mocks <i>not</i> created by the1234 * PowerMock API you can optionally specify them as <tt>additionalMocks</tt>1235 * . These are typically those mock objects you have created using pure1236 * EasyMock or EasyMock class extensions. No additional mocks needs to be1237 * specified if you're only using PowerMock API methods.1238 *1239 * @param additionalMocks Mocks not created by the PowerMock API. These are typically1240 * those mock objects you have created using pure EasyMock or1241 * EasyMock class extensions.1242 */1243 public static synchronized void resetAll(Object... additionalMocks) {1244 MockRepository.addObjectsToAutomaticallyReplayAndVerify(additionalMocks);1245 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1246 reset(classToReplayOrVerify);1247 }1248 }1249 /**1250 * Reset a list of class mocks.1251 */1252 public static synchronized void reset(Class<?>... classMocks) {1253 for (Class<?> type : classMocks) {1254 final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type);1255 if (invocationHandler != null) {1256 invocationHandler.reset();1257 }1258 NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type);1259 if (newInvocationControl != null) {1260 try {1261 newInvocationControl.reset();1262 } catch (AssertionError e) {1263 NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type);1264 }1265 }1266 }1267 }1268 /**1269 * Reset a list of mock objects or classes.1270 */1271 public static synchronized void reset(Object... mocks) {1272 try {1273 for (Object mock : mocks) {1274 if (mock instanceof Class<?>) {1275 reset((Class<?>) mock);1276 } else {1277 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1278 if (invocationControl != null) {1279 invocationControl.reset();1280 } else {1281 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1282 // ignore non-mock1283 } else {1284 /*1285 * Delegate to easy mock class extension if we have1286 * no handler registered for this object.1287 */1288 try {1289 org.easymock.EasyMock.reset(mock);1290 } catch (RuntimeException e) {1291 throw new RuntimeException(mock + " is not a mock object", e);1292 }1293 }1294 }1295 }1296 }1297 } catch (Throwable t) {1298 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false);1299 if (t instanceof RuntimeException) {1300 throw (RuntimeException) t;1301 } else if (t instanceof Error) {1302 throw (Error) t;1303 }1304 throw new RuntimeException(t);1305 }1306 }1307 /**1308 * Verify all classes and mock objects known by PowerMock. This includes all1309 * classes that are prepared for test using the {@link PrepareForTest} or1310 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1311 * their static initializers removed by using the1312 * {@link SuppressStaticInitializationFor} annotation. It also includes all1313 * mock instances created by PowerMock such as those created or used by1314 * {@link #createMock(Class, Method...)},1315 * {@link #mockStatic(Class, Method...)},1316 * {@link #expectNew(Class, Object...)},1317 * {@link #createPartialMock(Class, String...)} etc.1318 * <p/>1319 * Note that all <tt>additionalMocks</tt> passed to the1320 * {@link #replayAll(Object...)} method are also verified here1321 * automatically.1322 */1323 public static synchronized void verifyAll() {1324 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1325 verify(classToReplayOrVerify);1326 }1327 }1328 /**1329 * Switches the mocks or classes to replay mode. Note that you must use this1330 * method when using PowerMock!1331 *1332 * @param mocks mock objects or classes loaded by PowerMock.1333 * @throws RuntimeException If something unexpected goes wrong.1334 */1335 public static synchronized void replay(Object... mocks) {1336 try {1337 for (Object mock : mocks) {1338 if (mock instanceof Class<?>) {1339 replay((Class<?>) mock);1340 } else {1341 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1342 if (invocationControl != null) {1343 invocationControl.replay();1344 } else {1345 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1346 // ignore non-mock1347 } else {1348 /*1349 * Delegate to easy mock class extension if we have1350 * no handler registered for this object.1351 */1352 try {1353 org.easymock.EasyMock.replay(mock);1354 } catch (RuntimeException e) {1355 throw new RuntimeException(mock + " is not a mock object", e);1356 }1357 }1358 }1359 }1360 }1361 } catch (Throwable t) {1362 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false);1363 if (t instanceof RuntimeException) {1364 throw (RuntimeException) t;1365 } else if (t instanceof Error) {1366 throw (Error) t;1367 }1368 throw new RuntimeException(t);1369 }1370 }1371 /**1372 * Switches the mocks or classes to verify mode. Note that you must use this1373 * method when using PowerMock!1374 *1375 * @param objects mock objects or classes loaded by PowerMock.1376 */1377 public static synchronized void verify(Object... objects) {1378 for (Object mock : objects) {1379 if (mock instanceof Class<?>) {1380 verifyClass((Class<?>) mock);1381 } else {1382 EasyMockMethodInvocationControl invocationControl = (EasyMockMethodInvocationControl) MockRepository.getInstanceMethodInvocationControl(mock);1383 if (invocationControl != null) {1384 invocationControl.verify();1385 } else {1386 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1387 // ignore non-mock1388 } else {1389 /*1390 * Delegate to easy mock class extension if we have no1391 * handler registered for this object.1392 */1393 try {1394 org.easymock.EasyMock.verify(mock);1395 } catch (RuntimeException e) {1396 throw new RuntimeException(mock + " is not a mock object", e);1397 }1398 }1399 }1400 }1401 }1402 }1403 /**1404 * Convenience method for createMock followed by expectNew.1405 *1406 * @param type The class that should be mocked.1407 * @param arguments The constructor arguments.1408 * @return A mock object of the same type as the mock.1409 * @throws Exception1410 */1411 public static synchronized <T> T createMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1412 T mock = createMock(type);1413 expectNew(type, arguments).andReturn(mock);1414 return mock;1415 }1416 /**1417 * Convenience method for createMock followed by expectNew when PowerMock1418 * cannot determine which constructor to use automatically. This happens1419 * when you have one constructor taking a primitive type and another one1420 * taking the wrapper type of the primitive. For example {@code int}1421 * and {@code Integer}.1422 *1423 * @param type The class that should be mocked.1424 * @param parameterTypes The constructor parameter types.1425 * @param arguments The constructor arguments.1426 * @return A mock object of the same type as the mock.1427 * @throws Exception1428 */1429 public static synchronized <T> T createMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1430 Object... arguments) throws Exception {1431 T mock = createMock(type);1432 expectNew(type, parameterTypes, arguments).andReturn(mock);1433 return mock;1434 }1435 /**1436 * Convenience method for createNiceMock followed by expectNew.1437 *1438 * @param type The class that should be mocked.1439 * @param arguments The constructor arguments.1440 * @return A mock object of the same type as the mock.1441 * @throws Exception1442 */1443 public static synchronized <T> T createNiceMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1444 T mock = createNiceMock(type);1445 IExpectationSetters<T> expectationSetters = expectNiceNew(type, arguments);1446 if (expectationSetters != null) {1447 expectationSetters.andReturn(mock);1448 }1449 return mock;1450 }1451 /**1452 * Convenience method for createNiceMock followed by expectNew when1453 * PowerMock cannot determine which constructor to use automatically. This1454 * happens when you have one constructor taking a primitive type and another1455 * one taking the wrapper type of the primitive. For example1456 * {@code int} and {@code Integer}.1457 *1458 * @param type The class that should be mocked.1459 * @param parameterTypes The constructor parameter types.1460 * @param arguments The constructor arguments.1461 * @return A mock object of the same type as the mock.1462 * @throws Exception1463 */1464 public static synchronized <T> T createNiceMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1465 Object... arguments) throws Exception {1466 T mock = createNiceMock(type);1467 IExpectationSetters<T> expectationSetters = expectNiceNew(type, parameterTypes, arguments);1468 if (expectationSetters != null) {1469 expectationSetters.andReturn(mock);1470 }1471 return mock;1472 }1473 /**1474 * Convenience method for createStrictMock followed by expectNew.1475 *1476 * @param type The class that should be mocked.1477 * @param arguments The constructor arguments.1478 * @return A mock object of the same type as the mock.1479 * @throws Exception1480 */1481 public static synchronized <T> T createStrictMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1482 T mock = createStrictMock(type);1483 expectStrictNew(type, arguments).andReturn(mock);1484 return mock;1485 }1486 /**1487 * Convenience method for createStrictMock followed by expectNew when1488 * PowerMock cannot determine which constructor to use automatically. This1489 * happens when you have one constructor taking a primitive type and another1490 * one taking the wrapper type of the primitive. For example1491 * {@code int} and {@code Integer}.1492 *1493 * @param type The class that should be mocked.1494 * @param parameterTypes The constructor parameter types.1495 * @param arguments The constructor arguments.1496 * @return A mock object of the same type as the mock.1497 * @throws Exception1498 */1499 public static synchronized <T> T createStrictMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1500 Object... arguments) throws Exception {1501 T mock = createStrictMock(type);1502 expectStrictNew(type, parameterTypes, arguments).andReturn(mock);1503 return mock;1504 }1505 /**1506 * Allows specifying expectations on new invocations. For example you might1507 * want to throw an exception or return a mock. Note that you must replay1508 * the class when using this method since this behavior is part of the class1509 * mock.1510 * <p/>1511 * Use this method when you need to specify parameter types for the1512 * constructor when PowerMock cannot determine which constructor to use1513 * automatically. In most cases you should use1514 * {@link #expectNew(Class, Object...)} instead.1515 */1516 public static synchronized <T> IExpectationSetters<T> expectNew(Class<T> type, Class<?>[] parameterTypes,1517 Object... arguments) throws Exception {1518 return doExpectNew(type, new DefaultMockStrategy(), parameterTypes, arguments);1519 }1520 @SuppressWarnings("unchecked")1521 private static <T> IExpectationSetters<T> doExpectNew(Class<T> type, MockStrategy mockStrategy,1522 Class<?>[] parameterTypes, Object... arguments) throws Exception {1523 if (type == null) {1524 throw new IllegalArgumentException("type cannot be null");1525 } else if (mockStrategy == null) {1526 throw new IllegalArgumentException("Internal error: Mock strategy cannot be null");1527 }1528 final boolean isNiceMock = mockStrategy instanceof NiceMockStrategy;1529 final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(type);1530 if (!isNiceMock) {1531 if (parameterTypes == null) {1532 WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments);1533 } else {1534 WhiteboxImpl.getConstructor(unmockedType, parameterTypes);1535 }1536 }1537 /*1538 * Check if this type has been mocked before1539 */1540 NewInvocationControl<IExpectationSetters<T>> newInvocationControl = (NewInvocationControl<IExpectationSetters<T>>) MockRepository1541 .getNewInstanceControl(unmockedType);1542 if (newInvocationControl == null) {1543 InvocationSubstitute<T> mock = doMock(InvocationSubstitute.class, false, mockStrategy, null,1544 (Method[]) null);1545 newInvocationControl = new EasyMockNewInvocationControl<T>(mock, type);1546 MockRepository.putNewInstanceControl(type, newInvocationControl);1547 MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getOriginalUnmockedType(type));1548 }1549 if (isNiceMock && (arguments == null || arguments.length == 0)) {1550 return null;1551 }1552 return newInvocationControl.expectSubstitutionLogic(arguments);1553 }1554 /**1555 * Allows specifying expectations on new invocations. For example you might1556 * want to throw an exception or return a mock. Note that you must replay1557 * the class when using this method since this behavior is part of the class1558 * mock.1559 */1560 public static synchronized <T> IExpectationSetters<T> expectNew(Class<T> type, Object... arguments)1561 throws Exception {1562 return doExpectNew(type, new DefaultMockStrategy(), null, arguments);1563 }1564 /**1565 * Allows specifying expectations on new invocations for private member1566 * (inner) classes, local or anonymous classes. For example you might want1567 * to throw an exception or return a mock. Note that you must replay the1568 * class when using this method since this behavior is part of the class1569 * mock.1570 *1571 * @param fullyQualifiedName The fully-qualified name of the inner/local/anonymous type to1572 * expect.1573 * @param arguments Optional number of arguments.1574 */1575 @SuppressWarnings("unchecked")1576 public static synchronized <T> IExpectationSetters<T> expectNew(String fullyQualifiedName, Object... arguments)1577 throws Exception {1578 final Class<?> forName = Class.forName(fullyQualifiedName);1579 return (IExpectationSetters<T>) doExpectNew(forName, new DefaultMockStrategy(), null, arguments);1580 }1581 /**1582 * Allows specifying expectations on new invocations. For example you might1583 * want to throw an exception or return a mock.1584 * <p/>1585 * This method checks the order of constructor invocations.1586 * <p/>1587 * Note that you must replay the class when using this method since this1588 * behavior is part of the class mock.1589 */1590 public static synchronized <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Object... arguments)1591 throws Exception {1592 return doExpectNew(type, new StrictMockStrategy(), null, arguments);1593 }1594 /**1595 * Allows specifying expectations on new invocations. For example you might1596 * want to throw an exception or return a mock. Note that you must replay1597 * the class when using this method since this behavior is part of the class1598 * mock.1599 * <p/>1600 * This method checks the order of constructor invocations.1601 * <p/>1602 * Use this method when you need to specify parameter types for the1603 * constructor when PowerMock cannot determine which constructor to use1604 * automatically. In most cases you should use1605 * {@link #expectNew(Class, Object...)} instead.1606 */1607 public static synchronized <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Class<?>[] parameterTypes,1608 Object... arguments) throws Exception {1609 return doExpectNew(type, new StrictMockStrategy(), parameterTypes, arguments);1610 }1611 /**1612 * Allows specifying expectations on new invocations. For example you might1613 * want to throw an exception or return a mock.1614 * <p/>1615 * This method allows any number of calls to a new constructor without1616 * throwing an exception.1617 * <p/>1618 * Note that you must replay the class when using this method since this1619 * behavior is part of the class mock.1620 */1621 public static synchronized <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Object... arguments)1622 throws Exception {1623 return doExpectNew(type, new NiceMockStrategy(), null, arguments);1624 }1625 /**1626 * Allows specifying expectations on new invocations. For example you might1627 * want to throw an exception or return a mock. Note that you must replay1628 * the class when using this method since this behavior is part of the class1629 * mock.1630 * <p/>1631 * This method allows any number of calls to a new constructor without1632 * throwing an exception.1633 * <p/>1634 * Use this method when you need to specify parameter types for the1635 * constructor when PowerMock cannot determine which constructor to use1636 * automatically. In most cases you should use1637 * {@link #expectNew(Class, Object...)} instead.1638 */1639 public static synchronized <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Class<?>[] parameterTypes,1640 Object... arguments) throws Exception {1641 return doExpectNew(type, new NiceMockStrategy(), parameterTypes, arguments);1642 }1643 /**1644 * Suppress constructor calls on specific constructors only.1645 *1646 * @deprecated Use {@link #suppress(Constructor[])} instead.1647 */1648 @Deprecated1649 public static synchronized void suppressConstructor(Constructor<?>... constructors) {1650 SuppressCode.suppressConstructor(constructors);1651 }1652 /**1653 * This method can be used to suppress the code in a specific constructor.1654 *1655 * @param clazz The class where the constructor is located.1656 * @param parameterTypes The parameter types of the constructor to suppress.1657 * @deprecated Use {@link #suppress(Constructor)} instead.1658 */1659 @Deprecated1660 public static synchronized void suppressSpecificConstructor(Class<?> clazz, Class<?>... parameterTypes) {1661 SuppressCode.suppressSpecificConstructor(clazz, parameterTypes);1662 }1663 /**1664 * Suppress all constructors in the given class and it's super classes.1665 *1666 * @param classes The classes whose constructors will be suppressed.1667 * @deprecated Use {@link #suppress(Constructor[])} instead.1668 */1669 @Deprecated1670 public static synchronized void suppressConstructor(Class<?>... classes) {1671 SuppressCode.suppressConstructor(classes);1672 }1673 /**1674 * Suppress all constructors in the given class.1675 *1676 * @param clazz The classes whose constructors will be suppressed.1677 * @param excludePrivateConstructors optionally keep code in private constructors1678 * @deprecated Use {@link #suppress(Constructor[])} instead.1679 */1680 @Deprecated1681 public static synchronized void suppressConstructor(Class<?> clazz, boolean excludePrivateConstructors) {1682 SuppressCode.suppressConstructor(clazz, excludePrivateConstructors);1683 }1684 /**1685 * Suppress specific fields. This works on both instance methods and static1686 * methods. Note that replay and verify are not needed as this is not part1687 * of a mock behavior.1688 *1689 * @deprecated Use {@link #suppress(Field[])} instead.1690 */1691 @Deprecated1692 public static synchronized void suppressField(Field... fields) {1693 SuppressCode.suppressField(fields);1694 }1695 /**1696 * Suppress all fields for these classes.1697 *1698 * @deprecated Use {@link #suppress(Field[])} instead.1699 */1700 @Deprecated1701 public static synchronized void suppressField(Class<?>[] classes) {1702 SuppressCode.suppressField(classes);1703 }1704 /**1705 * Suppress multiple methods for a class.1706 *1707 * @param clazz The class whose methods will be suppressed.1708 * @param fieldNames The names of the methods that'll be suppressed. If field names1709 * are empty, <i>all</i> fields in the supplied class will be1710 * suppressed.1711 * @deprecated Use {@link #suppress(Field)} instead.1712 */1713 @Deprecated1714 public static synchronized void suppressField(Class<?> clazz, String... fieldNames) {1715 SuppressCode.suppressField(clazz, fieldNames);1716 }1717 /**1718 * Suppress specific method calls on all types containing this method. This1719 * works on both instance methods and static methods. Note that replay and1720 * verify are not needed as this is not part of a mock behavior.1721 *1722 * @deprecated Use {@link #suppress(Method[])} instead.1723 */1724 @Deprecated1725 public static synchronized void suppressMethod(Method... methods) {1726 SuppressCode.suppressMethod(methods);1727 }1728 /**1729 * Suppress all methods for these classes.1730 *1731 * @param cls The first class whose methods will be suppressed.1732 * @param additionalClasses Additional classes whose methods will be suppressed.1733 * @deprecated Use {@link #suppress(Method[])} instead.1734 */1735 @Deprecated1736 public static synchronized void suppressMethod(Class<?> cls, Class<?>... additionalClasses) {1737 SuppressCode.suppressMethod(cls, additionalClasses);1738 }1739 /**1740 * Suppress all methods for these classes.1741 *1742 * @param classes Classes whose methods will be suppressed.1743 * @deprecated Use {@link #suppress(Method[])} instead.1744 */1745 @Deprecated1746 public static synchronized void suppressMethod(Class<?>[] classes) {1747 SuppressCode.suppressMethod(classes);1748 }1749 /**1750 * Suppress multiple methods for a class.1751 *1752 * @param clazz The class whose methods will be suppressed.1753 * @param methodName The first method to be suppress in class {@code clazz}.1754 * @param additionalMethodNames Additional methods to suppress in class {@code clazz}.1755 * @deprecated Use {@link #suppress(Method[])} instead.1756 */1757 @Deprecated1758 public static synchronized void suppressMethod(Class<?> clazz, String methodName, String... additionalMethodNames) {1759 SuppressCode.suppressMethod(clazz, methodName, additionalMethodNames);1760 }1761 /**1762 * Suppress multiple methods for a class.1763 *1764 * @param clazz The class whose methods will be suppressed.1765 * @param methodNames Methods to suppress in class {@code clazz}.1766 * @deprecated Use {@link #suppress(Method[])} instead.1767 */1768 @Deprecated1769 public static synchronized void suppressMethod(Class<?> clazz, String[] methodNames) {1770 SuppressCode.suppressMethod(clazz, methodNames);1771 }1772 /**1773 * Suppress all methods for this class.1774 *1775 * @param clazz The class which methods will be suppressed.1776 * @param excludePrivateMethods optionally not suppress private methods1777 * @deprecated Use {@link #suppress(Method[])} instead.1778 */1779 @Deprecated1780 public static synchronized void suppressMethod(Class<?> clazz, boolean excludePrivateMethods) {1781 SuppressCode.suppressMethod(clazz, excludePrivateMethods);1782 }1783 /**1784 * Suppress a specific method call. Use this for overloaded methods.1785 *1786 * @deprecated Use {@link #suppress(Method)} instead.1787 */1788 @Deprecated1789 public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {1790 SuppressCode.suppressMethod(clazz, methodName, parameterTypes);1791 }1792 @SuppressWarnings("unchecked")1793 private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy,1794 ConstructorArgs constructorArgs, Method... methods) {1795 if (type == null) {1796 throw new IllegalArgumentException("The class to mock cannot be null");1797 }1798 /*1799 * Clear the EasyMock state after the test method is executed.1800 */1801 MockRepository.addAfterMethodRunner(new Runnable() {1802 @Override1803 public void run() {1804 LastControl.reportLastControl(null);1805 }1806 });1807 IMocksControl control = mockStrategy.createMockControl(type);1808 MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());1809 T mock;1810 if (type.isInterface()) {1811 mock = control.createMock(type);1812 } else if (type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers())) {1813 Class<?> replicaType = createReplicaType(type, isStatic, constructorArgs);1814 final Object replica = doCreateMock(replicaType, constructorArgs, control, methods);1815 control = mockStrategy.createMockControl(replicaType);1816 MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);1817 final Set<Method> methodsToMock = toSet(methods);1818 if (isStatic) {1819 MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h,1820 methodsToMock, replica));1821 MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);1822 return null;1823 } else {1824 final T newInstance;1825 if (constructorArgs == null) {1826 newInstance = Whitebox.newInstance(type);1827 DefaultFieldValueGenerator.fillWithDefaultValues(newInstance);1828 } else {1829 try {1830 newInstance = (T) constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs());1831 } catch (Exception e) {1832 throw new RuntimeException("Internal error", e);1833 }1834 }1835 MockRepository.putInstanceMethodInvocationControl(newInstance,1836 new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));1837 if (!(newInstance instanceof InvocationSubstitute<?>)) {1838 MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance);1839 }1840 return newInstance;1841 }1842 } else {1843 mock = doCreateMock(type, constructorArgs, control, methods);1844 }1845 MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);1846 final Set<Method> methodsToMock = toSet(methods);1847 if (isStatic) {1848 MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h,1849 methodsToMock, mock));1850 MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);1851 } else {1852 MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h,1853 methodsToMock));1854 if (!(mock instanceof InvocationSubstitute<?>)) {1855 MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);1856 }1857 }1858 ClassLoader classLoader = mock.getClass().getClassLoader();1859 if (classLoader instanceof MockClassLoader) {1860 ((MockClassLoader) classLoader).cache(mock.getClass());1861 }1862 return mock;1863 }1864 private static <T> Class<?> createReplicaType(Class<T> type, boolean isStatic, ConstructorArgs constructorArgs) {1865 final ClassReplicaCreator classReplicaCreator = new ClassReplicaCreator();1866 final Class<?> replicaType;1867 if (isStatic || constructorArgs == null) {1868 replicaType = classReplicaCreator.createClassReplica(type);1869 } else {1870 try {1871 replicaType = classReplicaCreator.createInstanceReplica(constructorArgs.getConstructor().newInstance(1872 constructorArgs.getInitArgs()));1873 } catch (RuntimeException e) {1874 throw e;1875 } catch (InvocationTargetException e) {1876 Throwable targetException = e.getTargetException();1877 if (targetException instanceof RuntimeException) {1878 throw (RuntimeException) targetException;1879 }1880 throw new RuntimeException(e);1881 } catch (Exception e) {1882 throw new RuntimeException(e);1883 }1884 }1885 return replicaType;1886 }1887 private static <T> T doCreateMock(Class<T> type, ConstructorArgs constructorArgs, final IMocksControl control,1888 Method... methods) {1889 T mock;1890 MocksControl mocksControl = ((MocksControl) control);1891 if (constructorArgs == null) {1892 if (methods == null) {1893 mock = mocksControl.createMock(type);1894 } else {1895 mock = mocksControl.createMock(null, type, null, methods);1896 }1897 } else {1898 if (methods == null) {1899 mock = mocksControl.createMock(null, type, constructorArgs);1900 } else {1901 mock = mocksControl.createMock(null, type, constructorArgs, methods);1902 }1903 }1904 return mock;1905 }1906 private static Set<Method> toSet(Method[] methods) {1907 return methods == null ? null : new HashSet<Method>(Arrays.asList(methods));1908 }1909 private static Class<?>[] mergeArgumentTypes(Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {1910 if (firstArgumentType == null) {1911 return additionalArgumentTypes == null ? new Class<?>[0] : additionalArgumentTypes;1912 } else if (additionalArgumentTypes == null) {1913 additionalArgumentTypes = new Class<?>[0];1914 }1915 final Class<?>[] argumentTypes = new Class[additionalArgumentTypes.length + 1];1916 argumentTypes[0] = firstArgumentType;1917 if (additionalArgumentTypes.length != 0) {1918 System.arraycopy(additionalArgumentTypes, 0, argumentTypes, 1, additionalArgumentTypes.length);1919 }1920 return argumentTypes;1921 }1922 @SuppressWarnings("unchecked")1923 private static <T> IExpectationSetters<T> doExpectPrivate(Object instance, Method methodToExpect,1924 Object... arguments) throws Exception {1925 WhiteboxImpl.performMethodInvocation(instance, methodToExpect, arguments);1926 return (IExpectationSetters<T>) org.easymock.EasyMock.expectLastCall();1927 }1928 private static synchronized void replay(Class<?>... types) {1929 for (Class<?> type : types) {1930 final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type);1931 if (invocationHandler != null) {1932 invocationHandler.replay();1933 }1934 NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type);1935 if (newInvocationControl != null) {1936 newInvocationControl.replay();1937 }1938 }1939 }1940 /**1941 * Note: doesn't clear PowerMock state.1942 */1943 private static synchronized void verifyClass(Class<?>... types) {1944 for (Class<?> type : types) {1945 final EasyMockMethodInvocationControl invocationHandler = (EasyMockMethodInvocationControl) MockRepository.getStaticMethodInvocationControl(type);1946 if (invocationHandler != null) {1947 invocationHandler.verify();1948 }1949 EasyMockNewInvocationControl<?> newInvocationControl = (EasyMockNewInvocationControl<?>) MockRepository.getNewInstanceControl(type);1950 if (newInvocationControl != null) {1951 try {1952 newInvocationControl.verify();1953 } catch (AssertionError e) {1954 NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type);1955 }1956 }1957 }1958 }1959 private static boolean isNiceReplayAndVerifyMode() {1960 final Boolean mode = MockRepository.getAdditionalState(NICE_REPLAY_AND_VERIFY_KEY);1961 return mode != null && mode;1962 }1963 /**...

Full Screen

Full Screen

Source:EasyMockNewInvocationControl.java Github

copy

Full Screen

...22import org.powermock.core.spi.NewInvocationControl;23import org.powermock.core.spi.support.InvocationSubstitute;24import org.powermock.reflect.internal.WhiteboxImpl;25import java.lang.reflect.Constructor;26public class EasyMockNewInvocationControl<T> implements NewInvocationControl<IExpectationSetters<T>> {27 private final InvocationSubstitute<T> substitute;28 private final Class<T> subsitutionType;29 private boolean hasReplayed;30 private boolean hasVerified;31 public EasyMockNewInvocationControl(InvocationSubstitute<T> substitute, Class<T> type) {32 if (substitute == null) {33 throw new IllegalArgumentException("Internal error: substitute cannot be null.");34 }35 this.subsitutionType = type;36 this.substitute = substitute;37 }38 @Override39 public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception {40 Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig);41 if (constructor.isVarArgs()) {42 /*43 * Get the last argument because this contains the actual varargs44 * arguments.45 */...

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import org.easymock.internal.MocksControl;3import org.easymock.internal.invocationcontrol.MockInvocationControl;4import org.easymock.internal.invocationcontrol.MockInvocationControlFactory;5import org.easymock.internal.matchers.Equals;6public class EasyMockNewInvocationControl extends MocksControl implements MockInvocationControlFactory {7 public EasyMockNewInvocationControl() {8 super(new Equals());9 }10 public MockInvocationControl create() {11 return this;12 }13 public MockInvocationControl create(Object[] params) {14 return this;15 }16 public MockInvocationControl create(Object[] params, Class<?>[] types) {17 return this;18 }19 public MockInvocationControl create(Object mock, Object[] params) {20 return this;21 }22 public MockInvocationControl create(Object mock, Object[] params, Class<?>[] types) {23 return this;24 }25 public MockInvocationControl create(Object mock) {26 return this;27 }28}29package org.powermock.api.easymock.internal.invocationcontrol;30import org.easymock.internal.MocksControl;31import org.easymock.internal.invocationcontrol.MockInvocationControl;32import org.easymock.internal.invocationcontrol.MockInvocationControlFactory;33import org.easymock.internal.matchers.Equals;34public class EasyMockNewInvocationControl extends MocksControl implements MockInvocationControlFactory {35 public EasyMockNewInvocationControl() {36 super(new Equals());37 }38 public MockInvocationControl create() {39 return this;40 }41 public MockInvocationControl create(Object[] params) {42 return this;43 }44 public MockInvocationControl create(Object[] params, Class<?>[] types) {45 return this;46 }47 public MockInvocationControl create(Object mock, Object[] params) {48 return this;49 }50 public MockInvocationControl create(Object mock, Object[] params, Class<?>[] types) {51 return this;52 }53 public MockInvocationControl create(Object mock) {54 return this;55 }56}57package org.powermock.api.easymock.internal.invocationcontrol;58import org.easymock.internal.MocksControl;59import org.easymock.internal.invocationcontrol.MockInvocationControl;60import org.easymock.internal.invocationcontrol.MockInvocationControlFactory;61import org.eas

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControl;2import org.powermock.api.easymock.support.MockSupport;3import org.powermock.core.classloader.annotations.PrepareForTest;4import org.powermock.modules.junit4.PowerMockRunner;5import org.powermock.modules.junit4.PowerMockRunnerDelegate;6import org.powermock.reflect.Whitebox;7import org.powermock.reflect.exceptions.FieldNotFoundException;8import org.powermock.reflect.exceptions.MethodNotFoundException;9import org.powermock.reflect.exceptions.TooManyFieldsFoundException;10import org.powermock.reflect.exceptions.TooManyMethodsFoundException;11import org.powermock.reflect.internal.WhiteboxImpl;12import org.powermock.reflect.internal.WhiteboxImpl.FieldInfo;13import org.powermock.reflect.internal.WhiteboxImpl.MethodInfo;14import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocation;15import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationException;16import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResult;17import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory;18import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory.MethodInvocationResultFactoryException;19import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory.MethodInvocationResultFactoryException.Reason;20import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory.MethodInvocationResultType;21import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory.MethodInvocationResultWithException;22import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory.MethodInvocationResultWithResult;23import org.powermock.reflect.internal.WhiteboxImpl.MethodInvocationResultFactory.MethodInvocationResultWithVoid;24import org.powermock.reflect.internal.WhiteboxImpl.MethodInvoker;25import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerException;26import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerException.Reason;27import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerFactory;28import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerFactory.MethodInvokerFactoryException;29import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerFactory.MethodInvokerFactoryException.Reason;30import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerFactory.MethodInvokerType;31import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerWithException;32import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerWithExceptionAndResult;33import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerWithExceptionAndVoid;34import org.powermock.reflect.internal.WhiteboxImpl.MethodInvokerWithExceptionOnly;35import org.powermock.reflect.internal.WhiteboxImpl.Method

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1import org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControl;2import org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControlBuilder;3import org.powermock.api.easymock.internal.invocationcontrol.MockGateway;4import org.powermock.reflect.Whitebox;5import org.powermock.reflect.exceptions.FieldNotFoundException;6import org.powermock.reflect.exceptions.MethodNotFoundException;7import org.powermock.reflect.exceptions.TooManyMethodsFoundException;8import org.powermock.reflect.exceptions.TooManyFieldsFoundException;9import org.powermock.reflect.exceptions.MethodInvocationException;10import org.powermock.reflect.exceptions.FieldSetterException;11import org.powermock.reflect.exceptions.MethodNotFoundException;12import org.powermock.reflect.exceptions.MethodInvocationException;13import org.powermock.reflect.exceptions.TooManyMethodsFoundException;14import org.powermock.reflect.exceptions.FieldNotFoundException;15import org.powermock.reflect.exceptions.TooManyFieldsFoundException;16import org.powermock.reflect.exceptions.FieldSetterException;17import org.powermock.reflect.Whitebox;18import java.lang.reflect.Method;19import java.lang.reflect.Field;20import java.lang.reflect.InvocationTargetException;21import java.lang.reflect.Modifier;22import java.lang.reflect.Constructor;23import java.lang.reflect.InvocationTargetException;24import java.lang.reflec

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import org.easymock.IAnswer;3import org.easymock.internal.InvocationMatcher;4import org.easymock.internal.MocksControl;5import org.easymock.internal.matchers.Equals;6import org.easymock.internal.matchers.InstanceOf;7import org.easymock.internal.matchers.Null;8import org.easymock.internal.matchers.Same;9import org.powermock.api.easymock.internal.expectation.PowerMockExpectedInvocation;10import org.powermock.api.easymock.internal.expectation.PowerMockExpectedMethodCall;11import java.util.ArrayList;12import java.util.List;13public class EasyMockNewInvocationControl extends MocksControl {14 private final List<PowerMockExpectedInvocation> expectedInvocations = new ArrayList<PowerMockExpectedInvocation>();15 public void setMatcher(InvocationMatcher matcher) {16 super.setMatcher(matcher);17 }18 public void setReturnValue(Object value) {19 super.setReturnValue(value);20 }21 public void setReturnValue(Object value, IAnswer<?> answer) {22 super.setReturnValue(value, answer);23 }24 public void setReturnValue(Object value, int count) {25 super.setReturnValue(value, count);26 }27 public void setReturnValue(Object value, IAnswer<?> answer, int count) {28 super.setReturnValue(value, answer, count);29 }30 public void setDefaultReturnValue(Object value) {31 super.setDefaultReturnValue(value);32 }33 public void setDefaultReturnValue(Object value, IAnswer<?> answer) {34 super.setDefaultReturnValue(value, answer);35 }36 public void setThrowable(Throwable throwable) {37 super.setThrowable(throwable);38 }39 public void setThrowable(Throwable throwable, int count) {40 super.setThrowable(throwable, count);41 }42 public void setDefaultThrowable(Throwable throwable) {43 super.setDefaultThrowable(throwable);44 }45 public void setMatcher(int index, InvocationMatcher matcher) {46 super.setMatcher(index, matcher);47 }48 public void setReturnValue(int index, Object value) {49 super.setReturnValue(index, value);50 }51 public void setReturnValue(int index, Object value, IAnswer

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import org.easymock.internal.MockInvocationControl;3import org.powermock.core.spi.NewInvocationControl;4public class EasyMockNewInvocationControl implements NewInvocationControl {5 public static final EasyMockNewInvocationControl INSTANCE = new EasyMockNewInvocationControl();6 public Object newInstance(Class<?> type) {7 return new MockInvocationControl();8 }9}10package org.powermock.api.easymock.internal.invocationcontrol;11import org.easymock.internal.MockInvocationControl;12import org.powermock.core.spi.NewInvocationControl;13public class EasyMockNewInvocationControl implements NewInvocationControl {14 public static final EasyMockNewInvocationControl INSTANCE = new EasyMockNewInvocationControl();15 public Object newInstance(Class<?> type) {16 return new MockInvocationControl();17 }18}19package org.powermock.api.easymock.internal.invocationcontrol;20import org.easymock.internal.MockInvocationControl;21import org.powermock.core.spi.NewInvocationControl;22public class EasyMockNewInvocationControl implements NewInvocationControl {23 public static final EasyMockNewInvocationControl INSTANCE = new EasyMockNewInvocationControl();24 public Object newInstance(Class<?> type) {25 return new MockInvocationControl();26 }27}28package org.powermock.api.easymock.internal.invocationcontrol;29import org.easymock.internal.MockInvocationControl;30import org.powermock.core.spi.NewInvocationControl;31public class EasyMockNewInvocationControl implements NewInvocationControl {32 public static final EasyMockNewInvocationControl INSTANCE = new EasyMockNewInvocationControl();33 public Object newInstance(Class<?> type) {34 return new MockInvocationControl();35 }36}37package org.powermock.api.easymock.internal.invocationcontrol;38import org.easymock.internal.MockInvocationControl;39import org.powermock.core.spi.NewInvocationControl

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import org.easymock.internal.MocksControl;3import org.easymock.internal.InvocationMatcher;4import org.easymock.internal.MockInvocationHandler;5import org.easymock.internal.ObjectMethodsFilter;6import org.easymock.internal.matchers.Equals;7import org.easymock.internal.matchers.Any;8import org.easymock.internal.matchers.Null;9import org.easymock.internal.matchers.Not;10import org.easymock.internal.matchers.And;11import org.easymock.internal.matchers.Or;12import org.easymock.internal.matchers.InstanceOf;13import org.easymock.internal.matchers.StartsWith;14import org.easymock.internal.matchers.EndsWith;15import org.easymock.internal.matchers.Find;16import org.easymock.internal.matchers.GreaterThan;17import org.easymock.internal.matchers.GreaterOrEqual;18import org.easymock.internal.matchers.LessThan;19import org.easymock.internal.matchers.LessOrEqual;20import org.easymock.internal.matchers.Compare;21import org.easymock.internal.matchers.Same;22import org.easymock.internal.matchers.NotSame;23import org.easymock.internal.matchers.Regex;24import org.easymock.internal.matchers.ArrayEquals;25import org.easymock.internal.matchers.ArrayContains;26import org.easymock.internal.matchers.CmpEq;27import org.easymock.internal.matchers.CmpGe;28import org.easymock.internal.matchers.CmpGt;29import org.easymock.internal.matchers.CmpLe;30import org.easymock.internal.matchers.CmpLt;31import org.easymock.internal.matchers.CmpNe;32import org.easymock.internal.matchers.CmpIsNull;33import org.easymock.internal.matchers.CmpIsNotNull;34import org.easymock.internal.matchers.CmpOrder;35import org.easymock.internal.matchers.CmpSame;36import org.easymock.internal.matchers.CmpNotSame;37import org.easymock.internal.matchers.CmpFind;38import org.easymock.internal.matchers.CmpArrayEquals;39import org.easymock.internal.matchers.CmpArrayContains;40import org.easymock.internal.matchers.CmpNot;41import org.eas

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import org.easymock.internal.InvocationControl;3import org.easymock.internal.MocksControl;4import org.easymock.internal.invocationcontrol.MockInvocationControl;5import org.powermock.api.easymock.internal.invocationcontrol.EasyMockNewInvocationControl;6import org.powermock.api.easymock.internal.invocationcontrol.MockGatewayMethod;7import org.powermock.api.easymock.internal.invocationcontrol.MockGatewayMethodFactory;8import org.powermock.api.easymock.internal.invocationcontrol.ProxyFactory;9import org.powermock.api.easymock.internal.invocationcontrol.ProxyFactoryImpl;10import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethod;11import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodFactory;12import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodFactoryImpl;13import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandler;14import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerFactory;15import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerFactoryImpl;16import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImpl;17import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactory;18import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl;19import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl2;20import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl3;21import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl4;22import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl5;23import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl6;24import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl7;25import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl8;26import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl9;27import org.powermock.api.easymock.internal.invocationcontrol.ProxyMethodHandlerImplFactoryImpl10;28import org.powermock.api.easymock

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import java.lang.reflect.Method;3import java.util.Arrays;4import java.util.Collection;5import org.easymock.internal.MocksControl;6import org.powermock.core.spi.NewInvocationControl;7public class EasyMockNewInvocationControl implements NewInvocationControl {8 private final MocksControl control;9 public EasyMockNewInvocationControl(MocksControl control) {10 this.control = control;11 }12 public Object invoke(Class<?> typeToMock, Method method, Object[] args) throws Throwable {13 if (args == null) {14 args = new Object[0];15 }16 return control.createMock(typeToMock, method.getName(), method.getParameterTypes(), args);17 }18 public Collection<Class<?>> getSupportedTypes() {19 return Arrays.asList(new Class<?>[] { Object.class });20 }21}22package org.powermock.api.easymock.internal.invocationcontrol;23import java.lang.reflect.Method;24import java.util.Arrays;25import java.util.Collection;26import org.easymock.internal.MocksControl;27import org.powermock.core.spi.NewInvocationControl;28public class EasyMockNewInvocationControl implements NewInvocationControl {29 private final MocksControl control;30 public EasyMockNewInvocationControl(MocksControl control) {31 this.control = control;32 }33 public Object invoke(Class<?> typeToMock, Method method, Object[] args) throws Throwable {34 if (args == null) {35 args = new Object[0];36 }37 return control.createMock(typeToMock, method.getName(), method.getParameterTypes(), args);38 }39 public Collection<Class<?>> getSupportedTypes() {40 return Arrays.asList(new Class<?>[] { Object.class });41 }42}43package org.powermock.api.easymock.internal.invocationcontrol;44import java.lang.reflect.Method;45import java.util.Arrays;46import java.util.Collection;47import org.easymock.internal.MocksControl;48import org.powermock.core.spi.NewInvocationControl;49public class EasyMockNewInvocationControl implements NewInvocationControl {50 private final MocksControl control;51 public EasyMockNewInvocationControl(MocksControl control) {

Full Screen

Full Screen

EasyMockNewInvocationControl

Using AI Code Generation

copy

Full Screen

1package org.powermock.api.easymock.internal.invocationcontrol;2import org.easymock.internal.InvocationControl;3import org.easymock.internal.MocksBehavior;4import org.easymock.internal.ObjectMethodsFilter;5import org.easymock.internal.matchers.Equals;6import org.easymock.internal.matchers.EqualsBuilder;7import org.easymock.internal.matchers.Same;8import org.easymock.internal.matchers.ToString;9import org.easymock.internal.matchers.VarargMatcher;10import org.easymock.internal.matchers.VarargMatcherFactory;11import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyFramework;12import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyFrameworkFactory;13import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyFrameworkInterface;14import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyInvocationHandler;15import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyInterface;16import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyInterfaceFactory;17import org.powermock.api.easymock.internal.invocationcontrol.proxyframework.ProxyInterfaceFactoryImpl;18import org.powermock.core.spi.NewInvocationControl;19import org.powermock.core.spi.support.InvocationSubstitute;20import java.lang.reflect.Method;21import java.util.ArrayList;22import java.util.List;23public class EasyMockNewInvocationControl implements NewInvocationControl {24 private final MocksBehavior mocksBehavior;25 private final ProxyInterfaceFactory proxyInterfaceFactory;26 private final ProxyFrameworkFactory proxyFrameworkFactory;27 private final ProxyFrameworkInterface proxyFrameworkInterface;28 private final ObjectMethodsFilter objectMethodsFilter;29 private final VarargMatcherFactory varargMatcherFactory;30 private final List<InvocationSubstitute> invocationSubstitutes;31 public EasyMockNewInvocationControl(final InvocationSubstitute... invocationSubstitutes) {32 this.invocationSubstitutes = new ArrayList<InvocationSubstitute>();33 for (InvocationSubstitute invocationSubstitute : invocationSubstitutes) {34 this.invocationSubstitutes.add(invocationSubstitute);35 }36 this.mocksBehavior = new MocksBehavior();37 this.proxyInterfaceFactory = new ProxyInterfaceFactoryImpl();38 this.proxyFrameworkFactory = new ProxyFrameworkFactory();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Powermock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in EasyMockNewInvocationControl

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful