/** * Use <code>doAnswer()</code> 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 class="code"><code class="java"> * doAnswer(new Answer() { * public Object answer(InvocationOnMock invocation) { * Object[] args = invocation.getArguments(); * Mock mock = invocation.getMock(); * return null; * }}) * .when(mock).someMethod(); * </code></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 Stubber doAnswer(Answer answer) { return MOCKITO_CORE.doAnswer(answer); }
/** * Use <code>doThrow()</code> 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 class="code"><code class="java"> * doThrow(new RuntimeException()).when(mock).someVoidMethod(); * </code></pre> * * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ public static Stubber doThrow(Throwable toBeThrown) { return MOCKITO_CORE.doAnswer(new ThrowsException(toBeThrown)); }
/** * Use <code>doThrow()</code> when you want to stub the void method to throw exception of specified class. * <p> * A new exception instance will be created for each method invocation. * <p> * Stubbing voids requires different approach from {@link Mockito#when(Object)} because the compiler does not like void methods inside brackets... * <p> * Example: * * <pre class="code"><code class="java"> * doThrow(RuntimeException.class).when(mock).someVoidMethod(); * </code></pre> * * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing * @since 1.9.0 */ public static Stubber doThrow(Class<? extends Throwable> toBeThrown) { return MOCKITO_CORE.doAnswer(new ThrowsExceptionClass(toBeThrown)); }
return MOCKITO_CORE.doAnswer(new CallsRealMethods());
return MOCKITO_CORE.doAnswer(new DoesNothing());
return MOCKITO_CORE.doAnswer(new Returns(toBeReturned));
/** * 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 Stubber doAnswer(Answer answer) { return MOCKITO_CORE.doAnswer(answer); }
/** * 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 Stubber doThrow(Throwable toBeThrown) { return MOCKITO_CORE.doAnswer(new ThrowsException(toBeThrown)); }
/** * Use doThrow() when you want to stub the void method to throw exception of specified class. * <p> * A new exception instance will be created for each method invocation. * <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(RuntimeException.class).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 Stubber doThrow(Class<? extends Throwable> toBeThrown) { return MOCKITO_CORE.doAnswer(new ThrowsExceptionClass(toBeThrown)); }
return MOCKITO_CORE.doAnswer(new Returns(toBeReturned));
return MOCKITO_CORE.doAnswer(new CallsRealMethods());
return MOCKITO_CORE.doAnswer(new DoesNothing());