Best Powermock code snippet using org.powermock.api.easymock.PowerMock.doMock
Source:PowerMock.java
...66 * optionally what methods to mock67 * @return the mock object.68 */69 public static synchronized <T> T createMock(Class<T> type, Method... methods) {70 return doMock(type, false, new DefaultMockStrategy(), null, methods);71 }72 /**73 * Creates a mock object that supports mocking of final and native methods.74 *75 * @param <T>76 * the type of the mock object77 * @param type78 * the type of the mock object79 * @return the mock object.80 */81 public static synchronized <T> T createMock(Class<T> type) {82 return doMock(type, false, new DefaultMockStrategy(), null, (Method[]) null);83 }84 /**85 * Creates a mock object that supports mocking of final and native methods86 * and invokes a specific constructor.87 *88 * @param <T>89 * the type of the mock object90 * @param type91 * the type of the mock object92 * @param constructorArgs93 * The constructor arguments that will be used to invoke a94 * special constructor.95 * @param methods96 * optionally what methods to mock97 * @return the mock object.98 */99 public static <T> T createMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) {100 return doMock(type, false, new DefaultMockStrategy(), constructorArgs, methods);101 }102 /**103 * Creates a mock object that supports mocking of final and native methods104 * and invokes a specific constructor based on the supplied argument values.105 *106 * @param <T>107 * the type of the mock object108 * @param type109 * the type of the mock object110 * @param constructorArguments111 * The constructor arguments that will be used to invoke a112 * certain constructor.113 * @return the mock object.114 */115 public static <T> T createMock(Class<T> type, Object... constructorArguments) {116 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);117 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);118 return doMock(type, false, new DefaultMockStrategy(), constructorArgs, (Method[]) null);119 }120 /**121 * Creates a strict mock object that supports mocking of final and native122 * methods.123 *124 * @param <T>125 * the type of the mock object126 * @param type127 * the type of the mock object128 * @param methods129 * optionally what methods to mock130 * @return the mock object.131 */132 public static synchronized <T> T createStrictMock(Class<T> type, Method... methods) {133 return doMock(type, false, new StrictMockStrategy(), null, methods);134 }135 /**136 * Creates a strict mock object that supports mocking of final and native137 * methods.138 *139 * @param <T>140 * the type of the mock object141 * @param type142 * the type of the mock object143 * @return the mock object.144 */145 public static synchronized <T> T createStrictMock(Class<T> type) {146 return doMock(type, false, new StrictMockStrategy(), null, (Method[]) null);147 }148 /**149 * Creates a nice mock object that supports mocking of final and native150 * methods.151 *152 * @param <T>153 * the type of the mock object154 * @param type155 * the type of the mock object156 * @param methods157 * optionally what methods to mock158 * @return the mock object.159 */160 public static synchronized <T> T createNiceMock(Class<T> type, Method... methods) {161 return doMock(type, false, new NiceMockStrategy(), null, methods);162 }163 /**164 * Creates a nice mock object that supports mocking of final and native165 * methods.166 *167 * @param <T>168 * the type of the mock object169 * @param type170 * the type of the mock object171 * @return the mock object.172 */173 public static synchronized <T> T createNiceMock(Class<T> type) {174 return doMock(type, false, new NiceMockStrategy(), null, (Method[]) null);175 }176 /**177 * Creates a strict mock object that supports mocking of final and native178 * methods and invokes a specific constructor.179 *180 * @param <T>181 * the type of the mock object182 * @param type183 * the type of the mock object184 * @param constructorArgs185 * The constructor arguments that will be used to invoke a186 * special constructor.187 * @param methods188 * optionally what methods to mock189 * @return the mock object.190 */191 public static <T> T createStrictMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) {192 return doMock(type, false, new StrictMockStrategy(), constructorArgs, methods);193 }194 /**195 * Creates a nice mock object that supports mocking of final and native196 * methods and invokes a specific constructor.197 *198 * @param <T>199 * the type of the mock object200 * @param type201 * the type of the mock object202 * @param constructorArgs203 * The constructor arguments that will be used to invoke a204 * special constructor.205 * @param methods206 * optionally what methods to mock207 * @return the mock object.208 */209 public static <T> T createNiceMock(Class<T> type, ConstructorArgs constructorArgs, Method... methods) {210 return doMock(type, false, new NiceMockStrategy(), constructorArgs, methods);211 }212 /**213 * Creates a strict mock object that supports mocking of final and native214 * methods and invokes a specific constructor based on the supplied argument215 * values.216 *217 * @param <T>218 * the type of the mock object219 * @param type220 * the type of the mock object221 * @param constructorArguments222 * The constructor arguments that will be used to invoke a223 * certain constructor.224 * @return the mock object.225 */226 public static <T> T createStrictMock(Class<T> type, Object... constructorArguments) {227 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);228 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);229 return doMock(type, false, new StrictMockStrategy(), constructorArgs, (Method[]) null);230 }231 /**232 * Creates a nice mock object that supports mocking of final and native233 * methods and invokes a specific constructor based on the supplied argument234 * values.235 *236 * @param <T>237 * the type of the mock object238 * @param type239 * the type of the mock object240 * @param constructorArguments241 * The constructor arguments that will be used to invoke a242 * certain constructor.243 * @return the mock object.244 */245 public static <T> T createNiceMock(Class<T> type, Object... constructorArguments) {246 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);247 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);248 return doMock(type, false, new NiceMockStrategy(), constructorArgs, (Method[]) null);249 }250 /**251 * Enable static mocking for a class.252 *253 * @param type254 * the class to enable static mocking255 * @param methods256 * optionally what methods to mock257 */258 public static synchronized void mockStatic(Class<?> type, Method... methods) {259 doMock(type, true, new DefaultMockStrategy(), null, methods);260 }261 /**262 * Enable static mocking for a class.263 *264 * @param type265 * the class to enable static mocking266 */267 public static synchronized void mockStatic(Class<?> type) {268 doMock(type, true, new DefaultMockStrategy(), null, (Method[]) null);269 }270 /**271 * Enable strict static mocking for a class.272 *273 * @param type274 * the class to enable static mocking275 * @param methods276 * optionally what methods to mock277 */278 public static synchronized void mockStaticStrict(Class<?> type, Method... methods) {279 doMock(type, true, new StrictMockStrategy(), null, methods);280 }281 /**282 * Enable strict static mocking for a class.283 *284 * @param type285 * the class to enable static mocking286 */287 public static synchronized void mockStaticStrict(Class<?> type) {288 doMock(type, true, new StrictMockStrategy(), null, (Method[]) null);289 }290 /**291 * Enable nice static mocking for a class.292 *293 * @param type294 * the class to enable static mocking295 * @param methods296 * optionally what methods to mock297 */298 public static synchronized void mockStaticNice(Class<?> type, Method... methods) {299 doMock(type, true, new NiceMockStrategy(), null, methods);300 }301 /**302 * Enable nice static mocking for a class.303 *304 * @param type305 * the class to enable static mocking306 */307 public static synchronized void mockStaticNice(Class<?> type) {308 doMock(type, true, new NiceMockStrategy(), null, (Method[]) null);309 }310 /**311 * A utility method that may be used to specify several methods that should312 * <i>not</i> be mocked in an easy manner (by just passing in the method313 * names of the method you wish <i>not</i> to mock). Note that you cannot314 * uniquely specify a method to exclude using this method if there are315 * several methods with the same name in <code>type</code>. This method will316 * mock ALL methods that doesn't match the supplied name(s) regardless of317 * parameter types and signature. If this is not the case you should318 * fall-back on using the {@link #createMock(Class, Method...)} method319 * instead.320 *321 * @param <T>322 * The type of the mock.323 * @param type324 * The type that'll be used to create a mock instance.325 * @param methodNames326 * The names of the methods that should be mocked. If327 * <code>null</code>, then this method will have the same effect328 * as just calling {@link #createMock(Class, Method...)} with the329 * second parameter as <code>new Method[0]</code> (i.e. all330 * methods in that class will be mocked).331 * @return A mock object of type <T>.332 */333 public static synchronized <T> T createPartialMockForAllMethodsExcept(Class<T> type, String... methodNames) {334 if (methodNames != null && methodNames.length == 0) {335 return createMock(type);336 }337 return createMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));338 }339 /**340 * A utility method that may be used to specify several methods that should341 * <i>not</i> be nicely mocked in an easy manner (by just passing in the342 * method names of the method you wish <i>not</i> to mock). Note that you343 * cannot uniquely specify a method to exclude using this method if there344 * are several methods with the same name in <code>type</code>. This method345 * will mock ALL methods that doesn't match the supplied name(s) regardless346 * of parameter types and signature. If this is not the case you should347 * fall-back on using the {@link #createMock(Class, Method...)} method348 * instead.349 *350 * @param <T>351 * The type of the mock.352 * @param type353 * The type that'll be used to create a mock instance.354 * @param methodNames355 * The names of the methods that should be mocked. If356 * <code>null</code>, then this method will have the same effect357 * as just calling {@link #createMock(Class, Method...)} with the358 * second parameter as <code>new Method[0]</code> (i.e. all359 * methods in that class will be mocked).360 * @return A mock object of type <T>.361 */362 public static synchronized <T> T createNicePartialMockForAllMethodsExcept(Class<T> type, String... methodNames) {363 if (methodNames != null && methodNames.length == 0) {364 return createNiceMock(type);365 }366 return createNiceMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));367 }368 /**369 * A utility method that may be used to specify several methods that should370 * <i>not</i> be strictly mocked in an easy manner (by just passing in the371 * method names of the method you wish <i>not</i> to mock). Note that you372 * cannot uniquely specify a method to exclude using this method if there373 * are several methods with the same name in <code>type</code>. This method374 * will mock ALL methods that doesn't match the supplied name(s) regardless375 * of parameter types and signature. If this is not the case you should376 * fall-back on using the {@link #createMock(Class, Method...)} method377 * instead.378 *379 * @param <T>380 * The type of the mock.381 * @param type382 * The type that'll be used to create a mock instance.383 * @param methodNames384 * The names of the methods that should be mocked. If385 * <code>null</code>, then this method will have the same effect386 * as just calling {@link #createMock(Class, Method...)} with the387 * second parameter as <code>new Method[0]</code> (i.e. all388 * methods in that class will be mocked).389 * @return A mock object of type <T>.390 */391 public static synchronized <T> T createStrictPartialMockForAllMethodsExcept(Class<T> type, String... methodNames) {392 if (methodNames != null && methodNames.length == 0) {393 return createStrictMock(type);394 }395 return createStrictMock(type, WhiteboxImpl.getAllMethodExcept(type, methodNames));396 }397 /**398 * Mock all methods of a class except for a specific one. Use this method399 * only if you have several overloaded methods.400 *401 * @param <T>402 * The type of the mock.403 * @param type404 * The type that'll be used to create a mock instance.405 * @param methodNameToExclude406 * The name of the method not to mock.407 * @param firstArgumentType408 * The type of the first parameter of the method not to mock409 * @param moreTypes410 * Optionally more parameter types that defines the method. Note411 * that this is only needed to separate overloaded methods.412 * @return A mock object of type <T>.413 */414 public static synchronized <T> T createPartialMockForAllMethodsExcept(Class<T> type, String methodNameToExclude,415 Class<?> firstArgumentType, Class<?>... moreTypes) {416 /*417 * The reason why we've split the first and "additional types" is418 * because it should not intervene with the mockAllExcept(type,419 * String...methodNames) method.420 */421 final Class<?>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);422 return createMock(type, WhiteboxImpl.getAllMetodsExcept(type, methodNameToExclude, argumentTypes));423 }424 /**425 * Mock all methods of a class except for a specific one nicely. Use this426 * method only if you have several overloaded methods.427 *428 * @param <T>429 * The type of the mock.430 * @param type431 * The type that'll be used to create a mock instance.432 * @param methodNameToExclude433 * The name of the method not to mock.434 * @param firstArgumentType435 * The type of the first parameter of the method not to mock436 * @param moreTypes437 * Optionally more parameter types that defines the method. Note438 * that this is only needed to separate overloaded methods.439 * @return A mock object of type <T>.440 */441 public static synchronized <T> T createNicePartialMockForAllMethodsExcept(Class<T> type,442 String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) {443 /*444 * The reason why we've split the first and "additional types" is445 * because it should not intervene with the mockAllExcept(type,446 * String...methodNames) method.447 */448 final Class<?>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);449 return createNiceMock(type, WhiteboxImpl.getAllMetodsExcept(type, methodNameToExclude, argumentTypes));450 }451 /**452 * Mock all methods of a class except for a specific one strictly. Use this453 * method only if you have several overloaded methods.454 *455 * @param <T>456 * The type of the mock.457 * @param type458 * The type that'll be used to create a mock instance.459 * @param methodNameToExclude460 * The name of the method not to mock.461 * @param firstArgumentType462 * The type of the first parameter of the method not to mock463 * @param moreTypes464 * Optionally more parameter types that defines the method. Note465 * that this is only needed to separate overloaded methods.466 * @return A mock object of type <T>.467 */468 public static synchronized <T> T createStrictPartialMockForAllMethodsExcept(Class<T> type,469 String methodNameToExclude, Class<?> firstArgumentType, Class<?>... moreTypes) {470 /*471 * The reason why we've split the first and "additional types" is472 * because it should not intervene with the mockAllExcept(type,473 * String...methodNames) method.474 */475 final Class<?>[] argumentTypes = mergeArgumentTypes(firstArgumentType, moreTypes);476 return createStrictMock(type, WhiteboxImpl.getAllMetodsExcept(type, methodNameToExclude, argumentTypes));477 }478 /**479 * Mock a single specific method. Use this to handle overloaded methods.480 *481 * @param <T>482 * The type of the mock.483 * @param type484 * The type that'll be used to create a mock instance.485 * @param methodNameToMock486 * The name of the method to mock487 * @param firstArgumentType488 * The type of the first parameter of the method to mock489 * @param additionalArgumentTypes490 * Optionally more parameter types that defines the method. Note491 * that this is only needed to separate overloaded methods.492 * @return A mock object of type <T>.493 */494 public static synchronized <T> T createPartialMock(Class<T> type, String methodNameToMock,495 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {496 return doMockSpecific(type, new DefaultMockStrategy(), new String[] { methodNameToMock }, null,497 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));498 }499 /**500 * Strictly mock a single specific method. Use this to handle overloaded501 * methods.502 *503 * @param <T>504 * The type of the mock.505 * @param type506 * The type that'll be used to create a mock instance.507 * @param methodNameToMock508 * The name of the method to mock509 * @param firstArgumentType510 * The type of the first parameter of the method to mock511 * @param additionalArgumentTypes512 * Optionally more parameter types that defines the method. Note513 * that this is only needed to separate overloaded methods.514 * @return A mock object of type <T>.515 */516 public static synchronized <T> T createStrictPartialMock(Class<T> type, String methodNameToMock,517 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {518 return doMockSpecific(type, new StrictMockStrategy(), new String[] { methodNameToMock }, null,519 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));520 }521 /**522 * Nicely mock a single specific method. Use this to handle overloaded523 * methods.524 *525 * @param <T>526 * The type of the mock.527 * @param type528 * The type that'll be used to create a mock instance.529 * @param methodNameToMock530 * The name of the method to mock531 * @param firstArgumentType532 * The type of the first parameter of the method to mock533 * @param additionalArgumentTypes534 * Optionally more parameter types that defines the method. Note535 * that this is only needed to separate overloaded methods.536 * @return A mock object of type <T>.537 */538 public static synchronized <T> T createNicePartialMock(Class<T> type, String methodNameToMock,539 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {540 return doMockSpecific(type, new NiceMockStrategy(), new String[] { methodNameToMock }, null,541 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));542 }543 /**544 * Mock a single static method.545 *546 * @param clazz547 * The class where the method is specified in.548 * @param methodNameToMock549 * The first argument550 * @param firstArgumentType551 * The first argument type.552 * @param additionalArgumentTypes553 * Optional additional argument types.554 */555 public static synchronized void mockStaticPartial(Class<?> clazz, String methodNameToMock,556 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {557 doMockSpecific(clazz, new DefaultMockStrategy(), new String[] { methodNameToMock }, null,558 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));559 }560 /**561 * Mock a single static method (strict).562 *563 * @param clazz564 * The class where the method is specified in.565 * @param methodNameToMock566 * The first argument567 * @param firstArgumentType568 * The first argument type.569 * @param additionalArgumentTypes570 * Optional additional argument types.571 */572 public static synchronized void mockStaticPartialStrict(Class<?> clazz, String methodNameToMock,573 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {574 doMockSpecific(clazz, new StrictMockStrategy(), new String[] { methodNameToMock }, null,575 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));576 }577 /**578 * Mock a single static method (nice).579 *580 * @param clazz581 * The class where the method is specified in.582 * @param methodNameToMock583 * The first argument584 * @param firstArgumentType585 * The first argument type.586 * @param additionalArgumentTypes587 * Optional additional argument types.588 */589 public static synchronized void mockStaticPartialNice(Class<?> clazz, String methodNameToMock,590 Class<?> firstArgumentType, Class<?>... additionalArgumentTypes) {591 doMockSpecific(clazz, new NiceMockStrategy(), new String[] { methodNameToMock }, null,592 mergeArgumentTypes(firstArgumentType, additionalArgumentTypes));593 }594 /**595 * A utility method that may be used to mock several <b>static</b> methods596 * in an easy way (by just passing in the method names of the method you597 * wish to mock). Note that you cannot uniquely specify a method to mock598 * using this method if there are several methods with the same name in599 * <code>type</code>. This method will mock ALL methods that match the600 * supplied name regardless of parameter types and signature. If this is the601 * case you should fall-back on using the602 * {@link #mockStatic(Class, Method...)} method instead.603 *604 * @param clazz605 * The class that contains the static methods that should be606 * mocked.607 * @param methodNames608 * The names of the methods that should be mocked. If609 * <code>null</code>, then this method will have the same effect610 * as just calling {@link #mockStatic(Class, Method...)} with the611 * second parameter as <code>new Method[0]</code> (i.e. all612 * methods in that class will be mocked).613 */614 public static synchronized void mockStaticPartial(Class<?> clazz, String... methodNames) {615 mockStatic(clazz, Whitebox.getMethods(clazz, methodNames));616 }617 /**618 * A utility method that may be used to mock several <b>static</b> methods619 * (strict) in an easy way (by just passing in the method names of the620 * method you wish to mock). Note that you cannot uniquely specify a method621 * to mock using this method if there are several methods with the same name622 * in <code>type</code>. This method will mock ALL methods that match the623 * supplied name regardless of parameter types and signature. If this is the624 * case you should fall-back on using the625 * {@link #mockStaticStrict(Class, Method...)} method instead.626 *627 * @param clazz628 * The class that contains the static methods that should be629 * mocked.630 * @param methodNames631 * The names of the methods that should be mocked. If632 * <code>null</code>, then this method will have the same effect633 * as just calling {@link #mockStatic(Class, Method...)} with the634 * second parameter as <code>new Method[0]</code> (i.e. all635 * methods in that class will be mocked).636 */637 public static synchronized void mockStaticPartialStrict(Class<?> clazz, String... methodNames) {638 mockStaticStrict(clazz, Whitebox.getMethods(clazz, methodNames));639 }640 /**641 * A utility method that may be used to mock several <b>static</b> methods642 * (nice) in an easy way (by just passing in the method names of the method643 * you wish to mock). Note that you cannot uniquely specify a method to mock644 * using this method if there are several methods with the same name in645 * <code>type</code>. This method will mock ALL methods that match the646 * supplied name regardless of parameter types and signature. If this is the647 * case you should fall-back on using the648 * {@link #mockStaticStrict(Class, Method...)} method instead.649 *650 * @param clazz651 * The class that contains the static methods that should be652 * mocked.653 * @param methodNames654 * The names of the methods that should be mocked. If655 * <code>null</code>, then this method will have the same effect656 * as just calling {@link #mockStatic(Class, Method...)} with the657 * second parameter as <code>new Method[0]</code> (i.e. all658 * methods in that class will be mocked).659 */660 public static synchronized void mockStaticPartialNice(Class<?> clazz, String... methodNames) {661 mockStaticNice(clazz, Whitebox.getMethods(clazz, methodNames));662 }663 static <T> T doMockSpecific(Class<T> type, MockStrategy mockStrategy, String[] methodNamesToMock,664 ConstructorArgs constructorArgs, Class<?>... argumentTypes) {665 List<Method> methods = new LinkedList<Method>();666 for (String methodName : methodNamesToMock) {667 methods.add(WhiteboxImpl.findMethodOrThrowException(type, methodName, argumentTypes));668 }669 final Method[] methodArray = methods.toArray(new Method[0]);670 if (WhiteboxImpl.areAllMethodsStatic(methodArray)) {671 if (mockStrategy instanceof DefaultMockStrategy) {672 mockStatic(type, methodArray);673 } else if (mockStrategy instanceof StrictMockStrategy) {674 mockStaticStrict(type, methodArray);675 } else {676 mockStaticNice(type, methodArray);677 }678 return null;679 }680 T mock = null;681 if (mockStrategy instanceof DefaultMockStrategy) {682 mock = createMock(type, constructorArgs, methodArray);683 } else if (mockStrategy instanceof StrictMockStrategy) {684 mock = createStrictMock(type, constructorArgs, methodArray);685 } else {686 mock = createNiceMock(type, constructorArgs, methodArray);687 }688 return mock;689 }690 /**691 * A utility method that may be used to mock several methods in an easy way692 * (by just passing in the method names of the method you wish to mock).693 * Note that you cannot uniquely specify a method to mock using this method694 * if there are several methods with the same name in <code>type</code>.695 * This method will mock ALL methods that match the supplied name regardless696 * of parameter types and signature. If this is the case you should697 * fall-back on using the {@link #createMock(Class, Method...)} method698 * instead.699 *700 * @param <T>701 * The type of the mock.702 * @param type703 * The type that'll be used to create a mock instance.704 * @param methodNames705 * The names of the methods that should be mocked. If706 * <code>null</code>, then this method will have the same effect707 * as just calling {@link #createMock(Class, Method...)} with the708 * second parameter as <code>new Method[0]</code> (i.e. all709 * methods in that class will be mocked).710 * @return A mock object of type <T>.711 */712 public static synchronized <T> T createPartialMock(Class<T> type, String... methodNames) {713 return createMock(type, Whitebox.getMethods(type, methodNames));714 }715 /**716 * A utility method that may be used to mock several methods in an easy way717 * (by just passing in the method names of the method you wish to mock).718 * Note that you cannot uniquely specify a method to mock using this method719 * if there are several methods with the same name in <code>type</code>.720 * This method will mock ALL methods that match the supplied name regardless721 * of parameter types and signature. If this is the case you should722 * fall-back on using the {@link #createMock(Class, Method...)} method723 * instead.724 * <p>725 * With this method you can specify where the class hierarchy the methods726 * are located. This is useful in, for example, situations where class A727 * extends B and both have a method called "mockMe" (A overrides B's mockMe728 * method) and you like to specify the only the "mockMe" method in B should729 * be mocked. "mockMe" in A should be left intact. In this case you should730 * do:731 *732 * <pre>733 * A tested = createPartialMock(A.class, B.class, "mockMe");734 * </pre>735 *736 *737 * @param <T>738 * The type of the mock.739 * @param type740 * The type that'll be used to create a mock instance.741 * @param where742 * Where in the class hierarchy the methods resides.743 * @param methodNames744 * The names of the methods that should be mocked. If745 * <code>null</code>, then this method will have the same effect746 * as just calling {@link #createMock(Class, Method...)} with the747 * second parameter as <code>new Method[0]</code> (i.e. all748 * methods in that class will be mocked).749 * @return A mock object of type <T>.750 */751 public static synchronized <T> T createPartialMock(Class<T> type, Class<? super T> where, String... methodNames) {752 return createMock(type, Whitebox.getMethods(where, methodNames));753 }754 /**755 * A utility method that may be used to strictly mock several methods in an756 * easy way (by just passing in the method names of the method you wish to757 * mock). Note that you cannot uniquely specify a method to mock using this758 * method if there are several methods with the same name in759 * <code>type</code>. This method will mock ALL methods that match the760 * supplied name regardless of parameter types and signature. If this is the761 * case you should fall-back on using the762 * {@link #createMock(Class, Method...)} method instead.763 *764 * @param <T>765 * The type of the mock.766 * @param type767 * The type that'll be used to create a mock instance.768 * @param methodNames769 * The names of the methods that should be mocked. If770 * <code>null</code>, then this method will have the same effect771 * as just calling {@link #createMock(Class, Method...)} with the772 * second parameter as <code>new Method[0]</code> (i.e. all773 * methods in that class will be mocked).774 * @return A mock object of type <T>.775 */776 public static synchronized <T> T createStrictPartialMock(Class<T> type, String... methodNames) {777 return createStrictMock(type, Whitebox.getMethods(type, methodNames));778 }779 /**780 * A utility method that may be used to strictly mock several methods in an781 * easy way (by just passing in the method names of the method you wish to782 * mock). Note that you cannot uniquely specify a method to mock using this783 * method if there are several methods with the same name in784 * <code>type</code>. This method will mock ALL methods that match the785 * supplied name regardless of parameter types and signature. If this is the786 * case you should fall-back on using the787 * {@link #createMock(Class, Method...)} method instead.788 * <p>789 * With this method you can specify where the class hierarchy the methods790 * are located. This is useful in, for example, situations where class A791 * extends B and both have a method called "mockMe" (A overrides B's mockMe792 * method) and you like to specify the only the "mockMe" method in B should793 * be mocked. "mockMe" in A should be left intact. In this case you should794 * do:795 *796 * <pre>797 * A tested = createPartialMockStrict(A.class, B.class, "mockMe");798 * </pre>799 *800 * @param <T>801 * The type of the mock.802 * @param type803 * The type that'll be used to create a mock instance.804 * @param where805 * Where in the class hierarchy the methods resides.806 * @param methodNames807 * The names of the methods that should be mocked. If808 * <code>null</code>, then this method will have the same effect809 * as just calling {@link #createMock(Class, Method...)} with the810 * second parameter as <code>new Method[0]</code> (i.e. all811 * methods in that class will be mocked).812 * @return A mock object of type <T>.813 */814 public static synchronized <T> T createStrictPartialMock(Class<T> type, Class<? super T> where,815 String... methodNames) {816 return createStrictMock(type, Whitebox.getMethods(where, methodNames));817 }818 /**819 * A utility method that may be used to nicely mock several methods in an820 * easy way (by just passing in the method names of the method you wish to821 * mock). Note that you cannot uniquely specify a method to mock using this822 * method if there are several methods with the same name in823 * <code>type</code>. This method will mock ALL methods that match the824 * supplied name regardless of parameter types and signature. If this is the825 * case you should fall-back on using the826 * {@link #createMock(Class, Method...)} method instead.827 *828 * @param <T>829 * The type of the mock.830 * @param type831 * The type that'll be used to create a mock instance.832 * @param methodNames833 * The names of the methods that should be mocked. If834 * <code>null</code>, then this method will have the same effect835 * as just calling {@link #createMock(Class, Method...)} with the836 * second parameter as <code>new Method[0]</code> (i.e. all837 * methods in that class will be mocked).838 * @return A mock object of type <T>.839 */840 public static synchronized <T> T createNicePartialMock(Class<T> type, String... methodNames) {841 return createNiceMock(type, Whitebox.getMethods(type, methodNames));842 }843 /**844 * A utility method that may be used to nicely mock several methods in an845 * easy way (by just passing in the method names of the method you wish to846 * mock). Note that you cannot uniquely specify a method to mock using this847 * method if there are several methods with the same name in848 * <code>type</code>. This method will mock ALL methods that match the849 * supplied name regardless of parameter types and signature. If this is the850 * case you should fall-back on using the851 * {@link #createMock(Class, Method...)} method instead.852 * <p>853 * With this method you can specify where the class hierarchy the methods854 * are located. This is useful in, for example, situations where class A855 * extends B and both have a method called "mockMe" (A overrides B's mockMe856 * method) and you like to specify the only the "mockMe" method in B should857 * be mocked. "mockMe" in A should be left intact. In this case you should858 * do:859 *860 * <pre>861 * A tested = createPartialMockNice(A.class, B.class, "mockMe");862 * </pre>863 *864 * @param <T>865 * The type of the mock.866 * @param type867 * The type that'll be used to create a mock instance.868 * @param where869 * Where in the class hierarchy the methods resides.870 * @param methodNames871 * The names of the methods that should be mocked. If872 * <code>null</code>, then this method will have the same effect873 * as just calling {@link #createMock(Class, Method...)} with the874 * second parameter as <code>new Method[0]</code> (i.e. all875 * methods in that class will be mocked).876 * @return A mock object of type <T>.877 */878 public static synchronized <T> T createNicePartialMock(Class<T> type, Class<? super T> where, String... methodNames) {879 return createNiceMock(type, Whitebox.getMethods(where, methodNames));880 }881 /**882 * A utility method that may be used to mock several methods in an easy way883 * (by just passing in the method names of the method you wish to mock). The884 * mock object created will support mocking of final methods and invokes the885 * default constructor (even if it's private).886 *887 * @param <T>888 * the type of the mock object889 * @param type890 * the type of the mock object891 * @param methodNames892 * The names of the methods that should be mocked. If893 * <code>null</code>, then this method will have the same effect894 * as just calling {@link #createMock(Class, Method...)} with the895 * second parameter as <code>new Method[0]</code> (i.e. all896 * methods in that class will be mocked).897 * @return the mock object.898 */899 public static <T> T createPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames)900 throws Exception {901 return createMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),902 Whitebox.getMethods(type, methodNames));903 }904 /**905 * A utility method that may be used to nicely mock several methods in an906 * easy way (by just passing in the method names of the method you wish to907 * mock). The mock object created will support mocking of final methods and908 * invokes the default constructor (even if it's private).909 *910 * @param <T>911 * the type of the mock object912 * @param type913 * the type of the mock object914 * @param methodNames915 * The names of the methods that should be mocked. If916 * <code>null</code>, then this method will have the same effect917 * as just calling {@link #createMock(Class, Method...)} with the918 * second parameter as <code>new Method[0]</code> (i.e. all919 * methods in that class will be mocked).920 * @return the mock object.921 */922 public static <T> T createNicePartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames)923 throws Exception {924 return createNiceMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),925 Whitebox.getMethods(type, methodNames));926 }927 /**928 * A utility method that may be used to strictly mock several methods in an929 * easy way (by just passing in the method names of the method you wish to930 * mock). The mock object created will support mocking of final methods and931 * invokes the default constructor (even if it's private).932 *933 * @param <T>934 * the type of the mock object935 * @param type936 * the type of the mock object937 * @param methodNames938 * The names of the methods that should be mocked. If939 * <code>null</code>, then this method will have the same effect940 * as just calling {@link #createMock(Class, Method...)} with the941 * second parameter as <code>new Method[0]</code> (i.e. all942 * methods in that class will be mocked).943 * @return the mock object.944 */945 public static <T> T createStrictPartialMockAndInvokeDefaultConstructor(Class<T> type, String... methodNames)946 throws Exception {947 return createStrictMock(type, new ConstructorArgs(Whitebox.getConstructor(type)),948 Whitebox.getMethods(type, methodNames));949 }950 /**951 * A utility method that may be used to mock several methods in an easy way952 * (by just passing in the method names of the method you wish to mock). The953 * mock object created will support mocking of final and native methods and954 * invokes a specific constructor based on the supplied argument values.955 *956 * @param <T>957 * the type of the mock object958 * @param type959 * the type of the mock object960 * @param methodNames961 * The names of the methods that should be mocked. If962 * <code>null</code>, then this method will have the same effect963 * as just calling {@link #createMock(Class, Method...)} with the964 * second parameter as <code>new Method[0]</code> (i.e. all965 * methods in that class will be mocked).966 * @param constructorArguments967 * The constructor arguments that will be used to invoke a968 * certain constructor. (optional)969 * @return the mock object.970 */971 public static <T> T createPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) {972 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);973 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);974 return doMock(type, false, new DefaultMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames));975 }976 /**977 * * A utility method that may be used to strictly mock several methods in978 * an easy way (by just passing in the method names of the method you wish979 * to mock). The mock object created will support mocking of final and980 * native methods and invokes a specific constructor based on the supplied981 * argument values.982 *983 * @param <T>984 * the type of the mock object985 * @param type986 * the type of the mock object987 * @param methodNames988 * The names of the methods that should be mocked. If989 * <code>null</code>, then this method will have the same effect990 * as just calling {@link #createMock(Class, Method...)} with the991 * second parameter as <code>new Method[0]</code> (i.e. all992 * methods in that class will be mocked).993 * @param constructorArguments994 * The constructor arguments that will be used to invoke a995 * certain constructor. (optional)996 * @return the mock object.997 */998 public static <T> T createStrictPartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) {999 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);1000 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);1001 return doMock(type, false, new StrictMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames));1002 }1003 /**1004 * * A utility method that may be used to nicely mock several methods in an1005 * easy way (by just passing in the method names of the method you wish to1006 * mock). The mock object created will support mocking of final and native1007 * methods and invokes a specific constructor based on the supplied argument1008 * values.1009 *1010 * @param <T>1011 * the type of the mock object1012 * @param type1013 * the type of the mock object1014 * @param methodNames1015 * The names of the methods that should be mocked. If1016 * <code>null</code>, then this method will have the same effect1017 * as just calling {@link #createMock(Class, Method...)} with the1018 * second parameter as <code>new Method[0]</code> (i.e. all1019 * methods in that class will be mocked).1020 * @param constructorArguments1021 * The constructor arguments that will be used to invoke a1022 * certain constructor. (optional)1023 * @return the mock object.1024 */1025 public static <T> T createNicePartialMock(Class<T> type, String[] methodNames, Object... constructorArguments) {1026 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);1027 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);1028 return doMock(type, false, new NiceMockStrategy(), constructorArgs, Whitebox.getMethods(type, methodNames));1029 }1030 /**1031 * A utility method that may be used to mock several methods in an easy way1032 * (by just passing in the method names of the method you wish to mock). Use1033 * this to handle overloaded methods. The mock object created will support1034 * mocking of final and native methods and invokes a specific constructor1035 * based on the supplied argument values.1036 *1037 * @param <T>1038 * the type of the mock object1039 * @param type1040 * the type of the mock object1041 * @param methodName1042 * The names of the methods that should be mocked. If1043 * <code>null</code>, then this method will have the same effect1044 * as just calling {@link #createMock(Class, Method...)} with the1045 * second parameter as <code>new Method[0]</code> (i.e. all1046 * methods in that class will be mocked).1047 * @param methodParameterTypes1048 * Parameter types that defines the method. Note that this is1049 * only needed to separate overloaded methods.1050 * @param constructorArguments1051 * The constructor arguments that will be used to invoke a1052 * certain constructor. (optional)1053 * @return the mock object.1054 */1055 public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1056 Object... constructorArguments) {1057 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);1058 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);1059 return doMockSpecific(type, new DefaultMockStrategy(), new String[] { methodName }, constructorArgs,1060 methodParameterTypes);1061 }1062 /**1063 * A utility method that may be used to strictly mock several methods in an1064 * easy way (by just passing in the method names of the method you wish to1065 * mock). Use this to handle overloaded methods. The mock object created1066 * will support mocking of final and native methods and invokes a specific1067 * constructor based on the supplied argument values.1068 *1069 * @param <T>1070 * the type of the mock object1071 * @param type1072 * the type of the mock object1073 * @param methodName1074 * The names of the methods that should be mocked. If1075 * <code>null</code>, then this method will have the same effect1076 * as just calling {@link #createMock(Class, Method...)} with the1077 * second parameter as <code>new Method[0]</code> (i.e. all1078 * methods in that class will be mocked).1079 * @param methodParameterTypes1080 * Parameter types that defines the method. Note that this is1081 * only needed to separate overloaded methods.1082 * @param constructorArguments1083 * The constructor arguments that will be used to invoke a1084 * certain constructor. (optional)1085 * @return the mock object.1086 */1087 public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1088 Object... constructorArguments) {1089 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);1090 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);1091 return doMockSpecific(type, new StrictMockStrategy(), new String[] { methodName }, constructorArgs,1092 methodParameterTypes);1093 }1094 /**1095 * A utility method that may be used to nicely mock several methods in an1096 * easy way (by just passing in the method names of the method you wish to1097 * mock). Use this to handle overloaded methods. The mock object created1098 * will support mocking of final and native methods and invokes a specific1099 * constructor based on the supplied argument values.1100 *1101 * @param <T>1102 * the type of the mock object1103 * @param type1104 * the type of the mock object1105 * @param methodName1106 * The names of the methods that should be mocked. If1107 * <code>null</code>, then this method will have the same effect1108 * as just calling {@link #createMock(Class, Method...)} with the1109 * second parameter as <code>new Method[0]</code> (i.e. all1110 * methods in that class will be mocked).1111 * @param methodParameterTypes1112 * Parameter types that defines the method. Note that this is1113 * only needed to separate overloaded methods.1114 * @param constructorArguments1115 * The constructor arguments that will be used to invoke a1116 * certain constructor. (optional)1117 * @return the mock object.1118 */1119 public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1120 Object... constructorArguments) {1121 Constructor<?> constructor = WhiteboxImpl.findUniqueConstructorOrThrowException(type, constructorArguments);1122 ConstructorArgs constructorArgs = new ConstructorArgs(constructor, constructorArguments);1123 return doMockSpecific(type, new NiceMockStrategy(), new String[] { methodName }, constructorArgs,1124 methodParameterTypes);1125 }1126 /**1127 * A utility method that may be used to mock several methods in an easy way1128 * (by just passing in the method names of the method you wish to mock). Use1129 * this to handle overloaded methods <i>and</i> overloaded constructors. The1130 * mock object created will support mocking of final and native methods and1131 * invokes a specific constructor based on the supplied argument values.1132 *1133 * @param <T>1134 * the type of the mock object1135 * @param type1136 * the type of the mock object1137 * @param methodName1138 * The names of the methods that should be mocked. If1139 * <code>null</code>, then this method will have the same effect1140 * as just calling {@link #createMock(Class, Method...)} with the1141 * second parameter as <code>new Method[0]</code> (i.e. all1142 * methods in that class will be mocked).1143 * @param methodParameterTypes1144 * Parameter types that defines the method. Note that this is1145 * only needed to separate overloaded methods.1146 * @param constructorArguments1147 * The constructor arguments that will be used to invoke a1148 * certain constructor.1149 * @param constructorParameterTypes1150 * Parameter types that defines the constructor that should be1151 * invoked. Note that this is only needed to separate overloaded1152 * constructors.1153 * @return the mock object.1154 */1155 public static <T> T createPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1156 Object[] constructorArguments, Class<?>[] constructorParameterTypes) {1157 ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),1158 constructorArguments);1159 return doMockSpecific(type, new DefaultMockStrategy(), new String[] { methodName }, constructorArgs,1160 methodParameterTypes);1161 }1162 /**1163 * A utility method that may be used to strictly mock several methods in an1164 * easy way (by just passing in the method names of the method you wish to1165 * mock). Use this to handle overloaded methods <i>and</i> overloaded1166 * constructors. The mock object created will support mocking of final and1167 * native methods and invokes a specific constructor based on the supplied1168 * argument values.1169 *1170 * @param <T>1171 * the type of the mock object1172 * @param type1173 * the type of the mock object1174 * @param methodName1175 * The names of the methods that should be mocked. If1176 * <code>null</code>, then this method will have the same effect1177 * as just calling {@link #createMock(Class, Method...)} with the1178 * second parameter as <code>new Method[0]</code> (i.e. all1179 * methods in that class will be mocked).1180 * @param methodParameterTypes1181 * Parameter types that defines the method. Note that this is1182 * only needed to separate overloaded methods.1183 * @param constructorArguments1184 * The constructor arguments that will be used to invoke a1185 * certain constructor.1186 * @param constructorParameterTypes1187 * Parameter types that defines the constructor that should be1188 * invoked. Note that this is only needed to separate overloaded1189 * constructors.1190 * @return the mock object.1191 */1192 public static <T> T createStrictPartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1193 Object[] constructorArguments, Class<?>[] constructorParameterTypes) {1194 ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),1195 constructorArguments);1196 return doMockSpecific(type, new StrictMockStrategy(), new String[] { methodName }, constructorArgs,1197 methodParameterTypes);1198 }1199 /**1200 * A utility method that may be used to nicely mock several methods in an1201 * easy way (by just passing in the method names of the method you wish to1202 * mock). Use this to handle overloaded methods <i>and</i> overloaded1203 * constructors. The mock object created will support mocking of final and1204 * native methods and invokes a specific constructor based on the supplied1205 * argument values.1206 *1207 * @param <T>1208 * the type of the mock object1209 * @param type1210 * the type of the mock object1211 * @param methodName1212 * The names of the methods that should be mocked. If1213 * <code>null</code>, then this method will have the same effect1214 * as just calling {@link #createMock(Class, Method...)} with the1215 * second parameter as <code>new Method[0]</code> (i.e. all1216 * methods in that class will be mocked).1217 * @param methodParameterTypes1218 * Parameter types that defines the method. Note that this is1219 * only needed to separate overloaded methods.1220 * @param constructorArguments1221 * The constructor arguments that will be used to invoke a1222 * certain constructor.1223 * @param constructorParameterTypes1224 * Parameter types that defines the constructor that should be1225 * invoked. Note that this is only needed to separate overloaded1226 * constructors.1227 * @return the mock object.1228 */1229 public static <T> T createNicePartialMock(Class<T> type, String methodName, Class<?>[] methodParameterTypes,1230 Object[] constructorArguments, Class<?>[] constructorParameterTypes) {1231 ConstructorArgs constructorArgs = new ConstructorArgs(Whitebox.getConstructor(type, constructorParameterTypes),1232 constructorArguments);1233 return doMockSpecific(type, new NiceMockStrategy(), new String[] { methodName }, constructorArgs,1234 methodParameterTypes);1235 }1236 /**1237 * Used to specify expectations on private static methods. If possible use1238 * variant with only method name.1239 */1240 public static synchronized <T> IExpectationSetters<T> expectPrivate(Class<?> clazz, Method method,1241 Object... arguments) throws Exception {1242 return doExpectPrivate(clazz, method, arguments);1243 }1244 /**1245 * Used to specify expectations on private methods. If possible use variant1246 * with only method name.1247 */1248 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, Method method,1249 Object... arguments) throws Exception {1250 return doExpectPrivate(instance, method, arguments);1251 }1252 /**1253 * Used to specify expectations on private methods. Use this method to1254 * handle overloaded methods.1255 */1256 @SuppressWarnings("all")1257 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1258 Class<?>[] parameterTypes, Object... arguments) throws Exception {1259 if (arguments == null) {1260 arguments = new Object[0];1261 }1262 if (instance == null) {1263 throw new IllegalArgumentException("instance cannot be null.");1264 } else if (arguments.length != parameterTypes.length) {1265 throw new IllegalArgumentException(1266 "The length of the arguments must be equal to the number of parameter types.");1267 }1268 Method foundMethod = Whitebox.getMethod(instance.getClass(), methodName, parameterTypes);1269 WhiteboxImpl.throwExceptionIfMethodWasNotFound(instance.getClass(), methodName, foundMethod, parameterTypes);1270 return doExpectPrivate(instance, foundMethod, arguments);1271 }1272 /**1273 * Used to specify expectations on methods using the method name. Works on1274 * for example private or package private methods.1275 */1276 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1277 Object... arguments) throws Exception {1278 if (instance == null) {1279 throw new IllegalArgumentException("Instance or class cannot be null.");1280 }1281 return expectPrivate(instance, methodName, Whitebox.getType(instance), arguments);1282 }1283 /**1284 * Used to specify expectations on methods without specifying a method name.1285 * Works on for example private or package private methods. PowerMock tries1286 * to find a unique method to expect based on the argument parameters. If1287 * PowerMock is unable to locate a unique method you need to revert to using1288 * {@link #expectPrivate(Object, String, Object...)}.1289 */1290 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, Object... arguments)1291 throws Exception {1292 return expectPrivate(instance, null, Whitebox.getType(instance), arguments);1293 }1294 /**1295 * Used to specify expectations on methods using the method name at a1296 * specific place in the class hierarchy (specified by the1297 * <code>where</code> parameter). Works on for example private or package1298 * private methods.1299 * <p>1300 * Use this for overloaded methods.1301 */1302 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1303 Class<?> where, Class<?>[] parameterTypes, Object... arguments) throws Exception {1304 if (instance == null) {1305 throw new IllegalArgumentException("Instance or class to expect cannot be null.");1306 }1307 Method[] methods = null;1308 if (methodName != null) {1309 if (parameterTypes == null) {1310 methods = Whitebox.getMethods(where, methodName);1311 } else {1312 methods = new Method[] { Whitebox.getMethod(where, methodName, parameterTypes) };1313 }1314 }1315 Method methodToExpect;1316 if (methods != null && methods.length == 1) {1317 methodToExpect = methods[0];1318 } else {1319 methodToExpect = WhiteboxImpl.findMethodOrThrowException(instance, null, methodName, arguments);1320 }1321 return doExpectPrivate(instance, methodToExpect, arguments);1322 }1323 /**1324 * Used to specify expectations on methods using the method name at a1325 * specific place in the class hierarchy (specified by the1326 * <code>where</code> parameter). Works on for example private or package1327 * private methods.1328 */1329 public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName,1330 Class<?> where, Object... arguments) throws Exception {1331 return expectPrivate(instance, methodName, where, null, arguments);1332 }1333 /**1334 * This method just delegates to EasyMock class extensions1335 * {@link org.easymock.EasyMock#expectLastCall()} method.1336 *1337 * @see org.easymock.EasyMock#expectLastCall()1338 *1339 * @return The expectation setter.1340 */1341 public static synchronized IExpectationSetters<Object> expectLastCall() {1342 return org.easymock.EasyMock.expectLastCall();1343 }1344 /**1345 * Sometimes it is useful to allow replay and verify on non-mocks. For1346 * example when using partial mocking in some tests and no mocking in other1347 * test-methods, but using the same setUp and tearDown.1348 */1349 public static synchronized void niceReplayAndVerify() {1350 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, true);1351 }1352 /**1353 * Test if a object is a mock created by EasyMock or not.1354 */1355 private static boolean isEasyMocked(Object mock) {1356 return Enhancer.isEnhanced(mock.getClass()) || Proxy.isProxyClass(mock.getClass());1357 }1358 /**1359 * Replay all classes and mock objects known by PowerMock. This includes all1360 * classes that are prepared for test using the {@link PrepareForTest} or1361 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1362 * their static initializers removed by using the1363 * {@link SuppressStaticInitializationFor} annotation. It also includes all1364 * mock instances created by PowerMock such as those created or used by1365 * {@link #createMock(Class, Method...)},1366 * {@link #mockStatic(Class, Method...)},1367 * {@link #expectNew(Class, Object...)},1368 * {@link #createPartialMock(Class, String...)} etc.1369 * <p>1370 * To make it easy to pass in additional mocks <i>not</i> created by the1371 * PowerMock API you can optionally specify them as <tt>additionalMocks</tt>1372 * . These are typically those mock objects you have created using pure1373 * EasyMock or EasyMock class extensions. No additional mocks needs to be1374 * specified if you're only using PowerMock API methods.1375 * <p>1376 * Note that the <tt>additionalMocks</tt> are also automatically verified1377 * when invoking the {@link #verifyAll()} method.1378 *1379 * @param additionalMocks1380 * Mocks not created by the PowerMock API. These are typically1381 * those mock objects you have created using pure EasyMock or1382 * EasyMock class extensions.1383 */1384 public static synchronized void replayAll(Object... additionalMocks) {1385 MockRepository.addObjectsToAutomaticallyReplayAndVerify(additionalMocks);1386 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1387 replay(classToReplayOrVerify);1388 }1389 }1390 /**1391 * Reset all classes and mock objects known by PowerMock. This includes all1392 * classes that are prepared for test using the {@link PrepareForTest} or1393 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1394 * their static initializers removed by using the1395 * {@link SuppressStaticInitializationFor} annotation. It also includes all1396 * mock instances created by PowerMock such as those created or used by1397 * {@link #createMock(Class, Method...)},1398 * {@link #mockStatic(Class, Method...)},1399 * {@link #expectNew(Class, Object...)},1400 * {@link #createPartialMock(Class, String...)} etc.1401 * <p>1402 * To make it easy to pass in additional mocks <i>not</i> created by the1403 * PowerMock API you can optionally specify them as <tt>additionalMocks</tt>1404 * . These are typically those mock objects you have created using pure1405 * EasyMock or EasyMock class extensions. No additional mocks needs to be1406 * specified if you're only using PowerMock API methods.1407 *1408 * @param additionalMocks1409 * Mocks not created by the PowerMock API. These are typically1410 * those mock objects you have created using pure EasyMock or1411 * EasyMock class extensions.1412 */1413 public static synchronized void resetAll(Object... additionalMocks) {1414 MockRepository.addObjectsToAutomaticallyReplayAndVerify(additionalMocks);1415 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1416 reset(classToReplayOrVerify);1417 }1418 }1419 /**1420 * Reset a list of class mocks.1421 */1422 public static synchronized void reset(Class<?>... classMocks) {1423 for (Class<?> type : classMocks) {1424 final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type);1425 if (invocationHandler != null) {1426 invocationHandler.reset();1427 }1428 NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type);1429 if (newInvocationControl != null) {1430 try {1431 newInvocationControl.reset();1432 } catch (AssertionError e) {1433 NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type);1434 }1435 }1436 }1437 }1438 /**1439 * Reset a list of mock objects or classes.1440 */1441 public static synchronized void reset(Object... mocks) {1442 try {1443 for (Object mock : mocks) {1444 if (mock instanceof Class<?>) {1445 reset((Class<?>) mock);1446 } else {1447 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1448 if (invocationControl != null) {1449 invocationControl.reset();1450 } else {1451 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1452 // ignore non-mock1453 } else {1454 /*1455 * Delegate to easy mock class extension if we have1456 * no handler registered for this object.1457 */1458 try {1459 org.easymock.EasyMock.reset(mock);1460 } catch (RuntimeException e) {1461 throw new RuntimeException(mock + " is not a mock object", e);1462 }1463 }1464 }1465 }1466 }1467 } catch (Throwable t) {1468 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false);1469 if (t instanceof RuntimeException) {1470 throw (RuntimeException) t;1471 } else if (t instanceof Error) {1472 throw (Error) t;1473 }1474 throw new RuntimeException(t);1475 }1476 }1477 /**1478 * Verify all classes and mock objects known by PowerMock. This includes all1479 * classes that are prepared for test using the {@link PrepareForTest} or1480 * {@link PrepareOnlyThisForTest} annotations and all classes that have had1481 * their static initializers removed by using the1482 * {@link SuppressStaticInitializationFor} annotation. It also includes all1483 * mock instances created by PowerMock such as those created or used by1484 * {@link #createMock(Class, Method...)},1485 * {@link #mockStatic(Class, Method...)},1486 * {@link #expectNew(Class, Object...)},1487 * {@link #createPartialMock(Class, String...)} etc.1488 * <p>1489 * Note that all <tt>additionalMocks</tt> passed to the1490 * {@link #replayAll(Object...)} method are also verified here1491 * automatically.1492 *1493 */1494 public static synchronized void verifyAll() {1495 for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) {1496 verify(classToReplayOrVerify);1497 }1498 }1499 /**1500 * Switches the mocks or classes to replay mode. Note that you must use this1501 * method when using PowerMock!1502 *1503 * @param mocks1504 * mock objects or classes loaded by PowerMock.1505 * @throws Exception1506 * If something unexpected goes wrong.1507 */1508 public static synchronized void replay(Object... mocks) {1509 try {1510 for (Object mock : mocks) {1511 if (mock instanceof Class<?>) {1512 replay((Class<?>) mock);1513 } else {1514 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1515 if (invocationControl != null) {1516 invocationControl.replay();1517 } else {1518 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1519 // ignore non-mock1520 } else {1521 /*1522 * Delegate to easy mock class extension if we have1523 * no handler registered for this object.1524 */1525 try {1526 org.easymock.EasyMock.replay(mock);1527 } catch (RuntimeException e) {1528 throw new RuntimeException(mock + " is not a mock object", e);1529 }1530 }1531 }1532 }1533 }1534 } catch (Throwable t) {1535 MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false);1536 if (t instanceof RuntimeException) {1537 throw (RuntimeException) t;1538 } else if (t instanceof Error) {1539 throw (Error) t;1540 }1541 throw new RuntimeException(t);1542 }1543 }1544 /**1545 * Switches the mocks or classes to verify mode. Note that you must use this1546 * method when using PowerMock!1547 *1548 * @param objects1549 * mock objects or classes loaded by PowerMock.1550 */1551 public static synchronized void verify(Object... objects) {1552 for (Object mock : objects) {1553 if (mock instanceof Class<?>) {1554 verifyClass((Class<?>) mock);1555 } else {1556 MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock);1557 if (invocationControl != null) {1558 invocationControl.verify();1559 } else {1560 if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {1561 // ignore non-mock1562 } else {1563 /*1564 * Delegate to easy mock class extension if we have no1565 * handler registered for this object.1566 */1567 try {1568 org.easymock.EasyMock.verify(mock);1569 } catch (RuntimeException e) {1570 throw new RuntimeException(mock + " is not a mock object", e);1571 }1572 }1573 }1574 }1575 }1576 }1577 /**1578 * Convenience method for createMock followed by expectNew.1579 *1580 * @param type1581 * The class that should be mocked.1582 * @param arguments1583 * The constructor arguments.1584 * @return A mock object of the same type as the mock.1585 * @throws Exception1586 */1587 public static synchronized <T> T createMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1588 T mock = createMock(type);1589 expectNew(type, arguments).andReturn(mock);1590 return mock;1591 }1592 /**1593 * Convenience method for createMock followed by expectNew when PowerMock1594 * cannot determine which constructor to use automatically. This happens1595 * when you have one constructor taking a primitive type and another one1596 * taking the wrapper type of the primitive. For example <code>int</code>1597 * and <code>Integer</code>.1598 *1599 * @param type1600 * The class that should be mocked.1601 * @param parameterTypes1602 * The constructor parameter types.1603 * @param arguments1604 * The constructor arguments.1605 * @return A mock object of the same type as the mock.1606 * @throws Exception1607 */1608 public static synchronized <T> T createMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1609 Object... arguments) throws Exception {1610 T mock = createMock(type);1611 expectNew(type, parameterTypes, arguments).andReturn(mock);1612 return mock;1613 }1614 /**1615 * Convenience method for createNiceMock followed by expectNew.1616 *1617 * @param type1618 * The class that should be mocked.1619 * @param arguments1620 * The constructor arguments.1621 * @return A mock object of the same type as the mock.1622 * @throws Exception1623 */1624 public static synchronized <T> T createNiceMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1625 T mock = createNiceMock(type);1626 IExpectationSetters<T> expectationSetters = expectNiceNew(type, arguments);1627 if (expectationSetters != null) {1628 expectationSetters.andReturn(mock);1629 }1630 return mock;1631 }1632 /**1633 * Convenience method for createNiceMock followed by expectNew when1634 * PowerMock cannot determine which constructor to use automatically. This1635 * happens when you have one constructor taking a primitive type and another1636 * one taking the wrapper type of the primitive. For example1637 * <code>int</code> and <code>Integer</code>.1638 *1639 * @param type1640 * The class that should be mocked.1641 * @param parameterTypes1642 * The constructor parameter types.1643 * @param arguments1644 * The constructor arguments.1645 * @return A mock object of the same type as the mock.1646 * @throws Exception1647 */1648 public static synchronized <T> T createNiceMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1649 Object... arguments) throws Exception {1650 T mock = createNiceMock(type);1651 IExpectationSetters<T> expectationSetters = expectNiceNew(type, parameterTypes, arguments);1652 if (expectationSetters != null) {1653 expectationSetters.andReturn(mock);1654 }1655 return mock;1656 }1657 /**1658 * Convenience method for createStrictMock followed by expectNew.1659 *1660 * @param type1661 * The class that should be mocked.1662 * @param arguments1663 * The constructor arguments.1664 * @return A mock object of the same type as the mock.1665 * @throws Exception1666 */1667 public static synchronized <T> T createStrictMockAndExpectNew(Class<T> type, Object... arguments) throws Exception {1668 T mock = createStrictMock(type);1669 expectStrictNew(type, arguments).andReturn(mock);1670 return mock;1671 }1672 /**1673 * Convenience method for createStrictMock followed by expectNew when1674 * PowerMock cannot determine which constructor to use automatically. This1675 * happens when you have one constructor taking a primitive type and another1676 * one taking the wrapper type of the primitive. For example1677 * <code>int</code> and <code>Integer</code>.1678 *1679 * @param type1680 * The class that should be mocked.1681 * @param parameterTypes1682 * The constructor parameter types.1683 * @param arguments1684 * The constructor arguments.1685 * @return A mock object of the same type as the mock.1686 * @throws Exception1687 */1688 public static synchronized <T> T createStrictMockAndExpectNew(Class<T> type, Class<?>[] parameterTypes,1689 Object... arguments) throws Exception {1690 T mock = createStrictMock(type);1691 expectStrictNew(type, parameterTypes, arguments).andReturn(mock);1692 return mock;1693 }1694 /**1695 * Allows specifying expectations on new invocations. For example you might1696 * want to throw an exception or return a mock. Note that you must replay1697 * the class when using this method since this behavior is part of the class1698 * mock.1699 * <p>1700 * Use this method when you need to specify parameter types for the1701 * constructor when PowerMock cannot determine which constructor to use1702 * automatically. In most cases you should use1703 * {@link #expectNew(Class, Object...)} instead.1704 */1705 public static synchronized <T> IExpectationSetters<T> expectNew(Class<T> type, Class<?>[] parameterTypes,1706 Object... arguments) throws Exception {1707 return doExpectNew(type, new DefaultMockStrategy(), parameterTypes, arguments);1708 }1709 @SuppressWarnings("unchecked")1710 private static <T> IExpectationSetters<T> doExpectNew(Class<T> type, MockStrategy mockStrategy,1711 Class<?>[] parameterTypes, Object... arguments) throws Exception {1712 if (type == null) {1713 throw new IllegalArgumentException("type cannot be null");1714 } else if (mockStrategy == null) {1715 throw new IllegalArgumentException("Internal error: Mock strategy cannot be null");1716 }1717 final boolean isNiceMock = mockStrategy instanceof NiceMockStrategy;1718 final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getUnmockedType(type);1719 if (!isNiceMock) {1720 if (parameterTypes == null) {1721 WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments);1722 } else {1723 WhiteboxImpl.getConstructor(unmockedType, parameterTypes);1724 }1725 }1726 /*1727 * Check if this type has been mocked before1728 */1729 NewInvocationControl<IExpectationSetters<T>> newInvocationControl = (NewInvocationControl<IExpectationSetters<T>>) MockRepository1730 .getNewInstanceControl(unmockedType);1731 if (newInvocationControl == null) {1732 InvocationSubstitute<T> mock = doMock(InvocationSubstitute.class, false, mockStrategy, null,1733 (Method[]) null);1734 newInvocationControl = new NewInvocationControlImpl<T>(mock, type);1735 MockRepository.putNewInstanceControl(type, newInvocationControl);1736 MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getUnmockedType(type));1737 }1738 if (isNiceMock && (arguments == null || arguments.length == 0)) {1739 return null;1740 }1741 return newInvocationControl.expectSubstitutionLogic(arguments);1742 }1743 /**1744 * Allows specifying expectations on new invocations. For example you might1745 * want to throw an exception or return a mock. Note that you must replay1746 * the class when using this method since this behavior is part of the class1747 * mock.1748 */1749 public static synchronized <T> IExpectationSetters<T> expectNew(Class<T> type, Object... arguments)1750 throws Exception {1751 return doExpectNew(type, new DefaultMockStrategy(), null, arguments);1752 }1753 /**1754 * Allows specifying expectations on new invocations for private member1755 * (inner) classes, local or anonymous classes. For example you might want1756 * to throw an exception or return a mock. Note that you must replay the1757 * class when using this method since this behavior is part of the class1758 * mock.1759 *1760 * @param fullyQualifiedName1761 * The fully-qualified name of the inner/local/anonymous type to1762 * expect.1763 * @param arguments1764 * Optional number of arguments.1765 */1766 @SuppressWarnings("unchecked")1767 public static synchronized <T> IExpectationSetters<T> expectNew(String fullyQualifiedName, Object... arguments)1768 throws Exception {1769 final Class<?> forName = Class.forName(fullyQualifiedName);1770 return (IExpectationSetters<T>) doExpectNew(forName, new DefaultMockStrategy(), null, arguments);1771 }1772 /**1773 * Allows specifying expectations on new invocations. For example you might1774 * want to throw an exception or return a mock.1775 * <p>1776 * This method checks the order of constructor invocations.1777 * <p>1778 * Note that you must replay the class when using this method since this1779 * behavior is part of the class mock.1780 */1781 public static synchronized <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Object... arguments)1782 throws Exception {1783 return doExpectNew(type, new StrictMockStrategy(), null, arguments);1784 }1785 /**1786 * Allows specifying expectations on new invocations. For example you might1787 * want to throw an exception or return a mock. Note that you must replay1788 * the class when using this method since this behavior is part of the class1789 * mock.1790 * <p>1791 * This method checks the order of constructor invocations.1792 * <p>1793 * Use this method when you need to specify parameter types for the1794 * constructor when PowerMock cannot determine which constructor to use1795 * automatically. In most cases you should use1796 * {@link #expectNew(Class, Object...)} instead.1797 */1798 public static synchronized <T> IExpectationSetters<T> expectStrictNew(Class<T> type, Class<?>[] parameterTypes,1799 Object... arguments) throws Exception {1800 return doExpectNew(type, new StrictMockStrategy(), parameterTypes, arguments);1801 }1802 /**1803 * Allows specifying expectations on new invocations. For example you might1804 * want to throw an exception or return a mock.1805 * <p>1806 * This method allows any number of calls to a new constructor without1807 * throwing an exception.1808 * <p>1809 * Note that you must replay the class when using this method since this1810 * behavior is part of the class mock.1811 */1812 public static synchronized <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Object... arguments)1813 throws Exception {1814 return doExpectNew(type, new NiceMockStrategy(), null, arguments);1815 }1816 /**1817 * Allows specifying expectations on new invocations. For example you might1818 * want to throw an exception or return a mock. Note that you must replay1819 * the class when using this method since this behavior is part of the class1820 * mock.1821 * <p>1822 * This method allows any number of calls to a new constructor without1823 * throwing an exception.1824 * <p>1825 * Use this method when you need to specify parameter types for the1826 * constructor when PowerMock cannot determine which constructor to use1827 * automatically. In most cases you should use1828 * {@link #expectNew(Class, Object...)} instead.1829 */1830 public static synchronized <T> IExpectationSetters<T> expectNiceNew(Class<T> type, Class<?>[] parameterTypes,1831 Object... arguments) throws Exception {1832 return doExpectNew(type, new NiceMockStrategy(), parameterTypes, arguments);1833 }1834 /**1835 * Suppress constructor calls on specific constructors only.1836 *1837 * @deprecated Use {@link #suppress(Constructor[])} instead.1838 */1839 public static synchronized void suppressConstructor(Constructor<?>... constructors) {1840 SuppressCode.suppressConstructor(constructors);1841 }1842 /**1843 * This method can be used to suppress the code in a specific constructor.1844 *1845 * @param clazz1846 * The class where the constructor is located.1847 * @param parameterTypes1848 * The parameter types of the constructor to suppress.1849 * @deprecated Use {@link #suppress(Constructor)} instead.1850 */1851 public static synchronized void suppressSpecificConstructor(Class<?> clazz, Class<?>... parameterTypes) {1852 SuppressCode.suppressSpecificConstructor(clazz, parameterTypes);1853 }1854 /**1855 * Suppress all constructors in the given class and it's super classes.1856 *1857 * @param classes1858 * The classes whose constructors will be suppressed.1859 * @deprecated Use {@link #suppress(Constructor[])} instead.1860 */1861 public static synchronized void suppressConstructor(Class<?>... classes) {1862 SuppressCode.suppressConstructor(classes);1863 }1864 /**1865 * Suppress all constructors in the given class.1866 *1867 * @param clazz1868 * The classes whose constructors will be suppressed.1869 * @param excludePrivateConstructors1870 * optionally keep code in private constructors1871 * @deprecated Use {@link #suppress(Constructor[])} instead.1872 */1873 public static synchronized void suppressConstructor(Class<?> clazz, boolean excludePrivateConstructors) {1874 SuppressCode.suppressConstructor(clazz, excludePrivateConstructors);1875 }1876 /**1877 * Suppress specific fields. This works on both instance methods and static1878 * methods. Note that replay and verify are not needed as this is not part1879 * of a mock behavior.1880 *1881 * @deprecated Use {@link #suppress(Field[])} instead.1882 */1883 public static synchronized void suppressField(Field... fields) {1884 SuppressCode.suppressField(fields);1885 }1886 /**1887 * Suppress all fields for these classes.1888 *1889 * @deprecated Use {@link #suppress(Field[])} instead.1890 */1891 public static synchronized void suppressField(Class<?>[] classes) {1892 SuppressCode.suppressField(classes);1893 }1894 /**1895 * Suppress multiple methods for a class.1896 *1897 * @param clazz1898 * The class whose methods will be suppressed.1899 * @param fieldNames1900 * The names of the methods that'll be suppressed. If field names1901 * are empty, <i>all</i> fields in the supplied class will be1902 * suppressed.1903 * @deprecated Use {@link #suppress(Field)} instead.1904 */1905 public static synchronized void suppressField(Class<?> clazz, String... fieldNames) {1906 SuppressCode.suppressField(clazz, fieldNames);1907 }1908 /**1909 * Suppress specific method calls on all types containing this method. This1910 * works on both instance methods and static methods. Note that replay and1911 * verify are not needed as this is not part of a mock behavior.1912 *1913 * @deprecated Use {@link #suppress(Method[])} instead.1914 */1915 public static synchronized void suppressMethod(Method... methods) {1916 SuppressCode.suppressMethod(methods);1917 }1918 /**1919 * Suppress all methods for these classes.1920 *1921 * @param cls1922 * The first class whose methods will be suppressed.1923 * @param additionalClasses1924 * Additional classes whose methods will be suppressed.1925 * @deprecated Use {@link #suppress(Method[])} instead.1926 */1927 public static synchronized void suppressMethod(Class<?> cls, Class<?>... additionalClasses) {1928 SuppressCode.suppressMethod(cls, additionalClasses);1929 }1930 /**1931 * Suppress all methods for these classes.1932 *1933 * @param classes1934 * Classes whose methods will be suppressed.1935 * @deprecated Use {@link #suppress(Method[])} instead.1936 */1937 public static synchronized void suppressMethod(Class<?>[] classes) {1938 SuppressCode.suppressMethod(classes);1939 }1940 /**1941 * Suppress multiple methods for a class.1942 *1943 * @param clazz1944 * The class whose methods will be suppressed.1945 * @param methodName1946 * The first method to be suppress in class <code>clazz</code>.1947 * @param additionalMethodNames1948 * Additional methods to suppress in class <code>clazz</code>.1949 * @deprecated Use {@link #suppress(Method[])} instead.1950 */1951 public static synchronized void suppressMethod(Class<?> clazz, String methodName, String... additionalMethodNames) {1952 SuppressCode.suppressMethod(clazz, methodName, additionalMethodNames);1953 }1954 /**1955 * Suppress multiple methods for a class.1956 *1957 * @param clazz1958 * The class whose methods will be suppressed.1959 * @param methodNames1960 * Methods to suppress in class <code>clazz</code>.1961 * @deprecated Use {@link #suppress(Method[])} instead.1962 */1963 public static synchronized void suppressMethod(Class<?> clazz, String[] methodNames) {1964 SuppressCode.suppressMethod(clazz, methodNames);1965 }1966 /**1967 * Suppress all methods for this class.1968 *1969 * @param clazz1970 * The class which methods will be suppressed.1971 * @param excludePrivateMethods1972 * optionally not suppress private methods1973 * @deprecated Use {@link #suppress(Method[])} instead.1974 */1975 public static synchronized void suppressMethod(Class<?> clazz, boolean excludePrivateMethods) {1976 SuppressCode.suppressMethod(clazz, excludePrivateMethods);1977 }1978 /**1979 * Suppress a specific method call. Use this for overloaded methods.1980 *1981 * @deprecated Use {@link #suppress(Method)} instead.1982 */1983 public static synchronized void suppressMethod(Class<?> clazz, String methodName, Class<?>[] parameterTypes) {1984 SuppressCode.suppressMethod(clazz, methodName, parameterTypes);1985 }1986 @SuppressWarnings("unchecked")1987 private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy,1988 ConstructorArgs constructorArgs, Method... methods) {1989 if (type == null) {1990 throw new IllegalArgumentException("The class to mock cannot be null");1991 }1992 /*1993 * Clear the EasyMock state after the test method is executed.1994 */1995 MockRepository.addAfterMethodRunner(new Runnable() {1996 public void run() {1997 LastControl.reportLastControl(null);1998 }1999 });2000 IMocksControl control = mockStrategy.createMockControl(type);2001 MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());...
doMock
Using AI Code Generation
1public PowerMockRule rule = new PowerMockRule();2public PowerMockRule rule = new PowerMockRule();3public PowerMockRule rule = new PowerMockRule();4public PowerMockRule rule = new PowerMockRule();5public PowerMockRule rule = new PowerMockRule();6public PowerMockRule rule = new PowerMockRule();7public PowerMockRule rule = new PowerMockRule();8public PowerMockRule rule = new PowerMockRule();9public PowerMockRule rule = new PowerMockRule();10public PowerMockRule rule = new PowerMockRule();11public PowerMockRule rule = new PowerMockRule();12public PowerMockRule rule = new PowerMockRule();13public PowerMockRule rule = new PowerMockRule();14public PowerMockRule rule = new PowerMockRule();15public PowerMockRule rule = new PowerMockRule();
doMock
Using AI Code Generation
1PowerMock.mockStatic(PowerMock.class);2EasyMock.expect(PowerMock.doMock(Class.class)).andReturn(Class.class);3PowerMock.replay(PowerMock.class);4PowerMock.verify(PowerMock.class);5PowerMock.mockStatic(PowerMock.class);6EasyMock.expect(PowerMock.doMock(Class.class)).andReturn(Class.class);7PowerMock.replay(PowerMock.class);8PowerMock.verify(PowerMock.class);9PowerMock.mockStatic(PowerMock.class);10EasyMock.expect(PowerMock.doMock(Class.class)).andReturn(Class.class);11PowerMock.replay(PowerMock.class);12PowerMock.verify(PowerMock.class);13PowerMock.mockStatic(PowerMock.class);14EasyMock.expect(PowerMock.doMock(Class.class)).andReturn(Class.class);15PowerMock.replay(PowerMock.class);16PowerMock.verify(PowerMock.class);17PowerMock.mockStatic(PowerMock.class);18EasyMock.expect(PowerMock.doMock(Class.class)).andReturn(Class.class);19PowerMock.replay(PowerMock.class);20PowerMock.verify(PowerMock.class);21PowerMock.mockStatic(PowerMock.class);22EasyMock.expect(PowerMock.doMock(Class.class)).andReturn(Class.class);23PowerMock.replay(PowerMock.class);24PowerMock.verify(PowerMock.class);25PowerMock.mockStatic(PowerMock.class);
doMock
Using AI Code Generation
1doMock(Class<?> classToMock, Object mockObject)2doMockStatic(Class<?> classToMock)3doMockStatic(Class<?> classToMock, Object mockObject)4doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2)5doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3)6doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3, Object mockObject4)7doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3, Object mockObject4, Object mockObject5)8doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3, Object mockObject4, Object mockObject5, Object mockObject6)9doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3, Object mockObject4, Object mockObject5, Object mockObject6, Object mockObject7)10doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3, Object mockObject4, Object mockObject5, Object mockObject6, Object mockObject7, Object mockObject8)11doMockStatic(Class<?> classToMock, Object mockObject, Object mockObject2, Object mockObject3,
doMock
Using AI Code Generation
1PowerMock.mockStatic(Math.class);2EasyMock.expect(Math.max(10, 20)).andReturn(10);3PowerMock.replay(Math.class);4PowerMock.mockStatic(Math.class);5EasyMock.expect(Math.max(10, 20)).andReturn(10);6PowerMock.replay(Math.class);7PowerMock.mockStatic(Math.class);8EasyMock.expect(Math.max(10, 20)).andReturn(10);9PowerMock.replay(Math.class);10PowerMock.mockStatic(Math.class);11EasyMock.expect(Math.max(10, 20)).andReturn(10);12PowerMock.replay(Math.class);13PowerMock.mockStatic(Math.class);14EasyMock.expect(Math.max(10, 20)).andReturn(10);15PowerMock.replay(Math.class);16PowerMock.mockStatic(Math.class);17EasyMock.expect(Math.max(10, 20)).andReturn(10);18PowerMock.replay(Math.class);
doMock
Using AI Code Generation
1import org.easymock.EasyMock;2import org.junit.Assert;3import org.junit.Test;4import org.junit.runner.RunWith;5import org.powermock.api.easymock.PowerMock;6import org.powermock.core.classloader.annotations.PrepareForTest;7import org.powermock.modules.junit4.PowerMockRunner;8import org.powermock.modules.junit4.PowerMockRunnerDelegate;9@RunWith(PowerMockRunner.class)10@PowerMockRunnerDelegate(RunWith.class)11@PrepareForTest({PowerMock.class})12public class PowerMockTest {13 public void testMock() {14 PowerMock.doMock(PowerMock.class);15 PowerMock.expectLastCall().andReturn(null);16 PowerMock.replay(PowerMock.class);17 Assert.assertEquals(null, PowerMock.createMock(PowerMock.class));18 PowerMock.verify(PowerMock.class);19 }20}21 at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:60)22 at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:43)23 at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:34)24 at org.powermock.api.easymock.PowerMockTest.testMock(PowerMockTest.java:25)25 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)26 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)27 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)28 at java.lang.reflect.Method.invoke(Method.java:498)29 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!