/** * Use doAnswer() when you want to stub a void method with generic * {@link Answer}. * <p> * Stubbing voids requires different approach from * {@link Mockito#when(Object)} because the compiler does not like void * methods inside brackets... * <p> * Example: * * <pre> * doAnswer(new Answer() { * public Object answer(InvocationOnMock invocation) { * Object[] args = invocation.getArguments(); * Mock mock = invocation.getMock(); * return null; * } * }).when(mock).someMethod(); * </pre> * <p> * See examples in javadoc for {@link Mockito} class * * @param answer * to answer when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doAnswer(Answer<?> answer) { return POWERMOCKITO_CORE.doAnswer(answer); }
@SuppressWarnings("rawtypes") public PowerMockitoStubber doAnswer(Answer answer) { // We change the context classloader to the current CL in order for the Mockito // framework to load it's plugins (such as MockMaker) correctly. final ClassLoader originalCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); try { getMockingProgress().stubbingStarted(); } finally { Thread.currentThread().setContextClassLoader(originalCL); } getMockingProgress().resetOngoingStubbing(); return (PowerMockitoStubber) new PowerMockitoStubberImpl().doAnswer(answer); }
/** * Verify a private method invocation with a given verification mode. * * @see {@link Mockito#verify(Object)} * @throws Exception * If something unexpected goes wrong. */ public static PrivateMethodVerification verifyPrivate(Object object, VerificationMode verificationMode) throws Exception { Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted( POWERMOCKITO_CORE.wrapInMockitoSpecificVerificationMode(object, verificationMode)); return new DefaultPrivateMethodVerification(object); }
/** * Verifies certain behavior happened at least once / exact number of times * / never. E.g: * * <pre> * verifyStatic(times(5)); * ClassWithStaticMethod.someStaticMethod("was called five times"); * * verifyStatic(atLeast(2)); * ClassWithStaticMethod.someStaticMethod("was called at least two times"); * * //you can use flexible argument matchers, e.g: * verifyStatic(atLeastOnce()); * ClassWithStaticMethod.someMethod(<b>anyString()</b>); * </pre> * * <b>times(1) is the default</b> and can be omitted * <p> * * @param verificationMode * times(x), atLeastOnce() or never() * @deprecated Will be removed in PowerMock 2. Please use {@link #verifyStatic(Class, VerificationMode)} */ @Deprecated public static synchronized void verifyStatic(VerificationMode verificationMode) { Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted( POWERMOCKITO_CORE.wrapInStaticVerificationMode(verificationMode)); }
/** * Spy on objects that are final or otherwise not "spyable" from * normal Mockito. * * @param <T> the type of the mock object * @param object the object to spy on * @return the spy object. * @see PowerMockito#spy(Object) */ @SuppressWarnings("unchecked") public static synchronized <T> T spy(T object) { return POWERMOCKITO_CORE.spy(object); }
/** * Use doThrow() when you want to stub the void method with an exception. * <p> * Stubbing voids requires different approach from * {@link PowerMockito#when(Object)} because the compiler does not like void * methods inside brackets... * <p> * Example: * <p> * <pre> * doThrow(new RuntimeException()).when(mock).someVoidMethod(); * </pre> * * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doThrow(Throwable toBeThrown) { return POWERMOCKITO_CORE.doThrow(toBeThrown); }
/** * Verifies certain behavior happened at least once / exact number of times * / never. E.g: * <p> * <pre> * verifyNew(ClassWithStaticMethod.class, times(5)); * * verifyNew(ClassWithStaticMethod.class, atLeast(2)); * * //you can use flexible argument matchers, e.g: * verifyNew(ClassWithStaticMethod.class, atLeastOnce()); * </pre> * <p> * <b>times(1) is the default</b> and can be omitted * <p> * * @param mock to be verified * @param mode times(x), atLeastOnce() or never() */ @SuppressWarnings("unchecked") public static <T> ConstructorArgumentsVerification verifyNew(Class<T> mock, VerificationMode mode) { return POWERMOCKITO_CORE.verifyNew(mock, mode); }
return POWERMOCKITO_CORE.doNothing();
return POWERMOCKITO_CORE.doCallRealMethod();
return POWERMOCKITO_CORE.doReturn(toBeReturned);
/** * Verifies certain behavior of the <code>mockedClass</code> happened at least once / exact number of times * / never. E.g: * * <pre> * verifyStatic(ClassWithStaticMethod.class, times(5)); * ClassWithStaticMethod.someStaticMethod("was called five times"); * * verifyStatic(ClassWithStaticMethod.class, atLeast(2)); * ClassWithStaticMethod.someStaticMethod("was called at least two times"); * * //you can use flexible argument matchers, e.g: * verifyStatic(ClassWithStaticMethod.class, atLeastOnce()); * ClassWithStaticMethod.someMethod(<b>anyString()</b>); * </pre> * * <b>times(1) is the default</b> and can be omitted * <p> * * @param mockedClass the mocked class behavior of that have to be verified. * @param verificationMode * times(x), atLeastOnce() or never() * */ public static synchronized <T> void verifyStatic(Class<T> mockedClass, VerificationMode verificationMode) { Whitebox.getInternalState(Mockito.class, MockingProgress.class).verificationStarted( POWERMOCKITO_CORE.wrapInStaticVerificationMode(mockedClass, verificationMode)); }
/** * Use doThrow() when you want to stub the void method with an exception. * <p> * Stubbing voids requires different approach from * {@link Mockito#when(Object)} because the compiler does not like void * methods inside brackets... * <p> * Example: * * <pre> * doThrow(new RuntimeException()).when(mock).someVoidMethod(); * </pre> * * @param toBeThrown * to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doThrow(Throwable toBeThrown) { return POWERMOCKITO_CORE.doAnswer(new ThrowsException(toBeThrown)); }
MockRepository.putAdditionalState("VerificationMode", POWERMOCKITO_CORE.wrapInMockitoSpecificVerificationMode( mock, mode)); if (invocationControl == null) {
public static PowerMockitoStubber doReturn(Object toBeReturned, Object... othersToBeReturned) { if (othersToBeReturned != null && othersToBeReturned.length == 0) { return doReturn(toBeReturned); } return POWERMOCKITO_CORE.doAnswer(new ChainReturns(toBeReturned, othersToBeReturned)); } }
return POWERMOCKITO_CORE.doAnswer(new DoesNothing());
return POWERMOCKITO_CORE.doAnswer(new Returns(toBeReturned));
return POWERMOCKITO_CORE.doAnswer(new CallsRealMethods());
public PowerMockitoStubber doAnswer(final Answer answer) { return doAnswer(new Callable<Stubber>() { @Override public Stubber call() throws Exception { return Mockito.doAnswer(answer); } }); }
public PowerMockitoStubber doNothing() { return doAnswer(new Callable<Stubber>() { @Override public Stubber call() throws Exception { return Mockito.doNothing(); } }); }
public PowerMockitoStubber doReturn(final Object toBeReturned) { return doAnswer(new Callable<Stubber>() { @Override public Stubber call() throws Exception { return Mockito.doReturn(toBeReturned); } }); }