All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.stubbing.Stubber Maven / Gradle / Ivy

There is a newer version: 5.13.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.stubbing;

import org.mockito.Mockito;

/**
 * Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style
 * 

* Example: *


 *   doThrow(new RuntimeException()).when(mockedList).clear();
 *   
 *   //following throws RuntimeException:
 *   mockedList.clear();
 * 
* * Also useful when stubbing consecutive calls: * *

 *   doThrow(new RuntimeException("one")).
 *   doThrow(new RuntimeException("two"))
 *   .when(mock).someVoidMethod();
 * 
* * Read more about those methods: *

* {@link Mockito#doThrow(Throwable[])} *

* {@link Mockito#doAnswer(Answer)} *

* {@link Mockito#doNothing()} *

* {@link Mockito#doReturn(Object)} *

* * See examples in javadoc for {@link Mockito} */ @SuppressWarnings("unchecked") public interface Stubber { /** * Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style *

* Example: *


     *   doThrow(new RuntimeException())
     *   .when(mockedList).clear();
     *   
     *   //following throws RuntimeException:
     *   mockedList.clear();
     * 
* * Read more about those methods: *

* {@link Mockito#doThrow(Throwable[])} *

* {@link Mockito#doAnswer(Answer)} *

* {@link Mockito#doNothing()} *

* {@link Mockito#doReturn(Object)} *

* * See examples in javadoc for {@link Mockito} * * @param mock The mock * @return select method for stubbing */ T when(T mock); /** * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Throwable[])} style: *


     *   doThrow(new RuntimeException("one")).
     *   doThrow(new RuntimeException("two"))
     *       .when(mock).someVoidMethod();
     * 
* See javadoc for {@link Mockito#doThrow(Throwable[])} * * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ Stubber doThrow(Throwable... toBeThrown); /** * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Class)} style: *

     *   doThrow(RuntimeException.class).
     *   doThrow(IllegalArgumentException.class)
     *       .when(mock).someVoidMethod();
     * 
* See javadoc for {@link Mockito#doThrow(Class)} * * @param toBeThrown exception class to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing * * @since 2.1.0 */ Stubber doThrow(Class toBeThrown); /** * Use it for stubbing consecutive calls in {@link Mockito#doThrow(Class)} style: *

     *   doThrow(RuntimeException.class).
     *   doThrow(IllegalArgumentException.class)
     *       .when(mock).someVoidMethod();
     * 
* See javadoc for {@link Mockito#doThrow(Class)} * * @param toBeThrown exception class to be thrown when the stubbed method is called * @param nextToBeThrown exception class next to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing * * @since 2.1.0 */ // Additional method helps users of JDK7+ to hide heap pollution / unchecked generics array creation @SuppressWarnings ({"unchecked", "varargs"}) Stubber doThrow(Class toBeThrown, Class... nextToBeThrown); /** * Use it for stubbing consecutive calls in {@link Mockito#doAnswer(Answer)} style: *

     *   doAnswer(answerOne).
     *   doAnswer(answerTwo)
     *       .when(mock).someVoidMethod();
     * 
* See javadoc for {@link Mockito#doAnswer(Answer)} * * @param answer to answer when the stubbed method is called * @return stubber - to select a method for stubbing */ Stubber doAnswer(Answer answer); /** * Use it for stubbing consecutive calls in {@link Mockito#doNothing()} style: *

     *   doNothing().
     *   doThrow(new RuntimeException("two"))
     *       .when(mock).someVoidMethod();
     * 
* See javadoc for {@link Mockito#doNothing()} * * @return stubber - to select a method for stubbing */ Stubber doNothing(); /** * Use it for stubbing consecutive calls in {@link Mockito#doReturn(Object)} style. *

* See javadoc for {@link Mockito#doReturn(Object)} * * @param toBeReturned to be returned when the stubbed method is called * @return stubber - to select a method for stubbing */ Stubber doReturn(Object toBeReturned); /** * Use it for stubbing consecutive calls in {@link Mockito#doReturn(Object)} style. *

* See javadoc for {@link Mockito#doReturn(Object, Object...)} * * @param toBeReturned to be returned when the stubbed method is called * @param nextToBeReturned to be returned in consecutive calls when the stubbed method is called * @return stubber - to select a method for stubbing */ @SuppressWarnings({"unchecked", "varargs"}) Stubber doReturn(Object toBeReturned, Object... nextToBeReturned); /** * Use it for stubbing consecutive calls in {@link Mockito#doCallRealMethod()} style. *

* See javadoc for {@link Mockito#doCallRealMethod()} * * @return stubber - to select a method for stubbing */ Stubber doCallRealMethod(); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy